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?, #raise_resource_not_available_error!

Instance Method Details

#access_level_attributes(type) ⇒ Object



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

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

#protected_branch_params(name) ⇒ Object



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

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



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

def resolve(project_path:, name:)
  project = authorized_find!(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