Commit 33a00ceeeacfc52272d25cef914a027b9bf13a2a

Authored by Dmitriy Zaporozhets
1 parent 95b84e2c

Create branch via API

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
lib/api/branches.rb
... ... @@ -65,6 +65,21 @@ module API
65 65  
66 66 present @branch, with: Entities::RepoObject, project: user_project
67 67 end
  68 +
  69 + # Create branch
  70 + #
  71 + # Parameters:
  72 + # id (required) - The ID of a project
  73 + # branch_name (required) - The name of the branch
  74 + # ref (required) - Create branch from commit sha or existing branch
  75 + # Example Request:
  76 + # POST /projects/:id/repository/branches
  77 + post ":id/repository/branches" do
  78 + authorize_push_project
  79 + @branch = CreateBranchService.new.execute(user_project, params[:branch_name], params[:ref], current_user)
  80 +
  81 + present @branch, with: Entities::RepoObject, project: user_project
  82 + end
68 83 end
69 84 end
70 85 end
... ...
lib/api/helpers.rb
... ... @@ -78,6 +78,10 @@ module API
78 78 end
79 79 end
80 80  
  81 + def authorize_push_project
  82 + authorize! :push_code, user_project
  83 + end
  84 +
81 85 def authorize_admin_project
82 86 authorize! :admin_project, user_project
83 87 end
... ...
spec/requests/api/branches_spec.rb
... ... @@ -92,4 +92,24 @@ describe API::API do
92 92 end
93 93  
94 94  
  95 + describe "POST /projects/:id/repository/branches" do
  96 + it "should create a new branch" do
  97 + post api("/projects/#{project.id}/repository/branches", user),
  98 + branch_name: 'new_design',
  99 + ref: '621491c677087aa243f165eab467bfdfbee00be1'
  100 +
  101 + response.status.should == 201
  102 +
  103 + json_response['name'].should == 'new_design'
  104 + json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
  105 + end
  106 +
  107 + it "should deny for user without push access" do
  108 + post api("/projects/#{project.id}/repository/branches", user2),
  109 + branch_name: 'new_design',
  110 + ref: '621491c677087aa243f165eab467bfdfbee00be1'
  111 +
  112 + response.status.should == 403
  113 + end
  114 + end
95 115 end
... ...