Commit 7a236f65 authored by Sean McGivern's avatar Sean McGivern

Add MR squashing to API

parent c51eb666
...@@ -75,6 +75,7 @@ Parameters: ...@@ -75,6 +75,7 @@ Parameters:
"approvals_before_merge": null "approvals_before_merge": null
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": false,
"web_url": "http://example.com/example/example/merge_requests/1" "web_url": "http://example.com/example/example/merge_requests/1"
} }
] ]
...@@ -145,6 +146,7 @@ Parameters: ...@@ -145,6 +146,7 @@ Parameters:
"approvals_before_merge": null "approvals_before_merge": null
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": false,
"web_url": "http://example.com/example/example/merge_requests/1" "web_url": "http://example.com/example/example/merge_requests/1"
} }
``` ```
...@@ -251,6 +253,7 @@ Parameters: ...@@ -251,6 +253,7 @@ Parameters:
"approvals_before_merge": null, "approvals_before_merge": null,
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": false,
"web_url": "http://example.com/example/example/merge_requests/1", "web_url": "http://example.com/example/example/merge_requests/1",
"changes": [ "changes": [
{ {
...@@ -287,6 +290,7 @@ POST /projects/:id/merge_requests ...@@ -287,6 +290,7 @@ POST /projects/:id/merge_requests
| `milestone_id` | integer | no | The ID of a milestone | | `milestone_id` | integer | no | The ID of a milestone |
| `remove_source_branch` | boolean | no | Flag indicating if a merge request should remove the source branch when merging | | `remove_source_branch` | boolean | no | Flag indicating if a merge request should remove the source branch when merging |
| `approvals_before_merge` | integer| no | Number of approvals required before this can be merged (see below) | | `approvals_before_merge` | integer| no | Number of approvals required before this can be merged (see below) |
| `squash` | boolean| no | Squash commits into a single commit when merging |
If `approvals_before_merge` is not provided, it inherits the value from the If `approvals_before_merge` is not provided, it inherits the value from the
target project. If it is provided, then the following conditions must hold in target project. If it is provided, then the following conditions must hold in
...@@ -349,6 +353,7 @@ order for it to take effect: ...@@ -349,6 +353,7 @@ order for it to take effect:
"approvals_before_merge": null "approvals_before_merge": null
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": false,
"web_url": "http://example.com/example/example/merge_requests/1" "web_url": "http://example.com/example/example/merge_requests/1"
} }
``` ```
...@@ -374,6 +379,7 @@ PUT /projects/:id/merge_requests/:merge_request_id ...@@ -374,6 +379,7 @@ PUT /projects/:id/merge_requests/:merge_request_id
| `labels` | string | no | Labels for MR as a comma-separated list | | `labels` | string | no | Labels for MR as a comma-separated list |
| `milestone_id` | integer | no | The ID of a milestone | | `milestone_id` | integer | no | The ID of a milestone |
| `remove_source_branch` | boolean | no | Flag indicating if a merge request should remove the source branch when merging | | `remove_source_branch` | boolean | no | Flag indicating if a merge request should remove the source branch when merging |
| `squash` | boolean| no | Squash commits into a single commit when merging |
```json ```json
{ {
...@@ -426,6 +432,7 @@ PUT /projects/:id/merge_requests/:merge_request_id ...@@ -426,6 +432,7 @@ PUT /projects/:id/merge_requests/:merge_request_id
"approvals_before_merge": null "approvals_before_merge": null
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": false,
"web_url": "http://example.com/example/example/merge_requests/1" "web_url": "http://example.com/example/example/merge_requests/1"
} }
``` ```
...@@ -471,7 +478,19 @@ Parameters: ...@@ -471,7 +478,19 @@ Parameters:
- `merge_commit_message` (optional) - Custom merge commit message - `merge_commit_message` (optional) - Custom merge commit message
- `should_remove_source_branch` (optional) - if `true` removes the source branch - `should_remove_source_branch` (optional) - if `true` removes the source branch
- `merge_when_build_succeeds` (optional) - if `true` the MR is merged when the build succeeds - `merge_when_build_succeeds` (optional) - if `true` the MR is merged when the build succeeds
- `sha` (optional) - if present, then this SHA must match the HEAD of the source branch, otherwise the merge will fail - `sha` (optional) - if present, then this SHA must
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
| `id` | string | yes | The ID of a project |
| `merge_request_id` | integer | yes | The ID of the merge request |
| `merge_commit_message` | string | no | Custom merge commit message |
| `should_remove_source_branch` | boolean | no | Remove the source branch after merge |
| `merge_when_build_succeeds` | boolean | no | Merge when build succeeds, rather than immediately |
| `sha` | string | no | If present, then this SHA must match the HEAD of the source branch, otherwise the merge will fail |
| `squash` | boolean | no | Squash the merge request into a single commit |
```json ```json
{ {
...@@ -525,6 +544,7 @@ Parameters: ...@@ -525,6 +544,7 @@ Parameters:
"approvals_before_merge": null "approvals_before_merge": null
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": false,
"web_url": "http://example.com/example/example/merge_requests/1" "web_url": "http://example.com/example/example/merge_requests/1"
} }
``` ```
...@@ -698,6 +718,7 @@ Parameters: ...@@ -698,6 +718,7 @@ Parameters:
"approvals_before_merge": null "approvals_before_merge": null
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": false,
"web_url": "http://example.com/example/example/merge_requests/1" "web_url": "http://example.com/example/example/merge_requests/1"
} }
``` ```
...@@ -1021,6 +1042,7 @@ Example response: ...@@ -1021,6 +1042,7 @@ Example response:
"user_notes_count": 7, "user_notes_count": 7,
"should_remove_source_branch": true, "should_remove_source_branch": true,
"force_remove_source_branch": false, "force_remove_source_branch": false,
"squash": true,
"web_url": "http://example.com/example/example/merge_requests/1" "web_url": "http://example.com/example/example/merge_requests/1"
}, },
"target_url": "https://gitlab.example.com/gitlab-org/gitlab-ci/merge_requests/7", "target_url": "https://gitlab.example.com/gitlab-org/gitlab-ci/merge_requests/7",
......
...@@ -326,6 +326,7 @@ module API ...@@ -326,6 +326,7 @@ module API
expose :approvals_before_merge expose :approvals_before_merge
expose :should_remove_source_branch?, as: :should_remove_source_branch expose :should_remove_source_branch?, as: :should_remove_source_branch
expose :force_remove_source_branch?, as: :force_remove_source_branch expose :force_remove_source_branch?, as: :force_remove_source_branch
expose :squash
expose :web_url do |merge_request, options| expose :web_url do |merge_request, options|
Gitlab::UrlBuilder.build(merge_request) Gitlab::UrlBuilder.build(merge_request)
......
...@@ -34,6 +34,7 @@ module API ...@@ -34,6 +34,7 @@ module API
optional :labels, type: String, desc: 'Comma-separated list of label names' optional :labels, type: String, desc: 'Comma-separated list of label names'
optional :approvals_before_merge, type: Integer, desc: 'Number of approvals required before this can be merged' optional :approvals_before_merge, type: Integer, desc: 'Number of approvals required before this can be merged'
optional :remove_source_branch, type: Boolean, desc: 'Remove source branch when merging' optional :remove_source_branch, type: Boolean, desc: 'Remove source branch when merging'
optional :squash, type: Boolean, desc: 'Squash commits when merging'
end end
end end
...@@ -153,7 +154,7 @@ module API ...@@ -153,7 +154,7 @@ module API
use :optional_params use :optional_params
at_least_one_of :title, :target_branch, :description, :assignee_id, at_least_one_of :title, :target_branch, :description, :assignee_id,
:milestone_id, :labels, :state_event, :approvals_before_merge, :milestone_id, :labels, :state_event, :approvals_before_merge,
:remove_source_branch :remove_source_branch, :squash
end end
put path do put path do
merge_request = find_merge_request_with_access(params.delete(:merge_request_id), :update_merge_request) merge_request = find_merge_request_with_access(params.delete(:merge_request_id), :update_merge_request)
...@@ -180,6 +181,7 @@ module API ...@@ -180,6 +181,7 @@ module API
optional :merge_when_build_succeeds, type: Boolean, optional :merge_when_build_succeeds, type: Boolean,
desc: 'When true, this merge request will be merged when the pipeline succeeds' desc: 'When true, this merge request will be merged when the pipeline succeeds'
optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch' optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch'
optional :squash, type: Boolean, desc: 'When true, the commits will be squashed into a single commit on merge'
end end
put "#{path}/merge" do put "#{path}/merge" do
merge_request = find_project_merge_request(params[:merge_request_id]) merge_request = find_project_merge_request(params[:merge_request_id])
...@@ -196,6 +198,10 @@ module API ...@@ -196,6 +198,10 @@ module API
render_api_error!("SHA does not match HEAD of source branch: #{merge_request.diff_head_sha}", 409) render_api_error!("SHA does not match HEAD of source branch: #{merge_request.diff_head_sha}", 409)
end end
if params[:squash]
merge_request.update(squash: params[:squash])
end
merge_params = { merge_params = {
commit_message: params[:merge_commit_message], commit_message: params[:merge_commit_message],
should_remove_source_branch: params[:should_remove_source_branch] should_remove_source_branch: params[:should_remove_source_branch]
......
...@@ -39,6 +39,7 @@ describe API::MergeRequests, api: true do ...@@ -39,6 +39,7 @@ describe API::MergeRequests, api: true do
expect(json_response.first['sha']).to eq(merge_request_merged.diff_head_sha) expect(json_response.first['sha']).to eq(merge_request_merged.diff_head_sha)
expect(json_response.first['merge_commit_sha']).not_to be_nil expect(json_response.first['merge_commit_sha']).not_to be_nil
expect(json_response.first['merge_commit_sha']).to eq(merge_request_merged.merge_commit_sha) expect(json_response.first['merge_commit_sha']).to eq(merge_request_merged.merge_commit_sha)
expect(json_response.first['squash']).to eq(merge_request_merged.squash)
end end
it "returns an array of all merge_requests" do it "returns an array of all merge_requests" do
...@@ -233,13 +234,15 @@ describe API::MergeRequests, api: true do ...@@ -233,13 +234,15 @@ describe API::MergeRequests, api: true do
author: user, author: user,
labels: 'label, label2', labels: 'label, label2',
milestone_id: milestone.id, milestone_id: milestone.id,
remove_source_branch: true remove_source_branch: true,
squash: true
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
expect(json_response['title']).to eq('Test merge_request') expect(json_response['title']).to eq('Test merge_request')
expect(json_response['labels']).to eq(['label', 'label2']) expect(json_response['labels']).to eq(['label', 'label2'])
expect(json_response['milestone']['id']).to eq(milestone.id) expect(json_response['milestone']['id']).to eq(milestone.id)
expect(json_response['force_remove_source_branch']).to be_truthy expect(json_response['force_remove_source_branch']).to be_truthy
expect(json_response['squash']).to be_truthy
end end
it "returns 422 when source_branch equals target_branch" do it "returns 422 when source_branch equals target_branch" do
...@@ -526,6 +529,14 @@ describe API::MergeRequests, api: true do ...@@ -526,6 +529,14 @@ describe API::MergeRequests, api: true do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it "updates the MR's squash attribute" do
expect do
put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), squash: true
end.to change { merge_request.reload.squash }
expect(response).to have_http_status(200)
end
it "enables merge when pipeline succeeds if the pipeline is active" do it "enables merge when pipeline succeeds if the pipeline is active" do
allow_any_instance_of(MergeRequest).to receive(:head_pipeline).and_return(pipeline) allow_any_instance_of(MergeRequest).to receive(:head_pipeline).and_return(pipeline)
allow(pipeline).to receive(:active?).and_return(true) allow(pipeline).to receive(:active?).and_return(true)
...@@ -566,6 +577,13 @@ describe API::MergeRequests, api: true do ...@@ -566,6 +577,13 @@ describe API::MergeRequests, api: true do
expect(json_response['milestone']['id']).to eq(milestone.id) expect(json_response['milestone']['id']).to eq(milestone.id)
end end
it "updates squash and returns merge_request" do
put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), squash: true
expect(response).to have_http_status(200)
expect(json_response['squash']).to be_truthy
end
it "returns merge_request with renamed target_branch" do it "returns merge_request with renamed target_branch" do
put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), target_branch: "wiki" put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), target_branch: "wiki"
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
......
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