Commit 1069ffb8 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

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

Create branch via API

Fixes #1096
parents 2d3d210b a88225b7
...@@ -5,6 +5,7 @@ v 6.8.0 ...@@ -5,6 +5,7 @@ v 6.8.0
- Remove omniauth-ldap nickname bug workaround - Remove omniauth-ldap nickname bug workaround
- Drop all tables before restoring a Postgres backup - Drop all tables before restoring a Postgres backup
- Make the repository downloads path configurable - Make the repository downloads path configurable
- Create branches via API (sponsored by O'Reilly Media)
v 6.7.2 v 6.7.2
- Fix upgrader script - Fix upgrader script
......
...@@ -16,11 +16,7 @@ class Projects::BranchesController < Projects::ApplicationController ...@@ -16,11 +16,7 @@ class Projects::BranchesController < Projects::ApplicationController
end end
def create def create
@repository.add_branch(params[:branch_name], params[:ref]) CreateBranchService.new.execute(project, params[:branch_name], params[:ref], current_user)
if new_branch = @repository.find_branch(params[:branch_name])
Event.create_ref_event(@project, current_user, new_branch, 'add')
end
redirect_to project_branches_path(@project) redirect_to project_branches_path(@project)
end end
......
class CreateBranchService
def execute(project, branch_name, ref, current_user)
repository = project.repository
repository.add_branch(branch_name, ref)
new_branch = repository.find_branch(branch_name)
if new_branch
Event.create_ref_event(project, current_user, new_branch, 'add')
end
new_branch
end
end
...@@ -165,3 +165,34 @@ Parameters: ...@@ -165,3 +165,34 @@ Parameters:
"protected": false "protected": false
} }
``` ```
## Create repository branch
```
POST /projects/:id/repository/branches
```
Parameters:
+ `id` (required) - The ID of a project
+ `branch_name` (required) - The name of the branch
+ `ref` (required) - Create branch from commit sha or existing branch
```json
{
"name": "my-new-branch",
"commit": {
"id": "8848c0e90327a0b70f1865b843fb2fbfb9345e57",
"message": "Merge pull request #54 from brightbox/use_fog_brightbox_module\n\nUpdate to use fog-brightbox module",
"parent_ids": ["fff449e0bf453576f16c91d6544f00a2664009d8", "f93a93626fec20fd659f4ed3ab2e64019b6169ae"],
"authored_date": "2014-02-20T19:54:55+02:00",
"author_name": "john smith",
"author_email": "john@example.com",
"committed_date": "2014-02-20T19:54:55+02:00",
"committer_name": "john smith",
"committer_email": "john@example.com"
},
"protected": false
}
```
...@@ -65,6 +65,21 @@ module API ...@@ -65,6 +65,21 @@ module API
present @branch, with: Entities::RepoObject, project: user_project present @branch, with: Entities::RepoObject, project: user_project
end end
# Create branch
#
# Parameters:
# id (required) - The ID of a project
# branch_name (required) - The name of the branch
# ref (required) - Create branch from commit sha or existing branch
# Example Request:
# POST /projects/:id/repository/branches
post ":id/repository/branches" do
authorize_push_project
@branch = CreateBranchService.new.execute(user_project, params[:branch_name], params[:ref], current_user)
present @branch, with: Entities::RepoObject, project: user_project
end
end end
end end
end end
...@@ -78,6 +78,10 @@ module API ...@@ -78,6 +78,10 @@ module API
end end
end end
def authorize_push_project
authorize! :push_code, user_project
end
def authorize_admin_project def authorize_admin_project
authorize! :admin_project, user_project authorize! :admin_project, user_project
end end
......
...@@ -92,4 +92,24 @@ describe API::API do ...@@ -92,4 +92,24 @@ describe API::API do
end end
describe "POST /projects/:id/repository/branches" do
it "should create a new branch" do
post api("/projects/#{project.id}/repository/branches", user),
branch_name: 'new_design',
ref: '621491c677087aa243f165eab467bfdfbee00be1'
response.status.should == 201
json_response['name'].should == 'new_design'
json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
end
it "should deny for user without push access" do
post api("/projects/#{project.id}/repository/branches", user2),
branch_name: 'new_design',
ref: '621491c677087aa243f165eab467bfdfbee00be1'
response.status.should == 403
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment