Commit 1069ffb8735a82900e14e05e07bf25841704dc90
Exists in
spb-stable
and in
3 other branches
Merge branch 'branch-via-api' into 'master'
Create branch via API Fixes #1096
Showing
7 changed files
with
85 additions
and
5 deletions
Show diff stats
CHANGELOG
@@ -5,6 +5,7 @@ v 6.8.0 | @@ -5,6 +5,7 @@ v 6.8.0 | ||
5 | - Remove omniauth-ldap nickname bug workaround | 5 | - Remove omniauth-ldap nickname bug workaround |
6 | - Drop all tables before restoring a Postgres backup | 6 | - Drop all tables before restoring a Postgres backup |
7 | - Make the repository downloads path configurable | 7 | - Make the repository downloads path configurable |
8 | + - Create branches via API (sponsored by O'Reilly Media) | ||
8 | 9 | ||
9 | v 6.7.2 | 10 | v 6.7.2 |
10 | - Fix upgrader script | 11 | - Fix upgrader script |
app/controllers/projects/branches_controller.rb
@@ -16,11 +16,7 @@ class Projects::BranchesController < Projects::ApplicationController | @@ -16,11 +16,7 @@ class Projects::BranchesController < Projects::ApplicationController | ||
16 | end | 16 | end |
17 | 17 | ||
18 | def create | 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 | redirect_to project_branches_path(@project) | 21 | redirect_to project_branches_path(@project) |
26 | end | 22 | end |
@@ -0,0 +1,13 @@ | @@ -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,3 +165,34 @@ Parameters: | ||
165 | "protected": false | 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,6 +65,21 @@ module API | ||
65 | 65 | ||
66 | present @branch, with: Entities::RepoObject, project: user_project | 66 | present @branch, with: Entities::RepoObject, project: user_project |
67 | end | 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 | end | 83 | end |
69 | end | 84 | end |
70 | end | 85 | end |
lib/api/helpers.rb
@@ -78,6 +78,10 @@ module API | @@ -78,6 +78,10 @@ module API | ||
78 | end | 78 | end |
79 | end | 79 | end |
80 | 80 | ||
81 | + def authorize_push_project | ||
82 | + authorize! :push_code, user_project | ||
83 | + end | ||
84 | + | ||
81 | def authorize_admin_project | 85 | def authorize_admin_project |
82 | authorize! :admin_project, user_project | 86 | authorize! :admin_project, user_project |
83 | end | 87 | end |
spec/requests/api/branches_spec.rb
@@ -92,4 +92,24 @@ describe API::API do | @@ -92,4 +92,24 @@ describe API::API do | ||
92 | end | 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 | end | 115 | end |