summaryrefslogtreecommitdiff
path: root/app/services/projects/move_forks_service.rb
blob: 33f0bab12c95cecd1f23a8b85a9b4d0b50e2c5e6 (plain)
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
36
37
# frozen_string_literal: true

module Projects
  class MoveForksService < BaseMoveRelationsService
    def execute(source_project, remove_remaining_elements: true)
      return unless super && source_project.fork_network

      Project.transaction(requires_new: true) do
        move_fork_network_members
        update_root_project
        refresh_forks_count

        success
      end
    end

    private

    # rubocop: disable CodeReuse/ActiveRecord
    def move_fork_network_members
      ForkNetworkMember.where(project: source_project).update_all(project_id: @project.id)
      ForkNetworkMember.where(forked_from_project: source_project).update_all(forked_from_project_id: @project.id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def update_root_project
      # Update root network project
      ForkNetwork.where(root_project: source_project).update_all(root_project_id: @project.id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def refresh_forks_count
      Projects::ForksCountService.new(@project).refresh_cache
    end
  end
end