From cbd6ea32b752394f672b9cde35c7d97889e83dd7 Mon Sep 17 00:00:00 2001 From: Guillamue Micouin-Jorda Date: Thu, 24 Apr 2025 17:13:03 +0200 Subject: [PATCH 1/3] feat: Add capability to filter mergerequests of commit by state Signed-off-by: guillaume.micouin-jorda --- doc/api/commits.md | 11 ++++++----- doc/api/openapi/openapi_v2.yaml | 10 ++++++++-- lib/api/commits.rb | 4 +++- spec/requests/api/commits_spec.rb | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/doc/api/commits.md b/doc/api/commits.md index 6aa2357793ff4a..6a8120244c63ad 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 320b48de45d94c..1fe3987ac48500 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 @@ -52291,12 +52297,12 @@ definitions: line_range: start: line_code: 1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_2_4 - type: + type: old_line: 2 new_line: 4 end: line_code: 1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_2_4 - type: + type: old_line: 2 new_line: 4 description: API_Entities_DraftNote model diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 478acfddfeed24..899dceca861b30 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 7e7033e4e5f30e..b38390ff0c105e 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 -- GitLab From f72e95f27e9ae003d2983c325ebc6af77610c20b Mon Sep 17 00:00:00 2001 From: Amy Qualls Date: Mon, 19 May 2025 09:05:56 -0700 Subject: [PATCH 2/3] Updates openAPI YAML file with changes Catch the YAML file up with the latest changes from this MR. --- doc/api/openapi/openapi_v2.yaml | 2 +- lib/api/commits.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/openapi/openapi_v2.yaml b/doc/api/openapi/openapi_v2.yaml index 1fe3987ac48500..70d15a18e68217 100644 --- a/doc/api/openapi/openapi_v2.yaml +++ b/doc/api/openapi/openapi_v2.yaml @@ -13525,7 +13525,7 @@ paths: required: true - in: query name: state - description: Filter merge-requests by state. + description: Filter merge-requests by state type: string required: false example: merged diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 899dceca861b30..0a87c074d23fb5 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -551,7 +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' } + 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 -- GitLab From 535f341aa64f50f26ffd7eff91f6999b97e56bbf Mon Sep 17 00:00:00 2001 From: Marcel Amirault Date: Wed, 4 Jun 2025 23:25:27 +0900 Subject: [PATCH 3/3] Remove extra spaces --- doc/api/openapi/openapi_v2.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/openapi/openapi_v2.yaml b/doc/api/openapi/openapi_v2.yaml index 70d15a18e68217..4233ca85a220b9 100644 --- a/doc/api/openapi/openapi_v2.yaml +++ b/doc/api/openapi/openapi_v2.yaml @@ -52297,12 +52297,12 @@ definitions: line_range: start: line_code: 1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_2_4 - type: + type: old_line: 2 new_line: 4 end: line_code: 1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_2_4 - type: + type: old_line: 2 new_line: 4 description: API_Entities_DraftNote model -- GitLab