Separate branch and tag names

This commit is contained in:
Robert Schilling 2018-02-09 17:46:41 +01:00
parent 2a970e0291
commit 922d156a5e
5 changed files with 27 additions and 22 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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