Skip to content

Commit

Permalink
Added sudo functionality
Browse files Browse the repository at this point in the history
-Param can add sudo, allows sudo in header
-Updated some calls to allow optional hash this allows for sudo to be used in various calls. This is not strictly necessary but reads better
  • Loading branch information
Izaak Alpert authored and Izaak Alpert committed Jun 19, 2013
1 parent 53d7a8a commit 32f1419
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
17 changes: 11 additions & 6 deletions lib/gitlab/client/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ def team_member(project, id)
# @param [Integer, String] project The ID or code name of a project.
# @param [Integer] id The ID of a user.
# @param [Integer] access_level The access level to project.
# @option options [Integer/String] :sudo The user id/username to preform the request as (admin only)
# @return [Array<Gitlab::ObjectifiedHash>] Information about added team member.
def add_team_member(project, id, access_level)
post("/projects/#{project}/members", :body => {:user_id => id, :access_level => access_level})
def add_team_member(project, id, access_level, options = {})
body ={:user_id => id, :access_level => access_level}.merge(options)
post("/projects/#{project}/members", :body => body)
end

# Updates a team member's project access level.
Expand All @@ -98,9 +100,11 @@ def add_team_member(project, id, access_level)
# @param [Integer, String] project The ID or code name of a project.
# @param [Integer] id The ID of a user.
# @param [Integer] access_level The access level to project.
# @option options [Integer/String] :sudo The user id/username to preform the request as (admin only)
# @return [Array<Gitlab::ObjectifiedHash>] Information about updated team member.
def edit_team_member(project, id, access_level)
put("/projects/#{project}/members/#{id}", :body => {:access_level => access_level})
def edit_team_member(project, id, access_level, options = {})
body = {:access_level => access_level}.merge(options)
put("/projects/#{project}/members/#{id}", :body => body)
end

# Removes a user from project team.
Expand All @@ -110,9 +114,10 @@ def edit_team_member(project, id, access_level)
#
# @param [Integer, String] project The ID or code name of a project.
# @param [Integer] id The ID of a user.
# @option options [Integer/String] :sudo The user id/username to preform the request as (admin only)
# @return [Array<Gitlab::ObjectifiedHash>] Information about removed team member.
def remove_team_member(project, id)
delete("/projects/#{project}/members/#{id}")
def remove_team_member(project, id, options= {})
delete("/projects/#{project}/members/#{id}", :body => options)
end

# Gets a list of project hooks.
Expand Down
3 changes: 2 additions & 1 deletion lib/gitlab/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Gitlab
# Defines constants and methods related to configuration.
module Configuration
# An array of valid keys in the options hash when configuring a Gitlab::API.
VALID_OPTIONS_KEYS = [:endpoint, :private_token, :user_agent].freeze
VALID_OPTIONS_KEYS = [:endpoint, :private_token, :user_agent, :sudo].freeze

# The user agent that will be sent to the API endpoint if none is set.
DEFAULT_USER_AGENT = "Gitlab Ruby Gem #{Gitlab::VERSION}".freeze
Expand Down Expand Up @@ -32,6 +32,7 @@ def options
def reset
self.endpoint = nil
self.private_token = nil
self.sudo = nil
self.user_agent = DEFAULT_USER_AGENT
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gitlab/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def put(path, options={})
validate self.class.put(path, options)
end

def delete(path)
validate self.class.delete(path)
def delete(path, options={})
validate self.class.delete(path, options={})
end

# Checks the response code for common errors.
Expand Down
37 changes: 27 additions & 10 deletions spec/gitlab/client/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

it "should get the correct resource" do
a_post("/projects/3/members").
with(:body => {:user_id => '1', :access_level => '40'}).should have_been_made
with(:body => {:user_id => '1', :access_level => '40'}).should have_been_made
end

it "should return information about an added team member" do
Expand All @@ -119,7 +119,7 @@

it "should get the correct resource" do
a_put("/projects/3/members/1").
with(:body => {:access_level => '40'}).should have_been_made
with(:body => {:access_level => '40'}).should have_been_made
end

it "should return information about an edited team member" do
Expand All @@ -128,18 +128,35 @@
end

describe ".remove_team_member" do
before do
stub_delete("/projects/3/members/1", "team_member")
@team_member = Gitlab.remove_team_member(3, 1)
end
context 'as a user' do
before do
stub_delete("/projects/3/members/1", "team_member")
@team_member = Gitlab.remove_team_member(3, 1)
end

it "should get the correct resource" do
a_delete("/projects/3/members/1").should have_been_made
it "should get the correct resource" do
a_delete("/projects/3/members/1").should have_been_made
end

it "should return information about a removed team member" do
@team_member.name.should == "John Smith"
end
end
context 'as sudo-ing to a user' do
before do
stub_delete("/projects/3/members/1", "team_member")
@team_member = Gitlab.remove_team_member(3, 1, :sudo => 1)
end

it "should return information about a removed team member" do
@team_member.name.should == "John Smith"
it "should get the correct resource" do
a_delete("/projects/3/members/1").should have_been_made
end

it "should return information about a removed team member" do
@team_member.name.should == "John Smith"
end
end

end

describe ".project_hooks" do
Expand Down

0 comments on commit 32f1419

Please sign in to comment.