Commit a390f5ff authored by Sean McGivern's avatar Sean McGivern

Merge branch '57657-promote-label-to-group-label-via-api-endpoint' into 'master'

Promote Label to Group Label via API endpoint

Closes #57657

See merge request gitlab-org/gitlab-ce!25218
parents 1c983571 1819dbd4
---
title: 'API: Promote project labels to group labels'
merge_request: 25218
author: Robert Schilling
type: added
......@@ -186,6 +186,40 @@ Example response:
}
```
## Promote a project label to a group label
> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/25218) in GitLab 12.3.
Promotes a project label to a group label.
```
PUT /projects/:id/labels/promote
```
| Attribute | Type | Required | Description |
| --------------- | ------- | --------------------------------- | ------------------------------- |
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user |
| `name` | string | yes | The name of the existing label |
```bash
curl --request PUT --data "name=documentation" --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/labels/promote"
```
Example response:
```json
{
"id" : 8,
"name" : "documentation",
"color" : "#8E44AD",
"description": "Documentation",
"open_issues_count": 1,
"closed_issues_count": 0,
"open_merge_requests_count": 2,
"subscribed": false
}
```
## Subscribe to a label
Subscribes the authenticated user to a label to receive notifications.
......
......@@ -62,6 +62,31 @@ module API
delete ':id/labels' do
delete_label(user_project)
end
desc 'Promote a label to a group label' do
detail 'This feature was added in GitLab 12.3'
success Entities::GroupLabel
end
params do
requires :name, type: String, desc: 'The name of the label to be promoted'
end
put ':id/labels/promote' do
authorize! :admin_label, user_project
label = find_label(user_project, params[:name], include_ancestor_groups: false)
begin
group_label = ::Labels::PromoteService.new(user_project, current_user).execute(label)
if group_label
present group_label, with: Entities::GroupLabel, current_user: current_user, parent: user_project.group
else
render_api_error!('Failed to promote project label to group label', 400)
end
rescue => error
render_api_error!(error.to_s, 400)
end
end
end
end
end
......@@ -473,6 +473,55 @@ describe API::Labels do
end
end
describe 'PUT /projects/:id/labels/promote' do
let(:group) { create(:group) }
before do
group.add_owner(user)
project.update!(group: group)
end
it 'returns 200 if label is promoted' do
put api("/projects/#{project.id}/labels/promote", user), params: { name: label1.name }
expect(response).to have_gitlab_http_status(200)
expect(json_response['name']).to eq(label1.name)
expect(json_response['color']).to eq(label1.color)
end
it 'returns 200 if group label already exists' do
create(:group_label, title: label1.name, group: group)
expect { put api("/projects/#{project.id}/labels/promote", user), params: { name: label1.name } }
.to change(project.labels, :count).by(-1)
.and change(group.labels, :count).by(0)
expect(response).to have_gitlab_http_status(200)
end
it 'returns 403 if guest promotes label' do
guest = create(:user)
project.add_guest(guest)
put api("/projects/#{project.id}/labels/promote", guest), params: { name: label1.name }
expect(response).to have_gitlab_http_status(403)
end
it 'returns 404 if label does not exist' do
put api("/projects/#{project.id}/labels/promote", user), params: { name: 'unknown' }
expect(response).to have_gitlab_http_status(404)
end
it 'returns 400 if no label name given' do
put api("/projects/#{project.id}/labels/promote", user)
expect(response).to have_gitlab_http_status(400)
expect(json_response['error']).to eq('name is missing')
end
end
describe "POST /projects/:id/labels/:label_id/subscribe" do
context "when label_id is a label title" do
it "subscribes to the label" do
......
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