Commit ef60b8e1 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Use pipelines.errors when communicating the error

parent 379dc6fb
...@@ -15,19 +15,17 @@ class Projects::PipelinesController < Projects::ApplicationController ...@@ -15,19 +15,17 @@ class Projects::PipelinesController < Projects::ApplicationController
end end
def new def new
@pipeline = project.ci_commits.new
end end
def create def create
begin @pipeline = Ci::CreatePipelineService.new(project, current_user, create_params).execute
pipeline = Ci::CreatePipelineService.new(project, current_user, create_params).execute unless @pipeline.persisted?
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.'
render 'new' render 'new'
return
end end
redirect_to namespace_project_pipeline_path(project.namespace, project, @pipeline)
end end
def show def show
......
module Ci module Ci
class CreatePipelineService < BaseService class CreatePipelineService < BaseService
def execute def execute
pipeline = project.ci_commits.new
unless ref_names.include?(params[:ref]) unless ref_names.include?(params[:ref])
raise ArgumentError, 'Reference not found' pipeline.errors.add(:base, 'Reference not found')
return pipeline
end end
unless commit unless commit
raise ArgumentError, 'Commit not found' pipeline.errors.add(:base, 'Commit not found')
return pipeline
end end
unless can?(current_user, :create_pipeline, project) 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 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
unless pipeline.config_processor pipeline.errors.add(:base, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file')
raise ArgumentError, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file' raise ActiveRecord::Rollback
end end
pipeline.save! pipeline.save!
pipeline.create_builds(current_user) pipeline.create_builds(current_user)
end
rescue
pipeline.errors.add(:base, 'The pipeline could not be created. Please try again.')
end end
pipeline pipeline
...@@ -29,10 +41,6 @@ module Ci ...@@ -29,10 +41,6 @@ module Ci
private private
def new_pipeline
project.ci_commits.new(sha: commit.id, ref: params[:ref], before_sha: Gitlab::Git::BLANK_SHA)
end
def ref_names def ref_names
@ref_names ||= project.repository.ref_names @ref_names ||= project.repository.ref_names
end end
......
- page_title "New Pipeline" - page_title "New Pipeline"
= render "header_title" = render "header_title"
- if @error
.alert.alert-danger
%button{ type: "button", class: "close", "data-dismiss" => "alert"} &times;
= @error
%h3.page-title %h3.page-title
New Pipeline New Pipeline
%hr %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 .form-group
= label_tag :ref, 'Create for', class: 'control-label' = label_tag :ref, 'Create for', class: 'control-label'
.col-sm-10 .col-sm-10
......
...@@ -126,12 +126,19 @@ describe "Pipelines" do ...@@ -126,12 +126,19 @@ describe "Pipelines" do
before { visit new_namespace_project_pipeline_path(project.namespace, project) } before { visit new_namespace_project_pipeline_path(project.namespace, project) }
context 'for valid commit' do context 'for valid commit' do
before do before { fill_in('Create for', with: 'master') }
fill_in('Create for', with: 'master')
stub_ci_commit_to_return_yaml_file 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 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 end
context 'for invalid commit' do context 'for invalid commit' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment