Commit 1069ffb8735a82900e14e05e07bf25841704dc90

Authored by Dmitriy Zaporozhets
2 parents 2d3d210b a88225b7

Merge branch 'branch-via-api' into 'master'

Create branch via API

Fixes #1096
CHANGELOG
... ... @@ -5,6 +5,7 @@ v 6.8.0
5 5 - Remove omniauth-ldap nickname bug workaround
6 6 - Drop all tables before restoring a Postgres backup
7 7 - Make the repository downloads path configurable
  8 + - Create branches via API (sponsored by O'Reilly Media)
8 9  
9 10 v 6.7.2
10 11 - Fix upgrader script
... ...
app/controllers/projects/branches_controller.rb
... ... @@ -16,11 +16,7 @@ class Projects::BranchesController < Projects::ApplicationController
16 16 end
17 17  
18 18 def create
19   - @repository.add_branch(params[:branch_name], params[:ref])
20   -
21   - if new_branch = @repository.find_branch(params[:branch_name])
22   - Event.create_ref_event(@project, current_user, new_branch, 'add')
23   - end
  19 + CreateBranchService.new.execute(project, params[:branch_name], params[:ref], current_user)
24 20  
25 21 redirect_to project_branches_path(@project)
26 22 end
... ...
app/services/create_branch_service.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class CreateBranchService
  2 + def execute(project, branch_name, ref, current_user)
  3 + repository = project.repository
  4 + repository.add_branch(branch_name, ref)
  5 + new_branch = repository.find_branch(branch_name)
  6 +
  7 + if new_branch
  8 + Event.create_ref_event(project, current_user, new_branch, 'add')
  9 + end
  10 +
  11 + new_branch
  12 + end
  13 +end
... ...
doc/api/branches.md
... ... @@ -165,3 +165,34 @@ Parameters:
165 165 "protected": false
166 166 }
167 167 ```
  168 +
  169 +## Create repository branch
  170 +
  171 +
  172 +```
  173 +POST /projects/:id/repository/branches
  174 +```
  175 +
  176 +Parameters:
  177 +
  178 ++ `id` (required) - The ID of a project
  179 ++ `branch_name` (required) - The name of the branch
  180 ++ `ref` (required) - Create branch from commit sha or existing branch
  181 +
  182 +```json
  183 +{
  184 + "name": "my-new-branch",
  185 + "commit": {
  186 + "id": "8848c0e90327a0b70f1865b843fb2fbfb9345e57",
  187 + "message": "Merge pull request #54 from brightbox/use_fog_brightbox_module\n\nUpdate to use fog-brightbox module",
  188 + "parent_ids": ["fff449e0bf453576f16c91d6544f00a2664009d8", "f93a93626fec20fd659f4ed3ab2e64019b6169ae"],
  189 + "authored_date": "2014-02-20T19:54:55+02:00",
  190 + "author_name": "john smith",
  191 + "author_email": "john@example.com",
  192 + "committed_date": "2014-02-20T19:54:55+02:00",
  193 + "committer_name": "john smith",
  194 + "committer_email": "john@example.com"
  195 + },
  196 + "protected": false
  197 +}
  198 +```
... ...
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
... ...