Class: Mutations::BranchRules::Create

Inherits:
Mutations::BaseMutation show all
Defined in:
app/graphql/mutations/branch_rules/create.rb

Constant Summary

Constants inherited from Mutations::BaseMutation

Mutations::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 Mutations::BaseMutation

#api_user?, authorization, authorization_scopes, 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_find!, #authorized_resource?, #find_object, #raise_resource_not_available_error!

Instance Method Details

#access_level_attributes(type) ⇒ Object



47
48
49
50
51
52
53
# File 'app/graphql/mutations/branch_rules/create.rb', line 47

def access_level_attributes(type)
  ::ProtectedRefs::AccessLevelParams.new(
    type,
    {},
    with_defaults: true
  ).access_levels
end

#protected_branch_params(name) ⇒ Object



39
40
41
42
43
44
45
# File 'app/graphql/mutations/branch_rules/create.rb', line 39

def protected_branch_params(name)
  {
    name: name,
    push_access_levels_attributes: access_level_attributes(:push),
    merge_access_levels_attributes: access_level_attributes(:merge)
  }
end

#resolve(project_path:, name:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/graphql/mutations/branch_rules/create.rb', line 21

def resolve(project_path:, name:)
  project = Project.find_by_full_path(project_path)

  service_params = protected_branch_params(name)
  protected_branch = ::ProtectedBranches::CreateService.new(project, current_user, service_params).execute

  if protected_branch.persisted?
    {
      branch_rule: ::Projects::BranchRule.new(project, protected_branch),
      errors: []
    }
  else
    { errors: errors_on_object(protected_branch) }
  end
rescue Gitlab::Access::AccessDeniedError
  raise_resource_not_available_error!
end