diff --git a/lib/gitlab/api.rb b/lib/gitlab/api.rb index 1171a5956..d9078c52c 100644 --- a/lib/gitlab/api.rb +++ b/lib/gitlab/api.rb @@ -11,7 +11,7 @@ def initialize(options={}) Configuration::VALID_OPTIONS_KEYS.each do |key| send("#{key}=", options[key]) end - set_request_defaults @endpoint, @private_token + set_request_defaults @endpoint, @private_token, @sudo end end end diff --git a/lib/gitlab/client/projects.rb b/lib/gitlab/client/projects.rb index ec6a4b9d2..88c8a7f6d 100644 --- a/lib/gitlab/client/projects.rb +++ b/lib/gitlab/client/projects.rb @@ -22,8 +22,8 @@ def projects(options={}) # # @param [Integer, String] id The ID or name of a project. # @return [Gitlab::ObjectifiedHash] - def project(id, options ={}) - get("/projects/#{id}", :query => options) + def project(id) + get("/projects/#{id}") end # Creates a new project. @@ -87,11 +87,9 @@ def team_member(project, id) # @param [Integer] id The ID of a user. # @param [Integer] access_level The access level to project. # @param [Hash] options A customizable set of options. - # @option options [Integer, String] :sudo The user id/username to perform the request as (admin only) # @return [Gitlab::ObjectifiedHash] Information about added team member. - 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) + def add_team_member(project, id, access_level) + post("/projects/#{project}/members", :body => {:user_id => id, :access_level => access_level}) end # Updates a team member's project access level. @@ -103,11 +101,9 @@ def add_team_member(project, id, access_level, options = {}) # @param [Integer] id The ID of a user. # @param [Integer] access_level The access level to project. # @param [Hash] options A customizable set of options. - # @option options [Integer, String] :sudo The user id/username to perform the request as (admin only) # @return [Array] Information about updated team member. - def edit_team_member(project, id, access_level, options = {}) - body = {:access_level => access_level}.merge(options) - put("/projects/#{project}/members/#{id}", :body => body) + def edit_team_member(project, id, access_level) + put("/projects/#{project}/members/#{id}", :body => {:access_level => access_level}) end # Removes a user from project team. @@ -118,10 +114,9 @@ def edit_team_member(project, id, access_level, options = {}) # @param [Integer, String] project The ID or name of a project. # @param [Integer] id The ID of a user. # @param [Hash] options A customizable set of options. - # @option options [Integer, String] :sudo The user id/username to perform the request as (admin only) # @return [Gitlab::ObjectifiedHash] Information about removed team member. - def remove_team_member(project, id, options= {}) - delete("/projects/#{project}/members/#{id}", :body => options) + def remove_team_member(project, id) + delete("/projects/#{project}/members/#{id}") end # Gets a list of project hooks. diff --git a/lib/gitlab/request.rb b/lib/gitlab/request.rb index 7c256da91..56304047f 100644 --- a/lib/gitlab/request.rb +++ b/lib/gitlab/request.rb @@ -42,8 +42,8 @@ def put(path, options={}) validate self.class.put(path, options) end - def delete(path, options={}) - validate self.class.delete(path, options={}) + def delete(path) + validate self.class.delete(path) end # Checks the response code for common errors. @@ -76,14 +76,15 @@ def error_message(response) "Request URI: #{response.request.base_uri}#{response.request.path}" end - # Sets a base_uri and private_token parameter for requests. + # Sets a base_uri and default_params for requests. # @raise [Error::MissingCredentials] if endpoint or private_token not set. - def set_request_defaults(endpoint, private_token) + def set_request_defaults(endpoint, private_token, sudo=nil) raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint raise Error::MissingCredentials.new("Please set a private_token for user") unless private_token self.class.base_uri endpoint - self.class.default_params :private_token => private_token + self.class.default_params :private_token => private_token, :sudo => sudo + self.class.default_params.delete(:sudo) if sudo.nil? end end end diff --git a/spec/gitlab/client/projects_spec.rb b/spec/gitlab/client/projects_spec.rb index c53d60ae0..2e5fd01ca 100644 --- a/spec/gitlab/client/projects_spec.rb +++ b/spec/gitlab/client/projects_spec.rb @@ -19,34 +19,19 @@ end describe ".project" do - context 'normal execution' do - before do - stub_get("/projects/3", "project") - @project = Gitlab.project(3) - end - - it "should get the correct resource" do - a_get("/projects/3").should have_been_made - end - - it "should return information about a project" do - @project.name.should == "Gitlab" - @project.owner.name.should == "John Smith" - end - end - - context 'supports sudo' do - it "should support the additional argument" do - stub_request(:get, "#{Gitlab.endpoint}/projects/3"). - with(:query => {:private_token => Gitlab.private_token, :sudo => "foo"}). - to_return(:body => load_fixture("project")) - @project = Gitlab.project(3, {sudo:'foo'}) - a_get("/projects/3?sudo=foo").should have_been_made - @project.name.should == "Gitlab" - @project.owner.name.should == "John Smith" - end + before do + stub_get("/projects/3", "project") + @project = Gitlab.project(3) + end + + it "should get the correct resource" do + a_get("/projects/3").should have_been_made end + it "should return information about a project" do + @project.name.should == "Gitlab" + @project.owner.name.should == "John Smith" + end end describe ".create_project" do @@ -143,35 +128,18 @@ end describe ".remove_team_member" do - 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 - end - - it "should return information about a removed team member" do - @team_member.name.should == "John Smith" - end + before do + stub_delete("/projects/3/members/1", "team_member") + @team_member = Gitlab.remove_team_member(3, 1) 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 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 + 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 describe ".project_hooks" do @@ -297,7 +265,7 @@ @deploy_keys.should be_an Array @deploy_keys.first.id.should eq 2 @deploy_keys.first.title.should eq "Key Title" - @deploy_keys.first.key.should match /ssh-rsa/ + @deploy_keys.first.key.should match(/ssh-rsa/) end end @@ -314,7 +282,7 @@ it "should return project deploy key" do @deploy_key.id.should eq 2 @deploy_key.title.should eq "Key Title" - @deploy_key.key.should match /ssh-rsa/ + @deploy_key.key.should match(/ssh-rsa/) end end diff --git a/spec/gitlab_spec.rb b/spec/gitlab_spec.rb index 5ad2de2f9..d93ba6a66 100644 --- a/spec/gitlab_spec.rb +++ b/spec/gitlab_spec.rb @@ -23,6 +23,13 @@ end end + describe ".sudo=" do + it "should set sudo" do + Gitlab.sudo = 'user' + Gitlab.sudo.should == 'user' + end + end + describe ".user_agent" do it "should return default user_agent" do Gitlab.user_agent.should == Gitlab::Configuration::DEFAULT_USER_AGENT