diff --git a/.rubocop_todo/gitlab/bounded_contexts.yml b/.rubocop_todo/gitlab/bounded_contexts.yml index cd6c2e509081d319f0c588646b46f5f6f46d629a..3cfc9b435de2b44bd7deb7fd4854e778b06384b8 100644 --- a/.rubocop_todo/gitlab/bounded_contexts.yml +++ b/.rubocop_todo/gitlab/bounded_contexts.yml @@ -154,7 +154,6 @@ Gitlab/BoundedContexts: - 'app/graphql/mutations/branch_rules/create.rb' - 'app/graphql/mutations/branch_rules/delete.rb' - 'app/graphql/mutations/branch_rules/update.rb' - - 'app/graphql/mutations/branches/create.rb' - 'app/graphql/mutations/commits/create.rb' - 'app/graphql/mutations/concerns/mutations/assignable.rb' - 'app/graphql/mutations/concerns/mutations/finds_namespace.rb' diff --git a/app/graphql/mutations/branches/create.rb b/app/graphql/mutations/branches/create.rb deleted file mode 100644 index 94ac4d3cabdd679a8feb6fb062113c8013a7ec32..0000000000000000000000000000000000000000 --- a/app/graphql/mutations/branches/create.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -module Mutations - module Branches - class Create < BaseMutation - graphql_name 'CreateBranch' - - include FindsProject - - argument :project_path, GraphQL::Types::ID, - required: true, - description: 'Project full path the branch is associated with.' - - argument :name, GraphQL::Types::String, - required: true, - description: 'Name of the branch.' - - argument :ref, - GraphQL::Types::String, - required: true, - description: 'Branch name or commit SHA to create branch from.' - - field :branch, - Types::BranchType, - null: true, - description: 'Branch after mutation.' - - authorize :push_code - - def resolve(project_path:, name:, ref:) - project = authorized_find!(project_path) - - result = ::Branches::CreateService.new(project, current_user) - .execute(name, ref) - - { - branch: (result[:branch] if result[:status] == :success), - errors: Array.wrap(result[:message]) - } - end - end - end -end diff --git a/app/graphql/mutations/branches/delete.rb b/app/graphql/mutations/branches/delete.rb deleted file mode 100644 index 20157387b7dd4182d45554caf5bd5cecc9ead03e..0000000000000000000000000000000000000000 --- a/app/graphql/mutations/branches/delete.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -module Mutations - module Branches # rubocop:disable Gitlab/BoundedContexts -- Existing module - class Delete < BaseMutation - graphql_name 'BranchDelete' - - include FindsProject - - argument :project_path, GraphQL::Types::ID, - required: true, - description: 'Project full path the branch is associated with.' - - argument :name, GraphQL::Types::String, - required: true, - description: 'Name of the branch.' - - field :branch, - Types::BranchType, - null: true, - description: 'Branch after mutation.' - - authorize :push_code - - def resolve(project_path:, name:) - project = authorized_find!(project_path) - - result = ::Branches::DeleteService.new(project, current_user).execute(name) - - { - branch: (result.payload[:branch] if result.error?), - errors: result.errors - } - end - end - end -end diff --git a/app/graphql/mutations/projects/blobs_remove.rb b/app/graphql/mutations/projects/blobs_remove.rb index 228a654bce428cb6faa2b4cfc0eb7f5b7e11b662..504394a494a53c5330da9504d3b98afcda8030d7 100644 --- a/app/graphql/mutations/projects/blobs_remove.rb +++ b/app/graphql/mutations/projects/blobs_remove.rb @@ -31,7 +31,7 @@ class BlobsRemove < BaseMutation def resolve(project_path:, blob_oids:) project = authorized_find!(project_path) - result = Repositories::RewriteHistoryService.new(project, current_user).async_execute(blob_oids: blob_oids) + result = ::Repositories::RewriteHistoryService.new(project, current_user).async_execute(blob_oids: blob_oids) return { errors: result.errors } if result.error? diff --git a/app/graphql/mutations/projects/text_replace.rb b/app/graphql/mutations/projects/text_replace.rb index b73455daf338d30cf25e53e5d7008a3d3cb1c00b..c55418ee89c1c559522a6a433d0846e139b7b2e0 100644 --- a/app/graphql/mutations/projects/text_replace.rb +++ b/app/graphql/mutations/projects/text_replace.rb @@ -40,7 +40,8 @@ class TextReplace < BaseMutation def resolve(project_path:, replacements:) project = authorized_find!(project_path) - result = Repositories::RewriteHistoryService.new(project, current_user).async_execute(redactions: replacements) + result = ::Repositories::RewriteHistoryService.new(project, current_user) + .async_execute(redactions: replacements) return { errors: result.errors } if result.error? diff --git a/app/graphql/mutations/repositories/branches/create.rb b/app/graphql/mutations/repositories/branches/create.rb new file mode 100644 index 0000000000000000000000000000000000000000..dc90215245d6bdd8c61b211a2cc1ae6405ef0a2a --- /dev/null +++ b/app/graphql/mutations/repositories/branches/create.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Mutations + module Repositories + module Branches + class Create < BaseMutation + graphql_name 'CreateBranch' + + include FindsProject + + argument :project_path, GraphQL::Types::ID, + required: true, + description: 'Project full path the branch is associated with.' + + argument :name, GraphQL::Types::String, + required: true, + description: 'Name of the branch.' + + argument :ref, + GraphQL::Types::String, + required: true, + description: 'Branch name or commit SHA to create branch from.' + + field :branch, + Types::BranchType, + null: true, + description: 'Branch after mutation.' + + authorize :push_code + + def resolve(project_path:, name:, ref:) + project = authorized_find!(project_path) + + result = ::Branches::CreateService.new(project, current_user) + .execute(name, ref) + + { + branch: (result[:branch] if result[:status] == :success), + errors: Array.wrap(result[:message]) + } + end + end + end + end +end diff --git a/app/graphql/mutations/repositories/branches/delete.rb b/app/graphql/mutations/repositories/branches/delete.rb new file mode 100644 index 0000000000000000000000000000000000000000..930af1accb6abc4f46e1e63c0900c1fa86347509 --- /dev/null +++ b/app/graphql/mutations/repositories/branches/delete.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Mutations + module Repositories + module Branches + class Delete < BaseMutation + graphql_name 'BranchDelete' + + include FindsProject + + argument :project_path, GraphQL::Types::ID, + required: true, + description: 'Project full path the branch is associated with.' + + argument :name, GraphQL::Types::String, + required: true, + description: 'Name of the branch.' + + field :branch, + Types::BranchType, + null: true, + description: 'Branch after mutation.' + + authorize :push_code + + def resolve(project_path:, name:) + project = authorized_find!(project_path) + + result = ::Branches::DeleteService.new(project, current_user).execute(name) + + { + branch: (result.payload[:branch] if result.error?), + errors: result.errors + } + end + end + end + end +end diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 90250e5b1273c1a54f66294bad4ee5b08da6403c..9d5bf8d1e6ae07706aecff9c00b5ee8fac7555d4 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -39,8 +39,8 @@ class MutationType < BaseObject mount_mutation Mutations::Boards::Lists::Create mount_mutation Mutations::Boards::Lists::Update mount_mutation Mutations::Boards::Lists::Destroy - mount_mutation Mutations::Branches::Create, calls_gitaly: true - mount_mutation Mutations::Branches::Delete, calls_gitaly: true + mount_mutation Mutations::Repositories::Branches::Create, calls_gitaly: true + mount_mutation Mutations::Repositories::Branches::Delete, calls_gitaly: true mount_mutation Mutations::Clusters::Agents::Create mount_mutation Mutations::Clusters::Agents::Delete mount_mutation Mutations::Clusters::AgentTokens::Create diff --git a/spec/graphql/mutations/branches/create_spec.rb b/spec/graphql/mutations/repositories/branches/create_spec.rb similarity index 68% rename from spec/graphql/mutations/branches/create_spec.rb rename to spec/graphql/mutations/repositories/branches/create_spec.rb index 6bfbb1a0072bf4b2cd3ff87de2ce7c9b59276d12..41edd3a8e929eaa21db26692c23581c3c261cb21 100644 --- a/spec/graphql/mutations/branches/create_spec.rb +++ b/spec/graphql/mutations/repositories/branches/create_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Mutations::Branches::Create, feature_category: :api do +RSpec.describe Mutations::Repositories::Branches::Create, feature_category: :api do include GraphqlHelpers let_it_be(:project) { create(:project, :public, :repository) } @@ -11,31 +11,33 @@ subject(:mutation) { described_class.new(object: nil, context: query_context, field: nil) } describe '#resolve' do - subject { mutation.resolve(project_path: project.full_path, name: branch, ref: ref) } + subject(:resolve) { mutation.resolve(project_path: project.full_path, name: branch, ref: ref) } let(:branch) { 'new_branch' } let(:ref) { 'master' } - let(:mutated_branch) { subject[:branch] } + let(:mutated_branch) { resolve[:branch] } it 'raises an error if the resource is not accessible to the user' do - expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) end context 'when the user can create a branch' do - before do + before_all do project.add_developer(current_user) + end + before do allow_next_instance_of(::Branches::CreateService, project, current_user) do |create_service| allow(create_service).to receive(:execute).with(branch, ref) { service_result } end end context 'when service successfully creates a new branch' do - let(:service_result) { { status: :success, branch: double(name: branch) } } + let(:service_result) { { status: :success, branch: instance_double(Gitlab::Git::Branch, name: branch) } } it 'returns a new branch' do expect(mutated_branch.name).to eq(branch) - expect(subject[:errors]).to be_empty + expect(resolve[:errors]).to be_empty end end @@ -43,7 +45,7 @@ let(:service_result) { { status: :error, message: 'Branch already exists' } } it { expect(mutated_branch).to be_nil } - it { expect(subject[:errors]).to eq(['Branch already exists']) } + it { expect(resolve[:errors]).to eq(['Branch already exists']) } end end end diff --git a/spec/requests/api/graphql/mutations/branches/create_spec.rb b/spec/requests/api/graphql/mutations/repositories/branches/create_spec.rb similarity index 96% rename from spec/requests/api/graphql/mutations/branches/create_spec.rb rename to spec/requests/api/graphql/mutations/repositories/branches/create_spec.rb index 32512e2ee1b039da5d740c1b535ac1ce297cad6b..70703a75fe16e7898eb38c9ff2667f75c4492c7e 100644 --- a/spec/requests/api/graphql/mutations/branches/create_spec.rb +++ b/spec/requests/api/graphql/mutations/repositories/branches/create_spec.rb @@ -36,7 +36,7 @@ context 'when user is a direct project member' do context 'and user is a developer' do - before do + before_all do project.add_developer(current_user) end @@ -57,7 +57,7 @@ let_it_be(:project) { create(:project, :public, :empty_repo, :repository_private, group: group) } context 'and user is a guest' do - before do + before_all do group.add_guest(current_user) end @@ -65,7 +65,7 @@ end context 'and user is a developer' do - before do + before_all do group.add_developer(current_user) end @@ -80,7 +80,7 @@ context 'when user is an inherited member from the group' do context 'and user is a guest' do - before do + before_all do group.add_guest(current_user) end @@ -88,7 +88,7 @@ end context 'and user is a developer' do - before do + before_all do group.add_developer(current_user) end diff --git a/spec/requests/api/graphql/mutations/branches/delete_spec.rb b/spec/requests/api/graphql/mutations/repositories/branches/delete_spec.rb similarity index 100% rename from spec/requests/api/graphql/mutations/branches/delete_spec.rb rename to spec/requests/api/graphql/mutations/repositories/branches/delete_spec.rb