diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-08-15 10:25:48 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-08-15 10:25:48 +0300 |
commit | 27cf081e1b0b1df1661aaf0ae6b60b05ef3eb8d8 (patch) | |
tree | e666cc1b8c925d8685c5128a58b415aea5e7ab81 /lib/api/labels.rb | |
parent | 014eff5d8f0081820f3f7a9ac905ab894e101dd7 (diff) | |
parent | cf3ba0209dc7dc8b9ac93d574a8f6296b858be40 (diff) | |
download | gitlab-ce-27cf081e1b0b1df1661aaf0ae6b60b05ef3eb8d8.tar.gz |
Merge pull request #7479 from Razer6/feature/labels_api
Implement complete labels API (create/delete/update)
Diffstat (limited to 'lib/api/labels.rb')
-rw-r--r-- | lib/api/labels.rb | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/api/labels.rb b/lib/api/labels.rb new file mode 100644 index 00000000000..c73a4dbe916 --- /dev/null +++ b/lib/api/labels.rb @@ -0,0 +1,100 @@ +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 + + # 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 |