Skip to content

Commit

Permalink
Add labels api for list, create, edit and delete.
Browse files Browse the repository at this point in the history
  • Loading branch information
artworx committed Nov 27, 2014
1 parent e9107e0 commit a7c18f1
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ class Client < API
include Snippets
include SystemHooks
include Users
include Labels
end
end
55 changes: 55 additions & 0 deletions lib/gitlab/client/labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Gitlab::Client
module Labels
# Gets a list of project's labels.
#
# @example
# Gitlab.labels(5)
#
# @param [Integer] project The ID of a project.
# @return [Array<Gitlab::ObjectifiedHash>]
def labels(project)
get("/projects/#{project}/labels")
end

# Creates a new label.
#
# @example
# Gitlab.create_label(42, "Backlog", '#DD10AA')
#
# @param [Integer] project The ID of a project.
# @option [String] name The name of a label.
# @option [String] color The color of a label.
# @return [Gitlab::ObjectifiedHash] Information about created label.
def create_label(project, name, color)
post("/projects/#{project}/labels", :body => { :name => name, :color => color})
end

# Updates a label.
#
# @example
# Gitlab.edit_label(42, "Backlog", :new_name => 'TODO')
# Gitlab.edit_label(42, "Backlog", :new_name => 'TODO', :color => '#DD10AA')
#
# @param [Integer] project The ID of a project.
# @param [String] name The name of a label.
# @param [Hash] options A customizable set of options.
# @option options [String] :new_name The new name of a label.
# @option options [String] :color The color of a label.
# @return [Gitlab::ObjectifiedHash] Information about updated label.
def edit_label(project, name, options={})
put("/projects/#{project}/labels", :body => options.merge({:name => name}))
end

# Deletes a label.
#
# @example
# Gitlab.delete_label(2, 'Backlog')
#
# @param [Integer] project The ID of a project.
# @param [String] name The name of a label.
# @return [Gitlab::ObjectifiedHash] Information about deleted label.
def delete_label(project, name)
delete("/projects/#{project}/labels", :body => {:name => name} )
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "Backlog", "color": "#DD10AA"}
1 change: 1 addition & 0 deletions spec/fixtures/labels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name": "Backlog", "color": "#DD10AA"},{"name": "Documentation", "color": "#1E80DD"}]
68 changes: 68 additions & 0 deletions spec/gitlab/client/labels_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'spec_helper'

describe Gitlab::Client do
describe ".labels" do
before do
stub_get("/projects/3/labels", "labels")
@labels = Gitlab.labels(3)
end

it "should get the correct resource" do
expect(a_get("/projects/3/labels")).to have_been_made
end

it "should return an array of project's labels" do
expect(@labels).to be_an Array
expect(@labels.first.name).to eq("Backlog")
end
end

describe ".delete" do
before do
stub_delete("/projects/3/labels", "label")
@label = Gitlab.delete_label(3, "Backlog")
end

it "should get the correct resource" do
expect(a_delete("/projects/3/labels").
with(:body => {:name => 'Backlog'})).to have_been_made
end

it "should return information about a deleted snippet" do
expect(@label.name).to eq("Backlog")
end
end

describe ".edit_label" do
before do
stub_put("/projects/3/labels", "label")
@label = Gitlab.edit_label(3, "TODO", :new_name => 'Backlog')
end

it "should get the correct resource" do
expect(a_put("/projects/3/labels").
with(:body => {:name => 'TODO', :new_name => "Backlog"})).to have_been_made
end

it "should return information about an edited label" do
expect(@label.name).to eq("Backlog")
end
end

describe ".create_label" do
before do
stub_post("/projects/3/labels", "label")
@label = Gitlab.create_label(3, 'Backlog', '#DD10AA')
end

it "should get the correct resource" do
expect(a_post("/projects/3/labels").
with(:body => {:name => 'Backlog', :color => '#DD10AA'})).to have_been_made
end

it "should return information about a created label" do
expect(@label.name).to eq('Backlog')
expect(@label.color).to eq('#DD10AA')
end
end
end

0 comments on commit a7c18f1

Please sign in to comment.