diff options
author | 🙈 jacopo beschi 🙉 <intrip@gmail.com> | 2018-09-05 16:07:02 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-09-05 16:07:02 +0300 |
commit | 20bd1e6b34417a2b9f688071e2891a64d1653770 (patch) | |
tree | 88c9bb554659f57e4ffb23d657aa69d6ceea534e | |
parent | cff47b2045999ba5ab1af7cf8c0aa45b2c52fb36 (diff) |
Resolve "Return how many commits the source branch is behind the target branch through the API"
-rw-r--r-- | changelogs/unreleased/36534-show-commit-behind-mr-api.yml | 5 | ||||
-rw-r--r-- | doc/api/merge_requests.md | 4 | ||||
-rw-r--r-- | lib/api/entities.rb | 2 | ||||
-rw-r--r-- | lib/api/merge_requests.rb | 3 | ||||
-rw-r--r-- | spec/requests/api/merge_requests_spec.rb | 9 |
5 files changed, 21 insertions, 2 deletions
diff --git a/changelogs/unreleased/36534-show-commit-behind-mr-api.yml b/changelogs/unreleased/36534-show-commit-behind-mr-api.yml new file mode 100644 index 00000000000..06471146fa3 --- /dev/null +++ b/changelogs/unreleased/36534-show-commit-behind-mr-api.yml @@ -0,0 +1,5 @@ +--- +title: Adds diverged_commits_count field to GET api/v4/projects/:project_id/merge_requests/:merge_request_iid +merge_request: 21405 +author: Jacopo Beschi @jacopo-beschi +type: added diff --git a/doc/api/merge_requests.md b/doc/api/merge_requests.md index 58d05a70d05..9e6676d62fe 100644 --- a/doc/api/merge_requests.md +++ b/doc/api/merge_requests.md @@ -352,6 +352,7 @@ Parameters: - `id` (required) - The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user - `merge_request_iid` (required) - The internal ID of the merge request - `render_html` (optional) - If `true` response includes rendered HTML for title and description +- `include_diverged_commits_count` (optional) - If `true` response includes the commits behind the target branch ```json { @@ -435,7 +436,8 @@ Parameters: "username" : "root", "id" : 1, "name" : "Administrator" - } + }, + "diverged_commits_count": 2 } ``` diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 624eda3f5dd..e63f11d721e 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -687,6 +687,8 @@ module API expose :diff_refs, using: Entities::DiffRefs + expose :diverged_commits_count, as: :diverged_commits_count, if: -> (_, options) { options[:include_diverged_commits_count] } + def build_available?(options) options[:project]&.feature_available?(:builds, options[:current_user]) end diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index abad418771c..55f54fe3c43 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -233,6 +233,7 @@ module API params do requires :merge_request_iid, type: Integer, desc: 'The IID of a merge request' optional :render_html, type: Boolean, desc: 'Returns the description and title rendered HTML' + optional :include_diverged_commits_count, type: Boolean, desc: 'Returns the commits count behind the target branch' end desc 'Get a single merge request' do success Entities::MergeRequest @@ -240,7 +241,7 @@ module API get ':id/merge_requests/:merge_request_iid' do merge_request = find_merge_request_with_access(params[:merge_request_iid]) - present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project, render_html: params[:render_html] + present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project, render_html: params[:render_html], include_diverged_commits_count: params[:include_diverged_commits_count] end desc 'Get the participants of a merge request' do diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb index 4de834bf93a..e987eee6e91 100644 --- a/spec/requests/api/merge_requests_spec.rb +++ b/spec/requests/api/merge_requests_spec.rb @@ -353,6 +353,15 @@ describe API::MergeRequests do end end + it 'returns the commits behind the target branch when include_diverged_commits_count is present' do + allow_any_instance_of(merge_request.class).to receive(:diverged_commits_count).and_return(1) + + get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}", user), include_diverged_commits_count: true + + expect(response).to have_gitlab_http_status(200) + expect(json_response['diverged_commits_count']).to eq(1) + end + it "returns a 404 error if merge_request_iid not found" do get api("/projects/#{project.id}/merge_requests/999", user) expect(response).to have_gitlab_http_status(404) |