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
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
9284038d
Commit
9284038d
authored
Aug 12, 2014
by
Robert Schilling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add, delete labels via API
parent
53ead2e3
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
205 additions
and
42 deletions
+205
-42
app/models/label.rb
app/models/label.rb
+5
-4
app/models/project.rb
app/models/project.rb
+4
-0
doc/api/README.md
doc/api/README.md
+1
-0
doc/api/labels.md
doc/api/labels.md
+63
-0
doc/api/projects.md
doc/api/projects.md
+0
-26
lib/api/api.rb
lib/api/api.rb
+1
-0
lib/api/entities.rb
lib/api/entities.rb
+1
-1
lib/api/labels.rb
lib/api/labels.rb
+65
-0
lib/api/projects.rb
lib/api/projects.rb
+0
-11
spec/requests/api/labels_spec.rb
spec/requests/api/labels_spec.rb
+65
-0
No files found.
app/models/label.rb
View file @
9284038d
...
...
@@ -7,13 +7,14 @@ class Label < ActiveRecord::Base
validates
:project
,
presence:
true
# Dont allow '?', '&', and ',' for label titles
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^&\?,&]*\z/
}
validates
:title
,
presence:
true
,
format:
{
with:
/\A[^&\?,&]*\z/
},
uniqueness:
true
scope
:order_by_name
,
->
{
reorder
(
"labels.title ASC"
)
}
def
name
title
end
alias_attribute
:name
,
:title
def
open_issues_count
issues
.
opened
.
count
...
...
app/models/project.rb
View file @
9284038d
...
...
@@ -577,4 +577,8 @@ class Project < ActiveRecord::Base
def
forks_count
ForkedProjectLink
.
where
(
forked_from_project_id:
self
.
id
).
count
end
def
find_label
(
name
)
labels
.
find_by
(
name:
name
)
end
end
doc/api/README.md
View file @
9284038d
...
...
@@ -12,6 +12,7 @@
-
[
Branches
](
branches.md
)
-
[
Merge Requests
](
merge_requests.md
)
-
[
Issues
](
issues.md
)
-
[
Labels
](
labels.md
)
-
[
Milestones
](
milestones.md
)
-
[
Notes
](
notes.md
)
(
comments
)
-
[
Deploy Keys
](
deploy_keys.md
)
...
...
doc/api/labels.md
0 → 100644
View file @
9284038d
# Labels
## List labels
Get all labels for given project.
```
GET /projects/:id/labels
```
```
json
[
{
"name"
:
"Awesome"
,
"color"
:
"#DD10AA"
},
{
"name"
:
"Documentation"
,
"color"
:
"#1E80DD"
},
{
"name"
:
"Feature"
,
"color"
:
"#11FF22"
},
{
"name"
:
"Bug"
,
"color"
:
"#EE1122"
}
]
```
## Create a new label
Creates a new label for given repository with given name and color.
```
POST /projects/:id/labels
```
Parameters:
-
`id`
(required) - The ID of a project
-
`name`
(required) - The name of the label
-
`color`
(required) - Color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)
It returns 200 and the newly created label, if the operation succeeds.
If the label already exists, 409 and an error message is returned.
If label parameters are invalid, 405 and an explaining error message is returned.
## Delete a label
Deletes a label given by its name.
```
DELETE /projects/:id/labels
```
-
`id`
(required) - The ID of a project
-
`name`
(required) - The name of the label to be deleted
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.
doc/api/projects.md
View file @
9284038d
...
...
@@ -632,29 +632,3 @@ Parameters:
+
query (required) - A string contained in the project name
+
per_page (optional) - number of projects to return per page
+
page (optional) - the page to retrieve
## Labels
### List project labels
Get a list of project labels.
```
GET /projects/:id/labels
```
Parameters:
+
`id`
(required) - The ID or NAMESPACE/PROJECT_NAME of a project
```
json
[
{
"name"
:
"feature"
},
{
"name"
:
"bug"
}
]
```
lib/api/api.rb
View file @
9284038d
...
...
@@ -46,5 +46,6 @@ module API
mount
Commits
mount
Namespaces
mount
Branches
mount
Labels
end
end
lib/api/entities.rb
View file @
9284038d
...
...
@@ -192,7 +192,7 @@ module API
end
class
Label
<
Grape
::
Entity
expose
:name
expose
:name
,
:color
end
class
RepoDiff
<
Grape
::
Entity
...
...
lib/api/labels.rb
0 → 100644
View file @
9284038d
module
API
# Labels API
class
Labels
<
Grape
::
API
before
{
authenticate!
}
resource
:projects
do
# Get all labels of the project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# GET /projects/:id/labels
get
':id/labels'
do
present
user_project
.
labels
,
with:
Entities
::
Label
end
# Creates a new label
#
# Parameters:
# id (required) - The ID of a project
# name (required) - The name of the label to be deleted
# color (required) - Color of the label given in 6-digit hex
# notation with leading '#' sign (e.g. #FFAABB)
# Example Request:
# POST /projects/:id/labels
post
':id/labels'
do
required_attributes!
[
:name
,
:color
]
attrs
=
attributes_for_keys
[
:name
,
:color
]
label
=
user_project
.
find_label
(
attrs
[
:name
])
if
label
return
render_api_error!
(
'Label already exists'
,
409
)
end
label
=
user_project
.
labels
.
create
(
attrs
)
if
label
.
valid?
present
label
,
with:
Entities
::
Label
else
render_api_error!
(
label
.
errors
.
full_messages
.
join
(
', '
),
405
)
end
end
# Deletes an existing label
#
# Parameters:
# id (required) - The ID of a project
# name (required) - The name of the label to be deleted
#
# Example Request:
# DELETE /projects/:id/labels
delete
':id/labels'
do
required_attributes!
[
:name
]
label
=
user_project
.
find_label
(
params
[
:name
])
if
!
label
return
render_api_error!
(
'Label not found'
,
404
)
end
label
.
destroy
end
end
end
end
lib/api/projects.rb
View file @
9284038d
...
...
@@ -224,17 +224,6 @@ module API
@users
=
paginate
@users
present
@users
,
with:
Entities
::
UserBasic
end
# Get a project labels
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# GET /projects/:id/labels
get
':id/labels'
do
@labels
=
user_project
.
labels
present
@labels
,
with:
Entities
::
Label
end
end
end
end
spec/requests/api/labels_spec.rb
View file @
9284038d
...
...
@@ -21,4 +21,69 @@ describe API::API, api: true do
json_response
.
first
[
'name'
].
should
==
label1
.
name
end
end
describe
'POST /projects/:id/labels'
do
it
'should return created label'
do
post
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'Foo'
,
color:
'#FFAABB'
response
.
status
.
should
==
201
json_response
[
'name'
].
should
==
'Foo'
json_response
[
'color'
].
should
==
'#FFAABB'
end
it
'should return a 400 bad request if name not given'
do
post
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
color:
'#FFAABB'
response
.
status
.
should
==
400
end
it
'should return a 400 bad request if color not given'
do
post
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'Foobar'
response
.
status
.
should
==
400
end
it
'should return 405 for invalid color'
do
post
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'Foo'
,
color:
'#FFAA'
response
.
status
.
should
==
405
json_response
[
'message'
].
should
==
'Color is invalid'
end
it
'should return 405 for invalid name'
do
post
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'?'
,
color:
'#FFAABB'
response
.
status
.
should
==
405
json_response
[
'message'
].
should
==
'Title is invalid'
end
it
'should return 409 if label already exists'
do
post
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
),
name:
'label1'
,
color:
'#FFAABB'
response
.
status
.
should
==
409
json_response
[
'message'
].
should
==
'Label already exists'
end
end
describe
'DELETE /projects/:id/labels'
do
it
'should return 200 for existing label'
do
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'
response
.
status
.
should
==
404
json_response
[
'message'
].
should
==
'Label not found'
end
it
'should return 400 for wrong parameters'
do
delete
api
(
"/projects/
#{
project
.
id
}
/labels"
,
user
)
response
.
status
.
should
==
400
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