From 95b84e2c5aa92a5a8effc108fdbdf596dff4818c Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 1 Apr 2014 10:33:23 +0300 Subject: [PATCH 1/4] Move branch creation logic in service Signed-off-by: Dmitriy Zaporozhets --- app/controllers/projects/branches_controller.rb | 6 +----- app/services/create_branch_service.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 app/services/create_branch_service.rb diff --git a/app/controllers/projects/branches_controller.rb b/app/controllers/projects/branches_controller.rb index aa6914414ce..6a6cbe48184 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 00000000000..98beeee8354 --- /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 From 33a00ceeeacfc52272d25cef914a027b9bf13a2a Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 1 Apr 2014 10:39:53 +0300 Subject: [PATCH 2/4] Create branch via API Signed-off-by: Dmitriy Zaporozhets --- lib/api/branches.rb | 15 +++++++++++++++ lib/api/helpers.rb | 4 ++++ spec/requests/api/branches_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/lib/api/branches.rb b/lib/api/branches.rb index 6339094bd99..953c6100f8b 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 03a2968fcc7..7ee4b9d1381 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 47c0ba94a4a..f792c618e67 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 From d7afdab7a5fcb1aa686955f6d13e024737e89a94 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 1 Apr 2014 10:44:58 +0300 Subject: [PATCH 3/4] Add docs for create branch via api Signed-off-by: Dmitriy Zaporozhets --- doc/api/branches.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/api/branches.md b/doc/api/branches.md index a62f9e38a90..3417a695077 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 +} +``` From a88225b7689ab87b49908db23e1bebf1c7ea09d3 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 1 Apr 2014 10:45:10 +0300 Subject: [PATCH 4/4] Update changelog with new feature Signed-off-by: Dmitriy Zaporozhets --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index e3ab6a5e924..d936986a779 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