Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
cf3ba020
Commit
cf3ba020
authored
Aug 13, 2014
by
Robert Schilling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update labels via API
parent
9284038d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
126 additions
and
9 deletions
+126
-9
CHANGELOG
CHANGELOG
+1
-0
app/models/label.rb
app/models/label.rb
+1
-4
doc/api/labels.md
doc/api/labels.md
+23
-1
lib/api/labels.rb
lib/api/labels.rb
+35
-0
spec/requests/api/labels_spec.rb
spec/requests/api/labels_spec.rb
+66
-4
No files found.
CHANGELOG
View file @
cf3ba020
...
...
@@ -11,6 +11,7 @@ v 7.2.0
- Fix bug when MR download patch return invalid diff
- Test gitlab-shell integration
- Repository import timeout increased from 2 to 4 minutes allowing larger repos to be imported
- API for labels (Robert Schilling)
v 7.1.0
- Remove observers
...
...
app/models/label.rb
View file @
cf3ba020
...
...
@@ -7,10 +7,7 @@ class Label < ActiveRecord::Base
validates
:project
,
presence:
true
# Dont allow '?', '&', and ',' for label titles
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^&\?,&]*\z/
},
uniqueness:
true
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^&\?,&]*\z/
}
scope
:order_by_name
,
->
{
reorder
(
"labels.title ASC"
)
}
...
...
doc/api/labels.md
View file @
cf3ba020
...
...
@@ -60,4 +60,26 @@ DELETE /projects/:id/labels
It returns 200 if the label successfully was deleted, 404 for wrong parameters
and 400 if the label does not exist.
In case of an error, additionally an error is returned.
In case of an error, additionally an error message is returned.
## Edit an existing label
Updates an existing label with new name or now color. At least one parameter
is required, to update the label.
```
PUT /projects/:id/labels
```
Parameters:
-
`id`
(required) - The ID of a project
-
`name`
(required) - The name of the existing label
-
`new_name`
(optional) - The new name of the label
-
`color`
(optional) - New color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)
On success, this method returns 200 with the updated label.
If required parameters are missing, 400 is returned.
If the label to be updated is missing, 404 is returned.
If parameters are invalid, 405 is returned. In case of an error,
additionally an error message is returned.
lib/api/labels.rb
View file @
cf3ba020
...
...
@@ -60,6 +60,41 @@ module API
label
.
destroy
end
# Updates an existing label. At least one optional parameter is required.
#
# Parameters:
# id (required) - The ID of a project
# name (optional) - The name of the label to be deleted
# color (optional) - Color of the label given in 6-digit hex
# notation with leading '#' sign (e.g. #FFAABB)
# Example Request:
# PUT /projects/:id/labels
put
':id/labels'
do
required_attributes!
[
:name
]
label
=
user_project
.
find_label
(
params
[
:name
])
if
!
label
return
render_api_error!
(
'Label not found'
,
404
)
end
attrs
=
attributes_for_keys
[
:new_name
,
:color
]
if
attrs
.
empty?
return
render_api_error!
(
'Required parameters "name" or "color" '
\
'missing'
,
400
)
end
# Rename new name to the actual label attribute name
attrs
[
:name
]
=
attrs
.
delete
(
:new_name
)
if
attrs
.
key?
(
:new_name
)
if
label
.
update
(
attrs
)
present
label
,
with:
Entities
::
Label
else
render_api_error!
(
label
.
errors
.
full_messages
.
join
(
', '
),
405
)
end
end
end
end
end
spec/requests/api/labels_spec.rb
View file @
cf3ba020
...
...
@@ -69,14 +69,12 @@ describe API::API, api: true do
describe
'DELETE /projects/:id/labels'
do
it
'should return 200 for existing label'
do
delete
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
delete
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
response
.
status
.
should
==
200
end
it
'should return 404 for non existing label'
do
delete
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label2'
delete
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label2'
response
.
status
.
should
==
404
json_response
[
'message'
].
should
==
'Label not found'
end
...
...
@@ -86,4 +84,68 @@ describe API::API, api: true do
response
.
status
.
should
==
400
end
end
describe
'PUT /projects/:id/labels'
do
it
'should return 200 if name and colors are changed'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
,
new_name:
'New Label'
,
color:
'#FFFFFF'
response
.
status
.
should
==
200
json_response
[
'name'
].
should
==
'New Label'
json_response
[
'color'
].
should
==
'#FFFFFF'
end
it
'should return 200 if name is changed'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
,
new_name:
'New Label'
response
.
status
.
should
==
200
json_response
[
'name'
].
should
==
'New Label'
json_response
[
'color'
].
should
==
label1
.
color
end
it
'should return 200 if colors is changed'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
,
color:
'#FFFFFF'
response
.
status
.
should
==
200
json_response
[
'name'
].
should
==
label1
.
name
json_response
[
'color'
].
should
==
'#FFFFFF'
end
it
'should return 404 if label does not exist'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label2'
,
new_name:
'label3'
response
.
status
.
should
==
404
end
it
'should return 400 if no label name given'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
new_name:
'label2'
response
.
status
.
should
==
400
end
it
'should return 400 if no new parameters given'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
response
.
status
.
should
==
400
end
it
'should return 405 for invalid name'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
,
new_name:
'?'
,
color:
'#FFFFFF'
response
.
status
.
should
==
405
json_response
[
'message'
].
should
==
'Title is invalid'
end
it
'should return 405 for invalid name'
do
put
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
,
color:
'#FF'
response
.
status
.
should
==
405
json_response
[
'message'
].
should
==
'Color is invalid'
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment