Commit 94a0008c authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '215619_add_mutation_to_create_mr' into 'master'

Add mutation to create a merge request in GraphQL

See merge request gitlab-org/gitlab!31867
parents dab25026 3fbccb69
# frozen_string_literal: true
module Mutations
module MergeRequests
class Create < BaseMutation
include Mutations::ResolvesProject
graphql_name 'MergeRequestCreate'
argument :project_path, GraphQL::ID_TYPE,
required: true,
description: 'Project full path the merge request is associated with'
argument :title, GraphQL::STRING_TYPE,
required: true,
description: copy_field_description(Types::MergeRequestType, :title)
argument :source_branch, GraphQL::STRING_TYPE,
required: true,
description: copy_field_description(Types::MergeRequestType, :source_branch)
argument :target_branch, GraphQL::STRING_TYPE,
required: true,
description: copy_field_description(Types::MergeRequestType, :target_branch)
argument :description, GraphQL::STRING_TYPE,
required: false,
description: copy_field_description(Types::MergeRequestType, :description)
field :merge_request,
Types::MergeRequestType,
null: true,
description: 'The merge request after mutation'
authorize :create_merge_request_from
def resolve(project_path:, title:, source_branch:, target_branch:, description: nil)
project = authorized_find!(full_path: project_path)
attributes = {
title: title,
source_branch: source_branch,
target_branch: target_branch,
author_id: current_user.id,
description: description
}
merge_request = ::MergeRequests::CreateService.new(project, current_user, attributes).execute
{
merge_request: merge_request.valid? ? merge_request : nil,
errors: errors_on_object(merge_request)
}
end
private
def find_object(full_path:)
resolve_project(full_path: full_path)
end
end
end
end
......@@ -16,6 +16,7 @@ module Types
mount_mutation Mutations::Issues::SetConfidential
mount_mutation Mutations::Issues::SetDueDate
mount_mutation Mutations::Issues::Update
mount_mutation Mutations::MergeRequests::Create
mount_mutation Mutations::MergeRequests::SetLabels
mount_mutation Mutations::MergeRequests::SetLocked
mount_mutation Mutations::MergeRequests::SetMilestone
......
---
title: Add mutation to create a merge request in GraphQL
merge_request: 31867
author:
type: added
......@@ -6188,6 +6188,61 @@ type MergeRequestConnection {
pageInfo: PageInfo!
}
"""
Autogenerated input type of MergeRequestCreate
"""
input MergeRequestCreateInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
"""
Description of the merge request (Markdown rendered as HTML for caching)
"""
description: String
"""
Project full path the merge request is associated with
"""
projectPath: ID!
"""
Source branch of the merge request
"""
sourceBranch: String!
"""
Target branch of the merge request
"""
targetBranch: String!
"""
Title of the merge request
"""
title: String!
}
"""
Autogenerated return type of MergeRequestCreate
"""
type MergeRequestCreatePayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
"""
Errors encountered during execution of the mutation.
"""
errors: [String!]!
"""
The merge request after mutation
"""
mergeRequest: MergeRequest
}
"""
An edge in a connection.
"""
......@@ -6788,6 +6843,7 @@ type Mutation {
issueSetWeight(input: IssueSetWeightInput!): IssueSetWeightPayload
jiraImportStart(input: JiraImportStartInput!): JiraImportStartPayload
markAsSpamSnippet(input: MarkAsSpamSnippetInput!): MarkAsSpamSnippetPayload
mergeRequestCreate(input: MergeRequestCreateInput!): MergeRequestCreatePayload
mergeRequestSetAssignees(input: MergeRequestSetAssigneesInput!): MergeRequestSetAssigneesPayload
mergeRequestSetLabels(input: MergeRequestSetLabelsInput!): MergeRequestSetLabelsPayload
mergeRequestSetLocked(input: MergeRequestSetLockedInput!): MergeRequestSetLockedPayload
......
......@@ -17302,6 +17302,160 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "MergeRequestCreateInput",
"description": "Autogenerated input type of MergeRequestCreate",
"fields": null,
"inputFields": [
{
"name": "projectPath",
"description": "Project full path the merge request is associated with",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "title",
"description": "Title of the merge request",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "sourceBranch",
"description": "Source branch of the merge request",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "targetBranch",
"description": "Target branch of the merge request",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "description",
"description": "Description of the merge request (Markdown rendered as HTML for caching)",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
},
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "MergeRequestCreatePayload",
"description": "Autogenerated return type of MergeRequestCreate",
"fields": [
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "errors",
"description": "Errors encountered during execution of the mutation.",
"args": [
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "mergeRequest",
"description": "The merge request after mutation",
"args": [
],
"type": {
"kind": "OBJECT",
"name": "MergeRequest",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "MergeRequestEdge",
......@@ -19785,6 +19939,33 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "mergeRequestCreate",
"description": null,
"args": [
{
"name": "input",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "MergeRequestCreateInput",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "MergeRequestCreatePayload",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "mergeRequestSetAssignees",
"description": null,
......@@ -912,6 +912,16 @@ Autogenerated return type of MarkAsSpamSnippet
| `webUrl` | String | Web URL of the merge request |
| `workInProgress` | Boolean! | Indicates if the merge request is a work in progress (WIP) |
## MergeRequestCreatePayload
Autogenerated return type of MergeRequestCreate
| Name | Type | Description |
| --- | ---- | ---------- |
| `clientMutationId` | String | A unique identifier for the client performing the mutation. |
| `errors` | String! => Array | Errors encountered during execution of the mutation. |
| `mergeRequest` | MergeRequest | The merge request after mutation |
## MergeRequestPermissions
Check permissions for the current user on a merge request
......
# frozen_string_literal: true
require 'spec_helper'
describe Mutations::MergeRequests::Create do
subject(:mutation) { described_class.new(object: nil, context: context, field: nil) }
let_it_be(:project) { create(:project, :public, :repository) }
let_it_be(:user) { create(:user) }
let_it_be(:context) do
GraphQL::Query::Context.new(
query: OpenStruct.new(schema: nil),
values: { current_user: user },
object: nil
)
end
describe '#resolve' do
subject do
mutation.resolve(
project_path: project.full_path,
title: title,
source_branch: source_branch,
target_branch: target_branch,
description: description
)
end
let(:title) { 'MergeRequest' }
let(:source_branch) { 'feature' }
let(:target_branch) { 'master' }
let(:description) { nil }
let(:mutated_merge_request) { subject[:merge_request] }
it 'raises an error if the resource is not accessible to the user' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
context 'when user does not have enough permissions to create a merge request' do
before do
project.add_guest(user)
end
it 'raises an error if the resource is not accessible to the user' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
context 'when the user can create a merge request' do
before_all do
project.add_developer(user)
end
it 'creates a new merge request' do
expect { mutated_merge_request }.to change(MergeRequest, :count).by(1)
end
it 'returns a new merge request' do
expect(mutated_merge_request.title).to eq(title)
expect(subject[:errors]).to be_empty
end
context 'when optional description field is set' do
let(:description) { 'content' }
it 'returns a new merge request with a description' do
expect(mutated_merge_request.description).to eq(description)
expect(subject[:errors]).to be_empty
end
end
context 'when service cannot create a merge request' do
let(:title) { nil }
it 'does not create a new merge request' do
expect { mutated_merge_request }.not_to change(MergeRequest, :count)
end
it 'returns errors' do
expect(mutated_merge_request).to be_nil
expect(subject[:errors]).to eq(['Title can\'t be blank'])
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe 'Creation of a new merge request' do
include GraphqlHelpers
let_it_be(:current_user) { create(:user) }
let(:project) { create(:project, :public, :repository) }
let(:input) do
{
project_path: project.full_path,
title: title,
source_branch: source_branch,
target_branch: target_branch
}
end
let(:title) { 'MergeRequest' }
let(:source_branch) { 'new_branch' }
let(:target_branch) { 'master' }
let(:mutation) { graphql_mutation(:merge_request_create, input) }
let(:mutation_response) { graphql_mutation_response(:merge_request_create) }
context 'the user is not allowed to create a branch' do
it_behaves_like 'a mutation that returns top-level errors',
errors: ['The resource that you are attempting to access does not exist or you don\'t have permission to perform this action']
end
context 'when user has permissions to create a merge request' do
before do
project.add_developer(current_user)
end
it 'creates a new merge request' do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(mutation_response['mergeRequest']).to include(
'title' => title
)
end
context 'when source branch is equal to the target branch' do
let(:source_branch) { target_branch }
it_behaves_like 'a mutation that returns errors in the response',
errors: ['Branch conflict You can\'t use same project/branch for source and target']
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment