Skip to content

Commit

Permalink
Create branches.rb. Move branch related actions there. Add new method…
Browse files Browse the repository at this point in the history
…s for (un)protecting branches and creating new branches - which requires Gitlab 6.8-stable or newer. Added tests for new methods and fixed old tests looking for methods in old locations.
  • Loading branch information
asedge committed Apr 26, 2014
1 parent af49245 commit 29e31e0
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 61 deletions.
1 change: 1 addition & 0 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Client < API
include Snippets
include Projects
include Repositories
include Branches
include MergeRequests
include Groups
end
Expand Down
79 changes: 79 additions & 0 deletions lib/gitlab/client/branches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class Gitlab::Client
# Defines methods related to repositories.
module Branches
# Gets a list of project repositiory branches.
#
# @example
# Gitlab.branches(42)
#
# @param [Integer] project The ID of a project.
# @param [Hash] options A customizable set of options.
# @option options [Integer] :page The page number.
# @option options [Integer] :per_page The number of results per page.
# @return [Array<Gitlab::ObjectifiedHash>]
def branches(project, options={})
get("/projects/#{project}/repository/branches", :query => options)
end
alias_method :repo_branches, :branches

# Gets information about a repository branch.
#
# @example
# Gitlab.branch(3, 'api')
# Gitlab.repo_branch(5, 'master')
#
# @param [Integer] project The ID of a project.
# @param [String] branch The name of the branch.
# @return [Gitlab::ObjectifiedHash]
def branch(project, branch)
get("/projects/#{project}/repository/branches/#{branch}")
end

alias_method :repo_branch, :branch

# Protects a repository branch.
#
# @example
# Gitlab.protect_branch(3, 'api')
# Gitlab.repo_protect_branch(5, 'master')
#
# @param [Integer] project The ID of a project.
# @param [String] branch The name of the branch.
# @return [Gitlab::ObjectifiedHash]
def protect_branch(project, branch)
put("/projects/#{project}/repository/branches/#{branch}/protect")
end
alias_method :repo_protect_branch, :protect_branch

# Unprotects a repository branch.
#
# @example
# Gitlab.unprotect_branch(3, 'api')
# Gitlab.repo_unprotect_branch(5, 'master')
#
# @param [Integer] project The ID of a project.
# @param [String] branch The name of the branch.
# @return [Gitlab::ObjectifiedHash]
def unprotect_branch(project, branch)
put("/projects/#{project}/repository/branches/#{branch}/unprotect")
end
alias_method :repo_unprotect_branch, :unprotect_branch

# Creates a repository branch. Requires Gitlab >= 6.8.x
#
# @example
# Gitlab.create_branch(3, 'api')
# Gitlab.repo_create_branch(5, 'master')
#
# @param [Integer] project The ID of a project.
# @param [String] branch The name of the new branch.
# @param [String] ref Create branch from commit sha or existing branch
# @return [Gitlab::ObjectifiedHash]
def create_branch(project, branch, ref)
post("/projects/#{project}/repository/branches",:body => {:branch_name => branch, :ref => ref})
end
alias_method :repo_create_branch, :create_branch

end
end

29 changes: 0 additions & 29 deletions lib/gitlab/client/repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,6 @@ def tags(project, options={})
end
alias_method :repo_tags, :tags

# Gets a list of project repositiory branches.
#
# @example
# Gitlab.branches(42)
#
# @param [Integer] project The ID of a project.
# @param [Hash] options A customizable set of options.
# @option options [Integer] :page The page number.
# @option options [Integer] :per_page The number of results per page.
# @return [Array<Gitlab::ObjectifiedHash>]
def branches(project, options={})
get("/projects/#{project}/repository/branches", :query => options)
end
alias_method :repo_branches, :branches

# Gets information about a repository branch.
#
# @example
# Gitlab.branch(3, 'api')
# Gitlab.repo_branch(5, 'master')
#
# @param [Integer] project The ID of a project.
# @param [String] branch The name of the branch.
# @return [Gitlab::ObjectifiedHash]
def branch(project, branch)
get("/projects/#{project}/repository/branches/#{branch}")
end
alias_method :repo_branch, :branch

