Merge branch 'branch-via-api' into 'master'
Create branch via API Fixes #1096
This commit is contained in:
commit
1069ffb873
7 changed files with 85 additions and 5 deletions
|
@ -5,6 +5,7 @@ v 6.8.0
|
|||
- Remove omniauth-ldap nickname bug workaround
|
||||
- Drop all tables before restoring a Postgres backup
|
||||
- Make the repository downloads path configurable
|
||||
- Create branches via API (sponsored by O'Reilly Media)
|
||||
|
||||
v 6.7.2
|
||||
- Fix upgrader script
|
||||
|
|
|
@ -16,11 +16,7 @@ class Projects::BranchesController < Projects::ApplicationController
|
|||
end
|
||||
|
||||
def create
|
||||
@repository.add_branch(params[:branch_name], params[:ref])
|
||||
|
||||
if new_branch = @repository.find_branch(params[:branch_name])
|
||||
Event.create_ref_event(@project, current_user, new_branch, 'add')
|
||||
end
|
||||
CreateBranchService.new.execute(project, params[:branch_name], params[:ref], current_user)
|
||||
|
||||
redirect_to project_branches_path(@project)
|
||||
end
|
||||
|
|
13
app/services/create_branch_service.rb
Normal file
13
app/services/create_branch_service.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
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:
|
|||
"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
|
|||
|
||||
present @branch, with: Entities::RepoObject, project: user_project
|
||||
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
|
||||
|
|
|
@ -78,6 +78,10 @@ module API
|
|||
end
|
||||
end
|
||||
|
||||
def authorize_push_project
|
||||
authorize! :push_code, user_project
|
||||
end
|
||||
|
||||
def authorize_admin_project
|
||||
authorize! :admin_project, user_project
|
||||
end
|
||||
|
|
|
@ -92,4 +92,24 @@ describe API::API do
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue