diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2017-04-21 12:57:41 -0500 |
---|---|---|
committer | Alfredo Sumaran <alfredo@gitlab.com> | 2017-05-03 13:42:54 -0500 |
commit | 88cba7e2ba4b49649a203ce8b8dde28daf34e386 (patch) | |
tree | bcef83f20ae9627c89f5b24358a9f111df006d37 | |
parent | 574e243bb046413b196d39b9d3454e41577e62bd (diff) | |
download | gitlab-ce-88cba7e2ba4b49649a203ce8b8dde28daf34e386.tar.gz |
Fix Project::BranchesController#create to support JSON format
-rw-r--r-- | app/controllers/projects/branches_controller.rb | 32 | ||||
-rw-r--r-- | spec/controllers/projects/branches_controller_spec.rb | 38 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/branch.json | 12 |
3 files changed, 70 insertions, 12 deletions
diff --git a/app/controllers/projects/branches_controller.rb b/app/controllers/projects/branches_controller.rb index 840405f38cb..71374925235 100644 --- a/app/controllers/projects/branches_controller.rb +++ b/app/controllers/projects/branches_controller.rb @@ -46,20 +46,30 @@ class Projects::BranchesController < Projects::ApplicationController SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue end - if result[:status] == :success - @branch = result[:branch] + respond_to do |format| + if result[:status] == :success + @branch = result[:branch] + + format.html do + if redirect_to_autodeploy + redirect_to url_to_autodeploy_setup(project, branch_name), + notice: view_context.autodeploy_flash_notice(branch_name) + else + redirect_to namespace_project_tree_path(@project.namespace, @project, @branch.name) + end + end - if redirect_to_autodeploy - redirect_to( - url_to_autodeploy_setup(project, branch_name), - notice: view_context.autodeploy_flash_notice(branch_name)) + format.json do + render json: { name: @branch.name, url: namespace_project_tree_url(@project.namespace, @project, @branch.name) } + end else - redirect_to namespace_project_tree_path(@project.namespace, @project, - @branch.name) + format.html do + @error = result[:message] + render action: 'new' + end + + format.json { render json: result[:messsage], status: :unprocessable_entity } end - else - @error = result[:message] - render action: 'new' end end diff --git a/spec/controllers/projects/branches_controller_spec.rb b/spec/controllers/projects/branches_controller_spec.rb index d20e7368086..8f915d9d210 100644 --- a/spec/controllers/projects/branches_controller_spec.rb +++ b/spec/controllers/projects/branches_controller_spec.rb @@ -14,7 +14,7 @@ describe Projects::BranchesController do controller.instance_variable_set(:@project, project) end - describe "POST create" do + describe "POST create with HTML format" do render_views context "on creation of a new branch" do @@ -152,6 +152,42 @@ describe Projects::BranchesController do end end + describe 'POST create with JSON format' do + before do + sign_in(user) + end + + context 'with valid params' do + it 'returns a successful 200 response' do + create_branch name: 'my-branch', ref: 'master' + + expect(response).to have_http_status(200) + end + + it 'returns the created branch' do + create_branch name: 'my-branch', ref: 'master' + + expect(response).to match_response_schema('branch') + end + end + + context 'with invalid params' do + it 'returns an unprocessable entity 422 response' do + create_branch name: "<script>alert('merge');</script>", ref: "<script>alert('ref');</script>" + + expect(response).to have_http_status(422) + end + end + + def create_branch(name:, ref:) + post :create, namespace_id: project.namespace.to_param, + project_id: project.to_param, + branch_name: name, + ref: ref, + format: :json + end + end + describe "POST destroy with HTML format" do render_views diff --git a/spec/fixtures/api/schemas/branch.json b/spec/fixtures/api/schemas/branch.json new file mode 100644 index 00000000000..0bb74577010 --- /dev/null +++ b/spec/fixtures/api/schemas/branch.json @@ -0,0 +1,12 @@ +{ + "type": "object", + "required" : [ + "name", + "url" + ], + "properties" : { + "name": { "type": "string" }, + "url": { "type": "uri" } + }, + "additionalProperties": false +} |