# Gets a list of project commits.
#
# @example
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"api","commit":{"id":"f7dd067490fe57505f7226c3b54d3127d2f7fd46","parents":[{"id":"949b1df930bedace1dbd755aaa4a82e8c451a616"}],"tree":"f8c4b21c036339f92fcc5482aa28a41250553b27","message":"API: expose issues project id","author":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"committer":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"authored_date":"2012-07-25T04:22:21-07:00","committed_date":"2012-07-25T04:22:21-07:00"},"protected": true}
File renamed without changes.
1 change: 1 addition & 0 deletions spec/fixtures/create_branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"api","commit":{ "id":"f7dd067490fe57505f7226c3b54d3127d2f7fd46","message":"API: expose issues project id","parent_ids":["949b1df930bedace1dbd755aaa4a82e8c451a616"],"authored_date":"2012-07-25T04:22:21-07:00","author_name":"Nihad Abbasov","author_email":"narkoz.2008@gmail.com","committed_date":"2012-07-25T04:22:21-07:00","committer_name":"Nihad Abbasov","committer_email":"narkoz.2008@gmail.com"},"protected": false}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"api","commit":{"id":"f7dd067490fe57505f7226c3b54d3127d2f7fd46","parents":[{"id":"949b1df930bedace1dbd755aaa4a82e8c451a616"}],"tree":"f8c4b21c036339f92fcc5482aa28a41250553b27","message":"API: expose issues project id","author":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"committer":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"authored_date":"2012-07-25T04:22:21-07:00","committed_date":"2012-07-25T04:22:21-07:00"}}
{"name":"api","commit":{"id":"f7dd067490fe57505f7226c3b54d3127d2f7fd46","parents":[{"id":"949b1df930bedace1dbd755aaa4a82e8c451a616"}],"tree":"f8c4b21c036339f92fcc5482aa28a41250553b27","message":"API: expose issues project id","author":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"committer":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"authored_date":"2012-07-25T04:22:21-07:00","committed_date":"2012-07-25T04:22:21-07:00"},"protected":true}
1 change: 1 addition & 0 deletions spec/fixtures/unprotect_branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"api","commit":{"id":"f7dd067490fe57505f7226c3b54d3127d2f7fd46","parents":[{"id":"949b1df930bedace1dbd755aaa4a82e8c451a616"}],"tree":"f8c4b21c036339f92fcc5482aa28a41250553b27","message":"API: expose issues project id","author":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"committer":{"name":"Nihad Abbasov","email":"narkoz.2008@gmail.com"},"authored_date":"2012-07-25T04:22:21-07:00","committed_date":"2012-07-25T04:22:21-07:00"},"protected":false}
103 changes: 103 additions & 0 deletions spec/gitlab/client/branches_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require 'spec_helper'

describe Gitlab::Client do
it { should respond_to :repo_branches }
it { should respond_to :repo_branch }
it { should respond_to :repo_protect_branch }
it { should respond_to :repo_unprotect_branch }

describe ".branches" do
before do
stub_get("/projects/3/repository/branches", "branches")
@branches = Gitlab.branches(3)
end

it "should get the correct resource" do
a_get("/projects/3/repository/branches").should have_been_made
end

it "should return an array of repository branches" do
@branches.should be_an Array
@branches.first.name.should == "api"
end
end

describe ".branch" do
before do
stub_get("/projects/3/repository/branches/api", "branch")
@branch = Gitlab.branch(3, "api")
end

it "should get the correct resource" do
a_get("/projects/3/repository/branches/api").should have_been_made
end

it "should return information about a repository branch" do
@branch.name.should == "api"
end
end

describe ".protect_branch" do
before do
stub_put("/projects/3/repository/branches/api/protect", "protect_branch")
@branch = Gitlab.protect_branch(3, "api")
end

it "should get the correct resource" do
a_put("/projects/3/repository/branches/api/protect").should have_been_made
end

it "should return information about a protected repository branch" do
@branch.name.should == "api"
@branch.protected.should == true
end
end

describe ".unprotect_branch" do
before do
stub_put("/projects/3/repository/branches/api/unprotect", "unprotect_branch")
@branch = Gitlab.unprotect_branch(3, "api")
end

it "should get the correct resource" do
a_put("/projects/3/repository/branches/api/unprotect").should have_been_made
end

it "should return information about an unprotected repository branch" do
@branch.name.should == "api"
@branch.protected.should == false
end
end

describe ".create_branch" do
context "with branch name" do
before do
stub_post("/projects/3/repository/branches", "create_branch")
@branch = Gitlab.create_branch(3, "api","master")
end

it "should get the correct resource" do
a_post("/projects/3/repository/branches").should have_been_made
end

it "should return information about a new repository branch" do
@branch.name.should == "api"
end
end
context "with commit hash" do
before do
stub_post("/projects/3/repository/branches", "create_branch")
@branch = Gitlab.create_branch(3, "api","949b1df930bedace1dbd755aaa4a82e8c451a616")
end

it "should get the correct resource" do
a_post("/projects/3/repository/branches").should have_been_made
end

it "should return information about a new repository branch" do
@branch.name.should == "api"
end
end
end

end
31 changes: 0 additions & 31 deletions spec/gitlab/client/repositories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,6 @@
end
end

describe ".branches" do
before do
stub_get("/projects/3/repository/branches", "project_branches")
@branches = Gitlab.branches(3)
end

it "should get the correct resource" do
a_get("/projects/3/repository/branches").should have_been_made
end

it "should return an array of repository branches" do
@branches.should be_an Array
@branches.first.name.should == "api"
end
end

describe ".branch" do
before do
stub_get("/projects/3/repository/branches/api", "project_branch")
@branch = Gitlab.branch(3, "api")
end

it "should get the correct resource" do
a_get("/projects/3/repository/branches/api").should have_been_made
end

it "should return information about a repository branch" do
@branch.name.should == "api"
end
end

describe ".commits" do
before do
stub_get("/projects/3/repository/commits", "project_commits").
Expand Down

0 comments on commit 29e31e0

Please sign in to comment.