Commit 7fc870f8 authored by Alex Kalderimis's avatar Alex Kalderimis

Add state events to merge request update mutation

This adds the ability to open and close MRs from GraphQL
parent 30741c7a
...@@ -19,9 +19,14 @@ module Mutations ...@@ -19,9 +19,14 @@ module Mutations
required: false, required: false,
description: copy_field_description(Types::MergeRequestType, :description) description: copy_field_description(Types::MergeRequestType, :description)
def resolve(args) argument :state, ::Types::MergeRequestStateEventEnum,
merge_request = authorized_find!(**args.slice(:project_path, :iid)) required: false,
attributes = args.slice(:title, :description, :target_branch).compact as: :state_event,
description: 'The action to perform to change the state.'
def resolve(project_path:, iid:, **args)
merge_request = authorized_find!(project_path: project_path, iid: iid)
attributes = args.compact
::MergeRequests::UpdateService ::MergeRequests::UpdateService
.new(merge_request.project, current_user, attributes) .new(merge_request.project, current_user, attributes)
......
# frozen_string_literal: true
module Types
class MergeRequestStateEventEnum < BaseEnum
graphql_name 'MergeRequestNewState'
description 'New state to apply to a merge request.'
value 'OPEN',
value: 'reopen',
description: 'Open the merge request if it is closed.'
value 'CLOSED',
value: 'close',
description: 'Close the merge request if it is open.'
end
end
...@@ -921,6 +921,10 @@ class MergeRequest < ApplicationRecord ...@@ -921,6 +921,10 @@ class MergeRequest < ApplicationRecord
closed? && !source_project_missing? && source_branch_exists? closed? && !source_project_missing? && source_branch_exists?
end end
def can_be_closed?
opened?
end
def ensure_merge_request_diff def ensure_merge_request_diff
merge_request_diff.persisted? || create_merge_request_diff merge_request_diff.persisted? || create_merge_request_diff
end end
......
---
title: Add state events to merge request update mutation
merge_request: 54133
author:
type: added
...@@ -15841,6 +15841,21 @@ Identifier of MergeRequest. ...@@ -15841,6 +15841,21 @@ Identifier of MergeRequest.
""" """
scalar MergeRequestID scalar MergeRequestID
"""
New state to apply to a merge request.
"""
enum MergeRequestNewState {
"""
Close the merge request if it is open.
"""
CLOSED
"""
Open the merge request if it is closed.
"""
OPEN
}
""" """
Check permissions for the current user on a merge request Check permissions for the current user on a merge request
""" """
...@@ -16340,6 +16355,11 @@ input MergeRequestUpdateInput { ...@@ -16340,6 +16355,11 @@ input MergeRequestUpdateInput {
""" """
projectPath: ID! projectPath: ID!
"""
The action to perform to change the state.
"""
state: MergeRequestNewState
""" """
Target branch of the merge request. Target branch of the merge request.
""" """
......
...@@ -43339,6 +43339,29 @@ ...@@ -43339,6 +43339,29 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "ENUM",
"name": "MergeRequestNewState",
"description": "New state to apply to a merge request.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "OPEN",
"description": "Open the merge request if it is closed.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "CLOSED",
"description": "Close the merge request if it is open.",
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "MergeRequestPermissions", "name": "MergeRequestPermissions",
...@@ -44668,6 +44691,16 @@ ...@@ -44668,6 +44691,16 @@
}, },
"defaultValue": null "defaultValue": null
}, },
{
"name": "state",
"description": "The action to perform to change the state.",
"type": {
"kind": "ENUM",
"name": "MergeRequestNewState",
"ofType": null
},
"defaultValue": null
},
{ {
"name": "clientMutationId", "name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.", "description": "A unique identifier for the client performing the mutation.",
...@@ -5111,6 +5111,15 @@ Possible identifier types for a measurement. ...@@ -5111,6 +5111,15 @@ Possible identifier types for a measurement.
| `PROJECTS` | Project count | | `PROJECTS` | Project count |
| `USERS` | User count | | `USERS` | User count |
### MergeRequestNewState
New state to apply to a merge request..
| Value | Description |
| ----- | ----------- |
| `CLOSED` | Close the merge request if it is open. |
| `OPEN` | Open the merge request if it is closed. |
### MergeRequestSort ### MergeRequestSort
Values for sorting merge requests. Values for sorting merge requests.
......
...@@ -12,10 +12,11 @@ RSpec.describe Mutations::MergeRequests::Update do ...@@ -12,10 +12,11 @@ RSpec.describe Mutations::MergeRequests::Update do
describe '#resolve' do describe '#resolve' do
let(:attributes) { { title: 'new title', description: 'new description', target_branch: 'new-branch' } } let(:attributes) { { title: 'new title', description: 'new description', target_branch: 'new-branch' } }
let(:arguments) { attributes }
let(:mutated_merge_request) { subject[:merge_request] } let(:mutated_merge_request) { subject[:merge_request] }
subject do subject do
mutation.resolve(project_path: merge_request.project.full_path, iid: merge_request.iid, **attributes) mutation.resolve(project_path: merge_request.project.full_path, iid: merge_request.iid, **arguments)
end end
it_behaves_like 'permission level for merge request mutation is correctly verified' it_behaves_like 'permission level for merge request mutation is correctly verified'
...@@ -61,6 +62,24 @@ RSpec.describe Mutations::MergeRequests::Update do ...@@ -61,6 +62,24 @@ RSpec.describe Mutations::MergeRequests::Update do
expect(mutated_merge_request).to have_attributes(attributes) expect(mutated_merge_request).to have_attributes(attributes)
end end
end end
context 'when closing the MR' do
let(:arguments) { { state_event: ::Types::MergeRequestStateEventEnum.values['CLOSED'].value } }
it 'closes the MR' do
expect(mutated_merge_request).to be_closed
end
end
context 'when re-opening the MR' do
let(:arguments) { { state_event: ::Types::MergeRequestStateEventEnum.values['OPEN'].value } }
it 'closes the MR' do
merge_request.close!
expect(mutated_merge_request).to be_open
end
end
end end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GitlabSchema.types['MergeRequestNewState'] do
it 'has the appropriate values' do
expect(described_class.values).to contain_exactly(
['OPEN', have_attributes(value: 'reopen')],
['CLOSED', have_attributes(value: 'close')]
)
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