diff options
author | Sean McGivern <sean@gitlab.com> | 2019-02-19 11:30:07 +0000 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2019-02-19 11:30:07 +0000 |
commit | 08e2104e0445877a056468d4675aa72af5ebb2ae (patch) | |
tree | 3d6abe94e6adc52843d9584142adab87af12a9a0 | |
parent | 197641c7d7278a7a8c17e318b08dec876636110a (diff) | |
parent | 42356201703b806219c86c04c2711ad9f52ce052 (diff) | |
download | gitlab-ce-08e2104e0445877a056468d4675aa72af5ebb2ae.tar.gz |
Merge branch 'add-related-merge-request-count-to-api-response' into 'master'
Add related merge request count to api response
See merge request gitlab-org/gitlab-ce!24974
-rw-r--r-- | changelogs/unreleased/add-related-merge-request-count-to-api-response.yml | 5 | ||||
-rw-r--r-- | doc/api/issues.md | 10 | ||||
-rw-r--r-- | lib/api/entities.rb | 9 | ||||
-rw-r--r-- | spec/requests/api/issues_spec.rb | 18 |
4 files changed, 42 insertions, 0 deletions
diff --git a/changelogs/unreleased/add-related-merge-request-count-to-api-response.yml b/changelogs/unreleased/add-related-merge-request-count-to-api-response.yml new file mode 100644 index 00000000000..7438053a84f --- /dev/null +++ b/changelogs/unreleased/add-related-merge-request-count-to-api-response.yml @@ -0,0 +1,5 @@ +--- +title: Add related merge request count to api response +merge_request: 24974 +author: +type: added diff --git a/doc/api/issues.md b/doc/api/issues.md index dd328fb8079..0571f280d2a 100644 --- a/doc/api/issues.md +++ b/doc/api/issues.md @@ -110,6 +110,7 @@ Example response: "labels" : [], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "user_notes_count": 1, "due_date": "2016-07-22", "web_url": "http://example.com/example/example/issues/6", @@ -219,6 +220,7 @@ Example response: "labels" : [], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "id" : 41, "title" : "Ut commodi ullam eos dolores perferendis nihil sunt.", "updated_at" : "2016-01-04T15:31:46.176Z", @@ -334,6 +336,7 @@ Example response: "labels" : [], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "id" : 41, "title" : "Ut commodi ullam eos dolores perferendis nihil sunt.", "updated_at" : "2016-01-04T15:31:46.176Z", @@ -430,6 +433,7 @@ Example response: "labels" : [], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "id" : 41, "title" : "Ut commodi ullam eos dolores perferendis nihil sunt.", "updated_at" : "2016-01-04T15:31:46.176Z", @@ -505,6 +509,7 @@ Example response: ], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "author" : { "name" : "Alexandra Bashirian", "avatar_url" : null, @@ -604,6 +609,7 @@ Example response: ], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "id" : 85, "assignees" : [], "assignee" : null, @@ -690,6 +696,7 @@ Example response: "labels": [], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "milestone": null, "assignees": [{ "name": "Miss Monserrate Beier", @@ -774,6 +781,7 @@ Example response: "labels": [], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "milestone": null, "assignees": [{ "name": "Miss Monserrate Beier", @@ -856,6 +864,7 @@ Example response: "labels": [], "upvotes": 4, "downvotes": 0, + "merge_requests_count": 0, "milestone": null, "assignee": { "name": "Edwardo Grady", @@ -973,6 +982,7 @@ Example response: "user_notes_count": 7, "upvotes": 0, "downvotes": 0, + "merge_requests_count": 0, "due_date": null, "web_url": "http://example.com/example/example/issues/110", "confidential": false, diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 27da2c2e5ed..9199f898ea0 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -557,6 +557,15 @@ module API expose :time_stats, using: 'API::Entities::IssuableTimeStats' do |issue| issue end + + expose :merge_requests_count do |issue, options| + if options[:issuable_metadata] + # Avoids an N+1 query when metadata is included + options[:issuable_metadata][issue.id].merge_requests_count + else + issue.merge_requests_closing_issues.count + end + end end class Issue < IssueBasic diff --git a/spec/requests/api/issues_spec.rb b/spec/requests/api/issues_spec.rb index 033cd1b2b38..b5cc13eff29 100644 --- a/spec/requests/api/issues_spec.rb +++ b/spec/requests/api/issues_spec.rb @@ -421,6 +421,24 @@ describe API::Issues do expect(response).to have_gitlab_http_status(200) expect(response).to match_response_schema('public_api/v4/issues') end + + it 'returns a related merge request count of 0 if there are no related merge requests' do + get api('/issues', user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to match_response_schema('public_api/v4/issues') + expect(json_response.first).to include('merge_requests_count' => 0) + end + + it 'returns a related merge request count > 0 if there are related merge requests' do + create(:merge_requests_closing_issues, issue: issue) + + get api('/issues', user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to match_response_schema('public_api/v4/issues') + expect(json_response.first).to include('merge_requests_count' => 1) + end end end |