From 32f14199c53da58ce00572530a002f769c808024 Mon Sep 17 00:00:00 2001 From: Izaak Alpert Date: Mon, 1 Apr 2013 08:53:56 -0400 Subject: [PATCH] Added sudo functionality -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 --- lib/gitlab/client/projects.rb | 17 ++++++++----- lib/gitlab/configuration.rb | 3 ++- lib/gitlab/request.rb | 4 ++-- spec/gitlab/client/projects_spec.rb | 37 +++++++++++++++++++++-------- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/gitlab/client/projects.rb b/lib/gitlab/client/projects.rb index 39386dcad..9ec8ecf35 100644 --- a/lib/gitlab/client/projects.rb +++ b/lib/gitlab/client/projects.rb @@ -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] 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. @@ -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] 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. @@ -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] 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. diff --git a/lib/gitlab/configuration.rb b/lib/gitlab/configuration.rb index 5124ca070..2cf6e2961 100644 --- a/lib/gitlab/configuration.rb +++ b/lib/gitlab/configuration.rb @@ -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 @@ -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 diff --git a/lib/gitlab/request.rb b/lib/gitlab/request.rb index 711f79af4..204dcb67b 100644 --- a/lib/gitlab/request.rb +++ b/lib/gitlab/request.rb @@ -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. diff --git a/spec/gitlab/client/projects_spec.rb b/spec/gitlab/client/projects_spec.rb index 15258c5a9..624583773 100644 --- a/spec/gitlab/client/projects_spec.rb +++ b/spec/gitlab/client/projects_spec.rb @@ -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 @@ -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 @@ -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