Commit 922d156a authored by Robert Schilling's avatar Robert Schilling

Separate branch and tag names

parent 2a970e02
--- ---
title: 'API: Get references a commit is pushed to' title: API: Get references a commit is pushed to
merge_request: 15026 merge_request: 15026
author: Robert Schilling author: Robert Schilling
type: added type: added
...@@ -214,7 +214,7 @@ Parameters: ...@@ -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 | `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 | | `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 ```bash
curl --request POST --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --form "type=all" "https://gitlab.example.com/api/v4/projects/5/repository/commits/5937ac0a7beb003549fc5fd26fc247adbce4a52e/refs" 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: ...@@ -224,11 +224,10 @@ Example response:
```json ```json
[ [
{"name": "'test'"}, {"branch_name": "'test'"},
{"name": "master"}, {"branch_name": "master"},
{"name": "v1.1.0"} {"tag_name": "v1.1.0"}
] ]
``` ```
## Cherry pick a commit ## Cherry pick a commit
......
...@@ -171,12 +171,12 @@ module API ...@@ -171,12 +171,12 @@ module API
refs = refs =
case params[:type] case params[:type]
when 'branches' 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' 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 else
refs = user_project.repository.branch_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)) refs.concat(user_project.repository.tag_names_contains(commit.id).map {|tag_name| [tag_name, false]})
end end
present refs, with: Entities::BasicRef present refs, with: Entities::BasicRef
......
...@@ -277,8 +277,12 @@ module API ...@@ -277,8 +277,12 @@ module API
end end
class BasicRef < Grape::Entity class BasicRef < Grape::Entity
expose :name do |ref, options| expose :branch_name, if: lambda { |ref, options| ref[1] } do |ref, options|
ref ref[0]
end
expose :tag_name, if: lambda { |ref, options| !ref[1] } do |ref, options|
ref[0]
end end
end end
......
...@@ -492,35 +492,37 @@ describe API::Commits do ...@@ -492,35 +492,37 @@ describe API::Commits do
it 'returns all refs with no scope' do it 'returns all refs with no scope' do
get api(route, current_user) get api(route, current_user)
repo_refs = project.repository.branch_names_contains(commit_id) branch_refs = project.repository.branch_names_contains(commit_id)
repo_refs.push(*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['branch_name'] }.compact).to eq(branch_refs)
expect(json_response.map { |refs| refs['tag_name'] }.compact).to eq(tag_refs)
end end
it 'returns all refs' do it 'returns all refs' do
get api(route, current_user), type: 'all' get api(route, current_user), type: 'all'
repo_refs = project.repository.branch_names_contains(commit_id) branch_refs = project.repository.branch_names_contains(commit_id)
repo_refs.push(*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['branch_name'] }.compact).to eq(branch_refs)
expect(json_response.map { |refs| refs['tag_name'] }.compact).to eq(tag_refs)
end end
it 'returns the branch refs' do it 'returns the branch refs' do
get api(route, current_user), type: 'branches' 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 end
it 'returns the tag refs' do it 'returns the tag refs' do
get api(route, current_user), type: 'tags' 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 end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment