diff --git a/changelogs/unreleased/api-refs-for-commit.yml b/changelogs/unreleased/api-refs-for-commit.yml index df8a2b0eccc..a5ed3992ef3 100644 --- a/changelogs/unreleased/api-refs-for-commit.yml +++ b/changelogs/unreleased/api-refs-for-commit.yml @@ -1,5 +1,5 @@ --- -title: 'API: Get references a commit is pushed to' +title: API: Get references a commit is pushed to merge_request: 15026 author: Robert Schilling type: added diff --git a/doc/api/commits.md b/doc/api/commits.md index 3c12906def4..18d31ffe24d 100644 --- a/doc/api/commits.md +++ b/doc/api/commits.md @@ -214,7 +214,7 @@ Parameters: | --------- | ---- | -------- | ----------- | | `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user | `sha` | string | yes | The commit hash | -| `type` | string | no | The scope of commits. Possible values `branches`, `tags`, `all`. Default is a `all`. | +| `type` | string | no | The scope of commits. Possible values `branches`, `tags`, `all`. Default is `all`. | ```bash curl --request POST --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --form "type=all" "https://gitlab.example.com/api/v4/projects/5/repository/commits/5937ac0a7beb003549fc5fd26fc247adbce4a52e/refs" @@ -224,11 +224,10 @@ Example response: ```json [ - {"name": "'test'"}, - {"name": "master"}, - {"name": "v1.1.0"} + {"branch_name": "'test'"}, + {"branch_name": "master"}, + {"tag_name": "v1.1.0"} ] - ``` ## Cherry pick a commit diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 6d845253c3a..afaf68114e8 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -171,12 +171,12 @@ module API refs = case params[:type] when 'branches' - user_project.repository.branch_names_contains(commit.id) + user_project.repository.branch_names_contains(commit.id).map {|branch_name| [branch_name, true]} when 'tags' - user_project.repository.tag_names_contains(commit.id) + user_project.repository.tag_names_contains(commit.id).map {|tag_name| [tag_name, false]} else - refs = user_project.repository.branch_names_contains(commit.id) - refs.concat(user_project.repository.tag_names_contains(commit.id)) + refs = user_project.repository.branch_names_contains(commit.id).map {|branch_name| [branch_name, true]} + refs.concat(user_project.repository.tag_names_contains(commit.id).map {|tag_name| [tag_name, false]}) end present refs, with: Entities::BasicRef diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 054c04bbd98..c7a817877f0 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -277,8 +277,12 @@ module API end class BasicRef < Grape::Entity - expose :name do |ref, options| - ref + expose :branch_name, if: lambda { |ref, options| ref[1] } do |ref, options| + ref[0] + end + + expose :tag_name, if: lambda { |ref, options| !ref[1] } do |ref, options| + ref[0] end end diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb index 964dd7ad39e..ac25f134697 100644 --- a/spec/requests/api/commits_spec.rb +++ b/spec/requests/api/commits_spec.rb @@ -492,35 +492,37 @@ describe API::Commits do it 'returns all refs with no scope' do get api(route, current_user) - repo_refs = project.repository.branch_names_contains(commit_id) - repo_refs.push(*project.repository.tag_names_contains(commit_id)) + branch_refs = project.repository.branch_names_contains(commit_id) + tag_refs = project.repository.tag_names_contains(commit_id) - expect(json_response.map { |refs| refs['name'] }).to eq(repo_refs) + expect(json_response.map { |refs| refs['branch_name'] }.compact).to eq(branch_refs) + expect(json_response.map { |refs| refs['tag_name'] }.compact).to eq(tag_refs) end it 'returns all refs' do get api(route, current_user), type: 'all' - repo_refs = project.repository.branch_names_contains(commit_id) - repo_refs.push(*project.repository.tag_names_contains(commit_id)) + branch_refs = project.repository.branch_names_contains(commit_id) + tag_refs = project.repository.tag_names_contains(commit_id) - expect(json_response.map { |refs| refs['name'] }).to eq(repo_refs) + expect(json_response.map { |refs| refs['branch_name'] }.compact).to eq(branch_refs) + expect(json_response.map { |refs| refs['tag_name'] }.compact).to eq(tag_refs) end it 'returns the branch refs' do get api(route, current_user), type: 'branches' - repo_refs = project.repository.branch_names_contains(commit_id) + branch_refs = project.repository.branch_names_contains(commit_id) - expect(json_response.map { |refs| refs['name'] }).to eq(repo_refs) + expect(json_response.map { |refs| refs['branch_name'] }.compact).to eq(branch_refs) end it 'returns the tag refs' do get api(route, current_user), type: 'tags' - repo_refs = project.repository.tag_names_contains(commit_id) + tag_refs = project.repository.tag_names_contains(commit_id) - expect(json_response.map { |refs| refs['name'] }).to eq(repo_refs) + expect(json_response.map { |refs| refs['tag_name'] }.compact).to eq(tag_refs) end end end