diff --git a/doc/api/commits.md b/doc/api/commits.md index 6aa2357793ff4a079f5faad84aebf3a6fffde51e..6a8120244c63addaa690c89bda073bbe219a3441 100644 --- a/doc/api/commits.md +++ b/doc/api/commits.md @@ -876,14 +876,15 @@ Returns information about the merge request that originally introduced a specifi GET /projects/:id/repository/commits/:sha/merge_requests ``` -| Attribute | Type | Required | Description | -| --------- | ---- | -------- | ----------- | -| `id` | integer/string | yes | The ID or [URL-encoded path of the project](rest/_index.md#namespaced-paths) | -| `sha` | string | yes | The commit SHA | +| Attribute | Type | Required | Description | +|-----------|----------------|----------|-------------| +| `id` | integer/string | Yes | The ID or [URL-encoded path of the project](rest/_index.md#namespaced-paths) | +| `sha` | string | Yes | The commit SHA | +| `state` | string | No | Returns merge requests with the specified state: `opened`, `closed`, `locked`, or `merged`. Omit this parameter to get all merge requests regardless of state. | ```shell curl --header "PRIVATE-TOKEN: " \ - --url "https://gitlab.example.com/api/v4/projects/5/repository/commits/af5b13261899fb2c0db30abdd0af8b07cb44fdc5/merge_requests" + --url "https://gitlab.example.com/api/v4/projects/5/repository/commits/af5b13261899fb2c0db30abdd0af8b07cb44fdc5/merge_requests?state=opened" ``` Example response: diff --git a/doc/api/openapi/openapi_v2.yaml b/doc/api/openapi/openapi_v2.yaml index 320b48de45d94c38a4b7f91ae8e2667f8a1bf0e8..4233ca85a220b9c3480b45098767cd370ed7ade6 100644 --- a/doc/api/openapi/openapi_v2.yaml +++ b/doc/api/openapi/openapi_v2.yaml @@ -13523,6 +13523,12 @@ paths: Merge Requests type: string required: true + - in: query + name: state + description: Filter merge-requests by state + type: string + required: false + example: merged - in: query name: page description: Current page number diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 478acfddfeed24d08a88c2e554dd71790259fcb7..0a87c074d23fb5acc354f459ef9e7080ae5ae70b 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -551,6 +551,7 @@ def track_commit_events end params do requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag on which to find Merge Requests' + optional :state, type: String, desc: 'Filter merge-requests by state', documentation: { example: 'merged' } use :pagination end get ':id/repository/commits/:sha/merge_requests', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS, urgency: :low do @@ -562,7 +563,8 @@ def track_commit_events commit_merge_requests = MergeRequestsFinder.new( current_user, project_id: user_project.id, - commit_sha: commit.sha + commit_sha: commit.sha, + state: params[:state] ).execute.with_api_entity_associations present paginate(commit_merge_requests), with: Entities::MergeRequestBasic diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb index 7e7033e4e5f30ed878e76ad48a9bbba6e971b8a4..b38390ff0c105e599c70842b2280376f382db8ee 100644 --- a/spec/requests/api/commits_spec.rb +++ b/spec/requests/api/commits_spec.rb @@ -2546,6 +2546,25 @@ def perform_request(user) expect { perform_request(user) }.not_to exceed_query_limit(control) end + + context 'merge-requests filtered by state' do + it 'returns the correct opened merge-request' do + get api("/projects/#{project.id}/repository/commits/#{commit.id}/merge_requests?state=opened", user) + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to include_limited_pagination_headers + expect(json_response.length).to eq(1) + expect(json_response[0]['id']).to eq(merged_mr.id) + end + + it 'does not found closed merge-request' do + get api("/projects/#{project.id}/repository/commits/#{commit.id}/merge_requests?state=closed", user) + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to include_limited_pagination_headers + expect(json_response.length).to eq(0) + end + end end describe 'GET /projects/:id/repository/commits/:sha/signature' do