File: copy_timelogs_worker.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (35 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module WorkItems
  class CopyTimelogsWorker
    include ApplicationWorker

    data_consistency :sticky
    deduplicate :until_executed
    idempotent!
    feature_category :team_planning
    urgency :high

    BATCH_SIZE = 100
    def perform(from_issue_id, to_issue_id)
      Gitlab::AppLogger.info("Copying timelogs from issue #{from_issue_id} to issue #{to_issue_id}")

      from_issue = Issue.find_by_id(from_issue_id)
      return if from_issue.nil? || from_issue.timelogs.empty?

      to_issue = Issue.find_by_id(to_issue_id)
      return if to_issue.nil?

      reset_attributes = { project_id: to_issue.project_id, issue_id: to_issue.id }
      ApplicationRecord.transaction do
        from_issue.timelogs.each_batch(of: BATCH_SIZE) do |timelogs|
          new_timelogs_attributes = timelogs.map do |timelog|
            timelog.attributes.except('id').merge(reset_attributes)
          end

          Timelog.insert_all!(new_timelogs_attributes)
        end
      end
    end
  end
end