Read about our upcoming Code of Conduct on this issue

Commit 161af17c authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Introduce source to pipeline entity

parent 3fff1617446d
......@@ -58,7 +58,7 @@ def new
def create
@pipeline = Ci::CreatePipelineService
.new(project, current_user, create_params)
.execute(ignore_skip_ci: true, save_on_errors: false)
.execute(:web, ignore_skip_ci: true, save_on_errors: false)
if @pipeline.persisted?
redirect_to namespace_project_pipeline_path(project.namespace, project, @pipeline)
......
......@@ -30,6 +30,7 @@ class Pipeline < ActiveRecord::Base
delegate :id, to: :project, prefix: true
validates :source, exclusion: { in: %w(unknown), unless: :importing? }, on: :create
validates :sha, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? }
validates :status, presence: { unless: :importing? }
......@@ -37,6 +38,16 @@ class Pipeline < ActiveRecord::Base
after_create :keep_around_commits, unless: :importing?
enum source: {
unknown: nil,
push: 1,
web: 2,
trigger: 3,
schedule: 4,
api: 5,
external: 6
}
state_machine :status, initial: :created do
event :enqueue do
transition created: :pending
......@@ -269,10 +280,6 @@ def latest?
commit.sha == sha
end
def triggered?
trigger_requests.any?
end
def retried
@retried ||= (statuses.order(id: :desc) - statuses.latest)
end
......
......@@ -1064,11 +1064,6 @@ def pipeline_for(ref, sha = nil)
pipelines.order(id: :desc).find_by(sha: sha, ref: ref)
end
def ensure_pipeline(ref, sha, current_user = nil)
pipeline_for(ref, sha) ||
pipelines.create(sha: sha, ref: ref, user: current_user)
end
def enable_ci
project_feature.update_attribute(:builds_access_level, ProjectFeature::ENABLED)
end
......
......@@ -5,6 +5,7 @@ class PipelineEntity < Grape::Entity
expose :user, using: UserEntity
expose :active?, as: :active
expose :coverage
expose :source
expose :path do |pipeline|
namespace_project_pipeline_path(
......@@ -24,7 +25,6 @@ class PipelineEntity < Grape::Entity
expose :flags do
expose :latest?, as: :latest
expose :triggered?, as: :triggered
expose :stuck?, as: :stuck
expose :has_yaml_errors?, as: :yaml_errors
expose :can_retry?, as: :retryable
......
......@@ -2,8 +2,9 @@ module Ci
class CreatePipelineService < BaseService
attr_reader :pipeline
def execute(ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil)
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil)
@pipeline = Ci::Pipeline.new(
source: source,
project: project,
ref: ref,
sha: sha,
......
......@@ -4,7 +4,7 @@ def execute(project, trigger, ref, variables = nil)
trigger_request = trigger.trigger_requests.create(variables: variables)
pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: ref).
execute(ignore_skip_ci: true, trigger_request: trigger_request)
execute(:trigger, ignore_skip_ci: true, trigger_request: trigger_request)
trigger_request if pipeline.persisted?
end
......
......@@ -106,7 +106,7 @@ def execute_related_hooks
EventCreateService.new.push(@project, current_user, build_push_data)
@project.execute_hooks(build_push_data.dup, :push_hooks)
@project.execute_services(build_push_data.dup, :push_hooks)
Ci::CreatePipelineService.new(@project, current_user, build_push_data).execute
Ci::CreatePipelineService.new(@project, current_user, build_push_data).execute(:push)
if push_remove_branch?
AfterBranchDeleteService
......
......@@ -11,7 +11,7 @@ def execute
SystemHooksService.new.execute_hooks(build_system_push_data.dup, :tag_push_hooks)
project.execute_hooks(@push_data.dup, :tag_push_hooks)
project.execute_services(@push_data.dup, :tag_push_hooks)
Ci::CreatePipelineService.new(project, current_user, @push_data).execute
Ci::CreatePipelineService.new(project, current_user, @push_data).execute(:push)
ProjectCacheWorker.perform_async(project.id, [], [:commit_count, :repository_size])
true
......
......@@ -14,7 +14,7 @@ def perform
Ci::CreatePipelineService.new(schedule.project,
schedule.owner,
ref: schedule.ref)
.execute(save_on_errors: false, schedule: schedule)
.execute(:schedule, save_on_errors: false, schedule: schedule)
rescue => e
Rails.logger.error "#{schedule.id}: Failed to create a scheduled pipeline: #{e.message}"
ensure
......
---
title: Introduce source to Pipeline entity
merge_request:
author:
......@@ -98,7 +98,7 @@ def create_merge_request_pipelines
def create_pipeline!(project, ref, commit)
project.pipelines.create(sha: commit.id, ref: ref)
project.pipelines.create(sha: commit.id, ref: ref, source: :push)
end
def build_create!(pipeline, opts = {})
......
......@@ -190,7 +190,7 @@ def run_builds(merge_requests)
service = Ci::CreatePipelineService.new(merge_request.project,
@user,
ref: "refs/heads/#{merge_request.source_branch}")
pipeline = service.execute(ignore_skip_ci: true, save_on_errors: false)
pipeline = service.execute(:push, ignore_skip_ci: true, save_on_errors: false)
pipeline.run!
Timecop.travel rand(1..6).hours.from_now
......
class AddSourceToCiPipeline < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
add_column :ci_pipelines, :source, :integer
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170523091700) do
ActiveRecord::Schema.define(version: 20170524125940) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -283,6 +283,7 @@
t.integer "lock_version"
t.integer "auto_canceled_by_id"
t.integer "pipeline_schedule_id"
t.integer "source"
end
add_index "ci_pipelines", ["auto_canceled_by_id"], name: "index_ci_pipelines_on_auto_canceled_by_id", using: :btree
......
......@@ -35,7 +35,7 @@ class Spinach::Features::ProjectPages < Spinach::FeatureSteps
end
step 'pages are deployed' do
pipeline = @project.ensure_pipeline('HEAD', @project.commit('HEAD').sha)
pipeline = @project.pipelines.create(ref: 'HEAD', sha: @project.commit('HEAD').sha)
build = build(:ci_build,
project: @project,
pipeline: pipeline,
......
......@@ -68,7 +68,14 @@ class CommitStatuses < Grape::API
name = params[:name] || params[:context] || 'default'
pipeline = @project.ensure_pipeline(ref, commit.sha, current_user)
pipeline = @project.pipeline_for(ref, commit.sha)
unless pipeline
pipeline = @project.pipelines.create!(
source: :external,
sha: commit.sha,
ref: ref,
user: current_user)
end
status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
project: @project,
......
......@@ -47,7 +47,7 @@ class Pipelines < Grape::API
new_pipeline = Ci::CreatePipelineService.new(user_project,
current_user,
declared_params(include_missing: false))
.execute(ignore_skip_ci: true, save_on_errors: false)
.execute(:api, ignore_skip_ci: true, save_on_errors: false)
if new_pipeline.persisted?
present new_pipeline, with: Entities::Pipeline
else
......
FactoryGirl.define do
factory :ci_empty_pipeline, class: Ci::Pipeline do
source :push
ref 'master'
sha '97de212e80737a608d939f648d959671fb0a0142'
status 'pending'
......
......@@ -442,6 +442,8 @@ def create_build(stage, stage_idx, name, status)
it 'creates a new pipeline' do
expect { click_on 'Create pipeline' }
.to change { Ci::Pipeline.count }.by(1)
expect(Ci::Pipeline.last).to be_web
end
end
......
......@@ -191,6 +191,7 @@ Ci::Pipeline:
- lock_version
- auto_canceled_by_id
- pipeline_schedule_id
- source
CommitStatus:
- id
- project_id
......
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