Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sync_fork.rb « projects « mutations « graphql « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4520f6388c5bb2ae0d16b3268a2010607a1bc8f5 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

module Mutations
  module Projects
    class SyncFork < BaseMutation
      graphql_name 'ProjectSyncFork'

      include FindsProject

      argument :project_path, GraphQL::Types::ID,
        required: true,
        description: 'Full path of the project to initialize.'

      argument :target_branch, GraphQL::Types::String,
        required: true,
        description: 'Ref of the fork to fetch into.'

      field :details, Types::Projects::ForkDetailsType,
        null: true,
        description: 'Updated fork details.'

      def resolve(project_path:, target_branch:)
        project = authorized_find!(project_path, target_branch)

        return respond(nil, ['Feature flag is disabled']) unless Feature.enabled?(:synchronize_fork,
          project.fork_source)

        return respond(nil, ['Target branch does not exist']) unless project.repository.branch_exists?(target_branch)

        details_resolver = Resolvers::Projects::ForkDetailsResolver.new(object: project, context: context, field: nil)
        details = details_resolver.resolve(ref: target_branch)

        return respond(nil, ['This branch of this project cannot be updated from the upstream']) unless details

        enqueue_sync_fork(project, target_branch, details)
      end

      def enqueue_sync_fork(project, target_branch, details)
        return respond(details, []) if details.counts[:behind] == 0

        if details.has_conflicts?
          return respond(details, ['The synchronization cannot happen due to the merge conflict'])
        end

        return respond(details, ['This service has been called too many times.']) if rate_limit_throttled?(project)
        return respond(details, ['Another fork sync is already in progress']) unless details.exclusive_lease.try_obtain

        ::Projects::Forks::SyncWorker.perform_async(project.id, current_user.id, target_branch) # rubocop:disable CodeReuse/Worker

        respond(details, [])
      end

      def rate_limit_throttled?(project)
        Gitlab::ApplicationRateLimiter.throttled?(:project_fork_sync, scope: [project, current_user])
      end

      def respond(details, errors)
        { details: details, errors: errors }
      end

      def authorized_find!(project_path, target_branch)
        project = find_object(project_path)

        return project if ::Gitlab::UserAccess.new(current_user, container: project).can_push_to_branch?(target_branch)

        raise_resource_not_available_error!
      end
    end
  end
end