Class: Mutations::Projects::SyncFork

Inherits:
BaseMutation
  • Object
show all
Includes:
FindsProject
Defined in:
app/graphql/mutations/projects/sync_fork.rb

Constant Summary

Constants inherited from BaseMutation

BaseMutation::ERROR_MESSAGE

Constants included from Gitlab::Graphql::Authorize::AuthorizeResource

Gitlab::Graphql::Authorize::AuthorizeResource::ConfigurationError, Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR

Instance Method Summary collapse

Methods inherited from BaseMutation

#api_user?, authorization, authorized?, authorizes_object?, #current_user, #errors_on_object, #load_application_object, #read_only?, #ready?, #unauthorized_object

Methods included from Gitlab::Graphql::Authorize::AuthorizeResource

#authorize!, #authorized_resource?, #find_object, #raise_resource_not_available_error!

Instance Method Details

#authorized_find!(project_path, target_branch) ⇒ Object



58
59
60
61
62
63
64
# File 'app/graphql/mutations/projects/sync_fork.rb', line 58

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

#enqueue_sync_fork(project, target_branch, details) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/graphql/mutations/projects/sync_fork.rb', line 35

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

#rate_limit_throttled?(project) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/graphql/mutations/projects/sync_fork.rb', line 50

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

#resolve(project_path:, target_branch:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/graphql/mutations/projects/sync_fork.rb', line 22

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

  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

#respond(details, errors) ⇒ Object



54
55
56
# File 'app/graphql/mutations/projects/sync_fork.rb', line 54

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