summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@gitlab.com>2017-11-03 13:49:37 +0000
committerSean McGivern <sean@gitlab.com>2017-11-03 14:28:18 +0000
commit9d259c2232a7a6bab344b961ef8dc9561c380c29 (patch)
treec47b47d24eb4f3a378c99824b35f3a4d20c3ceb7
parent8c01f3110c8d8601fc16b17850dfa778f1f3d877 (diff)
downloadgitlab-ce-add-changes-count-to-merge-requests-api.tar.gz
Add changes_count to the merge requests APIadd-changes-count-to-merge-requests-api
This returns the `real_size` of the MR's diff, which is a string indicating the number of changes. If the diff overflows after _n_ files, the string will be `$n+`.
-rw-r--r--changelogs/unreleased/add-changes-count-to-merge-requests-api.yml5
-rw-r--r--doc/api/merge_requests.md22
-rw-r--r--lib/api/entities.rb4
-rw-r--r--spec/fixtures/api/schemas/public_api/v4/merge_requests.json1
-rw-r--r--spec/requests/api/merge_requests_spec.rb34
5 files changed, 53 insertions, 13 deletions
diff --git a/changelogs/unreleased/add-changes-count-to-merge-requests-api.yml b/changelogs/unreleased/add-changes-count-to-merge-requests-api.yml
new file mode 100644
index 00000000000..d0a00fafb52
--- /dev/null
+++ b/changelogs/unreleased/add-changes-count-to-merge-requests-api.yml
@@ -0,0 +1,5 @@
+---
+title: Add a count of changes to the merge requests API
+merge_request:
+author:
+type: changed
diff --git a/doc/api/merge_requests.md b/doc/api/merge_requests.md
index 50a971102fb..6de460f2778 100644
--- a/doc/api/merge_requests.md
+++ b/doc/api/merge_requests.md
@@ -15,6 +15,11 @@ given state (`opened`, `closed`, or `merged`) or all of them (`all`).
The pagination parameters `page` and `per_page` can be used to
restrict the list of merge requests.
+**Note**: the `changes_count` value in the response is a string, not an
+integer. This is because when an MR has too many changes to display and store,
+it will be capped at 1,000. In that case, the API will return the string
+`"1000+"` for the changes count.
+
```
GET /merge_requests
GET /merge_requests?state=opened
@@ -92,6 +97,7 @@ Parameters:
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": null,
"user_notes_count": 1,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -130,6 +136,11 @@ will be the same. In the case of a merge request from a fork,
`target_project_id` and `project_id` will be the same and
`source_project_id` will be the fork project's ID.
+**Note**: the `changes_count` value in the response is a string, not an
+integer. This is because when an MR has too many changes to display and store,
+it will be capped at 1,000. In that case, the API will return the string
+`"1000+"` for the changes count.
+
Parameters:
| Attribute | Type | Required | Description |
@@ -198,6 +209,7 @@ Parameters:
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": null,
"user_notes_count": 1,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -274,6 +286,7 @@ Parameters:
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": "9999999999999999999999999999999999999999",
"user_notes_count": 1,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -386,6 +399,7 @@ Parameters:
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": null,
"user_notes_count": 1,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -480,6 +494,7 @@ POST /projects/:id/merge_requests
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": null,
"user_notes_count": 0,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -565,6 +580,7 @@ Must include at least one non-required attribute from above.
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": null,
"user_notes_count": 1,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -670,6 +686,7 @@ Parameters:
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": "9999999999999999999999999999999999999999",
"user_notes_count": 1,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -747,6 +764,7 @@ Parameters:
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": null,
"user_notes_count": 1,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1",
@@ -822,7 +840,8 @@ Example response when the GitLab issue tracker is used:
"created_at" : "2016-01-04T15:31:51.081Z",
"iid" : 6,
"labels" : [],
- "user_notes_count": 1
+ "user_notes_count": 1,
+ "changes_count": "1"
},
]
```
@@ -1077,6 +1096,7 @@ Example response:
"sha": "8888888888888888888888888888888888888888",
"merge_commit_sha": null,
"user_notes_count": 7,
+ "changes_count": "1",
"should_remove_source_branch": true,
"force_remove_source_branch": false,
"web_url": "http://example.com/example/example/merge_requests/1"
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 67cecb6a7ad..aeb6001cf46 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -478,6 +478,10 @@ module API
expose :subscribed do |merge_request, options|
merge_request.subscribed?(options[:current_user], options[:project])
end
+
+ expose :changes_count do |merge_request, _options|
+ merge_request.merge_request_diff.real_size
+ end
end
class MergeRequestChanges < MergeRequest
diff --git a/spec/fixtures/api/schemas/public_api/v4/merge_requests.json b/spec/fixtures/api/schemas/public_api/v4/merge_requests.json
index 5828be5255b..034509091a5 100644
--- a/spec/fixtures/api/schemas/public_api/v4/merge_requests.json
+++ b/spec/fixtures/api/schemas/public_api/v4/merge_requests.json
@@ -70,6 +70,7 @@
"sha": { "type": "string" },
"merge_commit_sha": { "type": ["string", "null"] },
"user_notes_count": { "type": "integer" },
+ "changes_count": { "type": "string" },
"should_remove_source_branch": { "type": ["boolean", "null"] },
"force_remove_source_branch": { "type": ["boolean", "null"] },
"discussion_locked": { "type": ["boolean", "null"] },
diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb
index 024cfe8b372..4bad14b4e75 100644
--- a/spec/requests/api/merge_requests_spec.rb
+++ b/spec/requests/api/merge_requests_spec.rb
@@ -435,17 +435,7 @@ describe API::MergeRequests do
expect(json_response['merge_status']).to eq('can_be_merged')
expect(json_response['should_close_merge_request']).to be_falsy
expect(json_response['force_close_merge_request']).to be_falsy
- end
-
- it "returns merge_request" do
- get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user)
- expect(response).to have_gitlab_http_status(200)
- expect(json_response['title']).to eq(merge_request.title)
- expect(json_response['iid']).to eq(merge_request.iid)
- expect(json_response['work_in_progress']).to eq(false)
- expect(json_response['merge_status']).to eq('can_be_merged')
- expect(json_response['should_close_merge_request']).to be_falsy
- expect(json_response['force_close_merge_request']).to be_falsy
+ expect(json_response['changes_count']).to eq(merge_request.merge_request_diff.real_size)
end
it "returns a 404 error if merge_request_iid not found" do
@@ -462,12 +452,32 @@ describe API::MergeRequests do
context 'Work in Progress' do
let!(:merge_request_wip) { create(:merge_request, author: user, assignee: user, source_project: project, target_project: project, title: "WIP: Test", created_at: base_time + 1.second) }
- it "returns merge_request" do
+ it "returns merge request" do
get api("/projects/#{project.id}/merge_requests/#{merge_request_wip.iid}", user)
+
expect(response).to have_gitlab_http_status(200)
expect(json_response['work_in_progress']).to eq(true)
end
end
+
+ context 'when a merge request has more than the changes limit' do
+ it "returns a string indicating that more changes were made" do
+ stub_const('Commit::DIFF_HARD_LIMIT_FILES', 5)
+
+ merge_request_overflow = create(:merge_request, :simple,
+ author: user,
+ assignee: user,
+ source_project: project,
+ source_branch: 'expand-collapse-files',
+ target_project: project,
+ target_branch: 'master')
+
+ get api("/projects/#{project.id}/merge_requests/#{merge_request_overflow.iid}", user)
+
+ expect(response).to have_gitlab_http_status(200)
+ expect(json_response['changes_count']).to eq('5+')
+ end
+ end
end
describe 'GET /projects/:id/merge_requests/:merge_request_iid/commits' do