Resolve "Return how many commits the source branch is behind the target branch through the API"
This commit is contained in:
		
							parent
							
								
									cff47b2045
								
							
						
					
					
						commit
						20bd1e6b34
					
				|  | @ -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 | ||||||
|  | @ -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 | - `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 | - `merge_request_iid` (required) - The internal ID of the merge request | ||||||
| - `render_html` (optional) - If `true` response includes rendered HTML for title and description | - `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 | ```json | ||||||
| { | { | ||||||
|  | @ -435,7 +436,8 @@ Parameters: | ||||||
|     "username" : "root", |     "username" : "root", | ||||||
|     "id" : 1, |     "id" : 1, | ||||||
|     "name" : "Administrator" |     "name" : "Administrator" | ||||||
|   } |   }, | ||||||
|  |   "diverged_commits_count": 2 | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -687,6 +687,8 @@ module API | ||||||
| 
 | 
 | ||||||
|       expose :diff_refs, using: Entities::DiffRefs |       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) |       def build_available?(options) | ||||||
|         options[:project]&.feature_available?(:builds, options[:current_user]) |         options[:project]&.feature_available?(:builds, options[:current_user]) | ||||||
|       end |       end | ||||||
|  |  | ||||||
|  | @ -233,6 +233,7 @@ module API | ||||||
|       params do |       params do | ||||||
|         requires :merge_request_iid, type: Integer, desc: 'The IID of a merge request' |         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 :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 |       end | ||||||
|       desc 'Get a single merge request' do |       desc 'Get a single merge request' do | ||||||
|         success Entities::MergeRequest |         success Entities::MergeRequest | ||||||
|  | @ -240,7 +241,7 @@ module API | ||||||
|       get ':id/merge_requests/:merge_request_iid' do |       get ':id/merge_requests/:merge_request_iid' do | ||||||
|         merge_request = find_merge_request_with_access(params[:merge_request_iid]) |         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 |       end | ||||||
| 
 | 
 | ||||||
|       desc 'Get the participants of a merge request' do |       desc 'Get the participants of a merge request' do | ||||||
|  |  | ||||||
|  | @ -353,6 +353,15 @@ describe API::MergeRequests do | ||||||
|       end |       end | ||||||
|     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 |     it "returns a 404 error if merge_request_iid not found" do | ||||||
|       get api("/projects/#{project.id}/merge_requests/999", user) |       get api("/projects/#{project.id}/merge_requests/999", user) | ||||||
|       expect(response).to have_gitlab_http_status(404) |       expect(response).to have_gitlab_http_status(404) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue