diff --git a/app/controllers/projects/pipelines_controller.rb b/app/controllers/projects/pipelines_controller.rb index 19071cc9e434eea89c6c6590dc2cc595d3136a29..1282913d9818b7c3f523dd2fdc90cf5f0734f7f4 100644 --- a/app/controllers/projects/pipelines_controller.rb +++ b/app/controllers/projects/pipelines_controller.rb @@ -15,19 +15,17 @@ class Projects::PipelinesController < Projects::ApplicationController end def new + @pipeline = project.ci_commits.new end def create - begin - pipeline = Ci::CreatePipelineService.new(project, current_user, create_params).execute - redirect_to namespace_project_pipeline_path(project.namespace, project, pipeline) - rescue ArgumentError => e - flash[:alert] = e.message - render 'new' - rescue - flash[:alert] = 'The pipeline could not be created. Please try again.' + @pipeline = Ci::CreatePipelineService.new(project, current_user, create_params).execute + unless @pipeline.persisted? render 'new' + return end + + redirect_to namespace_project_pipeline_path(project.namespace, project, @pipeline) end def show diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb index e13f4fce13d5c271e3927a00ea3a8f727dfd3760..b864807ec35ab241af8271ec1812906c2cc51bfd 100644 --- a/app/services/ci/create_pipeline_service.rb +++ b/app/services/ci/create_pipeline_service.rb @@ -1,27 +1,39 @@ module Ci class CreatePipelineService < BaseService def execute + pipeline = project.ci_commits.new + unless ref_names.include?(params[:ref]) - raise ArgumentError, 'Reference not found' + pipeline.errors.add(:base, 'Reference not found') + return pipeline end unless commit - raise ArgumentError, 'Commit not found' + pipeline.errors.add(:base, 'Commit not found') + return pipeline end unless can?(current_user, :create_pipeline, project) - raise RuntimeError, 'Insufficient permissions to create a new pipeline' + pipeline.errors.add(:base, 'Insufficient permissions to create a new pipeline') + return pipeline end - pipeline = new_pipeline + begin + Ci::Commit.transaction do + pipeline.sha = commit.id + pipeline.ref = params[:ref] + pipeline.before_sha = Gitlab::Git::BLANK_SHA - Ci::Commit.transaction do - unless pipeline.config_processor - raise ArgumentError, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file' - end + unless pipeline.config_processor + pipeline.errors.add(:base, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file') + raise ActiveRecord::Rollback + end - pipeline.save! - pipeline.create_builds(current_user) + pipeline.save! + pipeline.create_builds(current_user) + end + rescue + pipeline.errors.add(:base, 'The pipeline could not be created. Please try again.') end pipeline @@ -29,10 +41,6 @@ module Ci private - def new_pipeline - project.ci_commits.new(sha: commit.id, ref: params[:ref], before_sha: Gitlab::Git::BLANK_SHA) - end - def ref_names @ref_names ||= project.repository.ref_names end diff --git a/app/views/projects/pipelines/new.html.haml b/app/views/projects/pipelines/new.html.haml index 534a495dd85891de35f384bd52be68f253562c67..1050b28b3814ca825aa05ec11586ba2d596abe35 100644 --- a/app/views/projects/pipelines/new.html.haml +++ b/app/views/projects/pipelines/new.html.haml @@ -1,15 +1,12 @@ - page_title "New Pipeline" = render "header_title" -- if @error - .alert.alert-danger - %button{ type: "button", class: "close", "data-dismiss" => "alert"} × - = @error %h3.page-title New Pipeline %hr -= form_tag namespace_project_pipelines_path, method: :post, id: "new-pipeline-form", class: "form-horizontal js-new-pipeline-form js-requires-input" do += form_for @pipeline, url: namespace_project_pipelines_path(@project.namespace, @project), html: { id: "new-pipeline-form", class: "form-horizontal js-new-pipeline-form js-requires-input" } do + = form_errors(@pipeline) .form-group = label_tag :ref, 'Create for', class: 'control-label' .col-sm-10 diff --git a/spec/features/pipelines_spec.rb b/spec/features/pipelines_spec.rb index 1df516eafd550c2dcf059ee04dee6fb045d3ac7f..32665aadd22af94024cc43a98d891070b26e6cde 100644 --- a/spec/features/pipelines_spec.rb +++ b/spec/features/pipelines_spec.rb @@ -126,12 +126,19 @@ describe "Pipelines" do before { visit new_namespace_project_pipeline_path(project.namespace, project) } context 'for valid commit' do - before do - fill_in('Create for', with: 'master') - stub_ci_commit_to_return_yaml_file + before { fill_in('Create for', with: 'master') } + + context 'with gitlab-ci.yml' do + before { stub_ci_commit_to_return_yaml_file } + + it { expect{ click_on 'Create pipeline' }.to change{ Ci::Commit.count }.by(1) } end - it { expect{ click_on 'Create pipeline' }.to change{ Ci::Commit.count }.by(1) } + context 'without gitlab-ci.yml' do + before { click_on 'Create pipeline' } + + it { expect(page).to have_content('Missing .gitlab-ci.yml file') } + end end context 'for invalid commit' do