diff --git a/CHANGELOG b/CHANGELOG index e3ab6a5e92..d936986a77 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/app/controllers/projects/branches_controller.rb b/app/controllers/projects/branches_controller.rb index aa6914414c..6a6cbe4818 100644 --- a/app/controllers/projects/branches_controller.rb +++ b/app/controllers/projects/branches_controller.rb @@ -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 diff --git a/app/services/create_branch_service.rb b/app/services/create_branch_service.rb new file mode 100644 index 0000000000..98beeee835 --- /dev/null +++ b/app/services/create_branch_service.rb @@ -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 diff --git a/doc/api/branches.md b/doc/api/branches.md index a62f9e38a9..3417a69507 100644 --- a/doc/api/branches.md +++ b/doc/api/branches.md @@ -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 +} +``` diff --git a/lib/api/branches.rb b/lib/api/branches.rb index 6339094bd9..953c6100f8 100644 --- a/lib/api/branches.rb +++ b/lib/api/branches.rb @@ -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 diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 03a2968fcc..7ee4b9d138 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -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 diff --git a/spec/requests/api/branches_spec.rb b/spec/requests/api/branches_spec.rb index 47c0ba94a4..f792c618e6 100644 --- a/spec/requests/api/branches_spec.rb +++ b/spec/requests/api/branches_spec.rb @@ -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