Commit 163983e0 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add triggered by to pipeline to support relation between dependent pipelines

parent 0a70942f
......@@ -9,6 +9,8 @@ module Ci
belongs_to :trigger_request
belongs_to :erased_by, class_name: 'User'
has_many :sourced_pipelines, class_name: Ci::Sources::Pipeline, foreign_key: :source_job_id
has_many :deployments, as: :deployable
has_one :last_deployment, -> { order('deployments.id DESC') }, as: :deployable, class_name: 'Deployment'
......
......@@ -11,6 +11,13 @@ module Ci
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
belongs_to :pipeline_schedule, class_name: 'Ci::PipelineSchedule'
has_one :source_pipeline, class_name: Ci::Sources::Pipeline
has_many :sourced_pipelines, class_name: Ci::Sources::Pipeline, foreign_key: :source_pipeline_id
has_one :triggered_by_pipeline, through: :source_pipeline, source: :source_pipeline
has_many :triggered_pipelines, through: :sourced_pipelines, source: :pipeline
has_many :auto_canceled_pipelines, class_name: 'Ci::Pipeline', foreign_key: 'auto_canceled_by_id'
has_many :auto_canceled_jobs, class_name: 'CommitStatus', foreign_key: 'auto_canceled_by_id'
......
module Ci
module Sources
class Pipeline < ActiveRecord::Base
self.table_name = "ci_sources_pipelines"
belongs_to :project, class_name: Project
belongs_to :pipeline, class_name: Ci::Pipeline
belongs_to :source_project, class_name: Project, foreign_key: :source_project_id
belongs_to :source_job, class_name: Ci::Build, foreign_key: :source_job_id
belongs_to :source_pipeline, class_name: Ci::Pipeline, foreign_key: :source_pipeline_id
end
end
end
......@@ -187,6 +187,10 @@ class Project < ActiveRecord::Base
has_many :deployments, dependent: :destroy
has_many :pipeline_schedules, dependent: :destroy, class_name: 'Ci::PipelineSchedule'
has_many :sourced_pipelines, class_name: Ci::Sources::Pipeline, foreign_key: :source_project_id
has_many :source_pipelines, class_name: Ci::Sources::Pipeline, foreign_key: :project_id
has_many :path_locks, dependent: :destroy
has_many :active_runners, -> { active }, through: :runner_projects, source: :runner, class_name: 'Ci::Runner'
......
......@@ -4,4 +4,7 @@ class PipelineDetailsEntity < PipelineEntity
expose :artifacts, using: BuildArtifactEntity
expose :manual_actions, using: BuildActionEntity
end
expose :triggered_by_pipeline, as: :triggered_by, with: TriggeredPipelineEntity
expose :triggered_pipelines, as: :triggered, using: TriggeredPipelineEntity
end
......@@ -18,6 +18,8 @@ class PipelineSerializer < BaseSerializer
:cancelable_statuses,
:trigger_requests,
:project,
{ triggered_by_pipeline: [:project, :user] },
{ triggered_pipelines: [:project, :user] },
{ pending_builds: :project },
{ manual_actions: :project },
{ artifacts: :project }
......
class TriggeredPipelineEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :user, using: UserEntity
expose :active?, as: :active
expose :coverage
expose :source
expose :path do |pipeline|
namespace_project_pipeline_path(
pipeline.project.namespace,
pipeline.project,
pipeline)
end
expose :details do
expose :detailed_status, as: :status, with: StatusEntity
end
expose :project, using: ProjectEntity
private
alias_method :pipeline, :object
def detailed_status
pipeline.detailed_status(request.current_user)
end
end
......@@ -10,13 +10,19 @@ class ExpirePipelineCacheWorker
store = Gitlab::EtagCaching::Store.new
store.touch(project_pipelines_path(project))
store.touch(project_pipeline_path(project, pipeline))
store.touch(project_pipeline_path(pipeline))
store.touch(commit_pipelines_path(project, pipeline.commit)) if pipeline.commit
store.touch(new_merge_request_pipelines_path(project))
each_pipelines_merge_request_path(project, pipeline) do |path|
store.touch(path)
end
store.touch(project_pipeline_path(pipeline.triggered_by_pipeline)) if pipeline.triggered_by_pipeline
pipeline.triggered_pipelines.each do |triggered|
store.touch(project_pipeline_path(triggered))
end
Gitlab::Cache::Ci::ProjectPipelineStatus.update_for_pipeline(pipeline)
end
......@@ -29,10 +35,10 @@ class ExpirePipelineCacheWorker
format: :json)
end
def project_pipeline_path(project, pipeline)
def project_pipeline_path(pipeline)
Gitlab::Routing.url_helpers.namespace_project_pipeline_path(
project.namespace,
project,
pipeline.project.namespace,
pipeline.project,
pipeline,
format: :json)
end
......
......@@ -75,7 +75,15 @@ class Gitlab::Seeder::Pipelines
def create_master_pipelines
@project.repository.commits('master', limit: 4).map do |commit|
create_pipeline!(@project, 'master', commit)
create_pipeline!(@project, 'master', commit, triggerer_pipeline).tap do |pipeline|
triggerer_pipeline.tap do |triggerer_pipeline|
triggerer_pipeline.sourced_pipelines.create(
source_job: triggerer_pipeline.builds.all.sample,
source_project: triggerer_pipeline.project,
project: pipeline.project,
pipeline: pipeline)
end
end
end
rescue
[]
......@@ -96,8 +104,7 @@ class Gitlab::Seeder::Pipelines
[]
end
def create_pipeline!(project, ref, commit)
def create_pipeline!(project, ref, commit, triggerer = nil)
project.pipelines.create(sha: commit.id, ref: ref, source: :push)
end
......@@ -151,6 +158,10 @@ class Gitlab::Seeder::Pipelines
@project.team.users.sample
end
def triggerer_pipeline
Ci::Pipeline.limit(4).all.sample
end
def build_status
Ci::Build::AVAILABLE_STATUSES.sample
end
......
class CreatePipelineSourcePipeline < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
create_table :ci_sources_pipelines, force: :cascade do |t|
t.integer :project_id
t.integer :pipeline_id
t.integer :source_project_id
t.integer :source_job_id
t.integer :source_pipeline_id
end
end
end
class AddCiPipelineSourcePipelineIndexes < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :ci_sources_pipelines, :project_id
add_concurrent_index :ci_sources_pipelines, :pipeline_id
add_concurrent_index :ci_sources_pipelines, :source_project_id
add_concurrent_index :ci_sources_pipelines, :source_job_id
add_concurrent_index :ci_sources_pipelines, :source_pipeline_id
end
def down
remove_concurrent_index :ci_sources_pipelines, :project_id
remove_concurrent_index :ci_sources_pipelines, :pipeline_id
remove_concurrent_index :ci_sources_pipelines, :source_project_id
remove_concurrent_index :ci_sources_pipelines, :source_job_id
remove_concurrent_index :ci_sources_pipelines, :source_pipeline_id
end
end
class AddCiPipelineSourcePipelineForeignKey < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_foreign_key :ci_sources_pipelines, :projects, column: :project_id
add_concurrent_foreign_key :ci_sources_pipelines, :ci_pipelines, column: :pipeline_id
add_concurrent_foreign_key :ci_sources_pipelines, :projects, column: :source_project_id
add_concurrent_foreign_key :ci_sources_pipelines, :ci_builds, column: :source_job_id
add_concurrent_foreign_key :ci_sources_pipelines, :ci_pipelines, column: :source_pipeline_id
end
def down
remove_foreign_key :ci_sources_pipelines, :projects, column: :project_id
remove_foreign_key :ci_sources_pipelines, :ci_pipelines, column: :pipeline_id
remove_foreign_key :ci_sources_pipelines, :projects, column: :source_project_id
remove_foreign_key :ci_sources_pipelines, :ci_builds, column: :source_job_id
remove_foreign_key :ci_sources_pipelines, :ci_pipelines, column: :source_pipeline_id
end
end
......@@ -316,6 +316,20 @@ ActiveRecord::Schema.define(version: 20170602003304) do
add_index "ci_pipeline_schedules", ["next_run_at", "active"], name: "index_ci_pipeline_schedules_on_next_run_at_and_active", using: :btree
add_index "ci_pipeline_schedules", ["project_id"], name: "index_ci_pipeline_schedules_on_project_id", using: :btree
create_table "ci_sources_pipelines", force: :cascade do |t|
t.integer "project_id"
t.integer "pipeline_id"
t.integer "source_project_id"
t.integer "source_job_id"
t.integer "source_pipeline_id"
end
add_index "ci_sources_pipelines", ["pipeline_id"], name: "index_ci_pipeline_source_pipelines_on_pipeline_id", using: :btree
add_index "ci_sources_pipelines", ["project_id"], name: "index_ci_pipeline_source_pipelines_on_project_id", using: :btree
add_index "ci_sources_pipelines", ["source_job_id"], name: "index_ci_pipeline_source_pipelines_on_source_job_id", using: :btree
add_index "ci_sources_pipelines", ["source_pipeline_id"], name: "index_ci_pipeline_source_pipelines_on_source_pipeline_id", using: :btree
add_index "ci_sources_pipelines", ["source_project_id"], name: "index_ci_pipeline_source_pipelines_on_source_project_id", using: :btree
create_table "ci_pipelines", force: :cascade do |t|
t.string "ref"
t.string "sha"
......@@ -1681,6 +1695,11 @@ ActiveRecord::Schema.define(version: 20170602003304) do
add_foreign_key "ci_builds", "ci_pipelines", column: "auto_canceled_by_id", name: "fk_a2141b1522", on_delete: :nullify
add_foreign_key "ci_pipeline_schedules", "projects", name: "fk_8ead60fcc4", on_delete: :cascade
add_foreign_key "ci_pipeline_schedules", "users", column: "owner_id", name: "fk_9ea99f58d2", on_delete: :nullify
add_foreign_key "ci_sources_pipelines", "ci_builds", column: "source_job_id", name: "fk_3f0c88d7dc", on_delete: :cascade
add_foreign_key "ci_sources_pipelines", "ci_pipelines", column: "pipeline_id", name: "fk_b8c0fac459", on_delete: :cascade
add_foreign_key "ci_sources_pipelines", "ci_pipelines", column: "source_pipeline_id", name: "fk_3a3e3cb83a", on_delete: :cascade
add_foreign_key "ci_sources_pipelines", "projects", column: "source_project_id", name: "fk_8868d0f3e4", on_delete: :cascade
add_foreign_key "ci_sources_pipelines", "projects", name: "fk_83b4346e48", on_delete: :cascade
add_foreign_key "ci_pipelines", "ci_pipeline_schedules", column: "pipeline_schedule_id", name: "fk_3d34ab2e06", on_delete: :nullify
add_foreign_key "ci_pipelines", "ci_pipelines", column: "auto_canceled_by_id", name: "fk_262d4c2d19", on_delete: :nullify
add_foreign_key "ci_trigger_requests", "ci_triggers", column: "trigger_id", name: "fk_b8ec8b7245", on_delete: :cascade
......
......@@ -108,6 +108,10 @@ pipelines:
- artifacts
- pipeline_schedule
- merge_requests
- source_pipeline
- sourced_pipelines
- triggered_by_pipeline
- triggered_pipelines
statuses:
- project
- pipeline
......@@ -263,6 +267,8 @@ project:
- container_repositories
- uploads
- mirror_data
- source_pipelines
- sourced_pipelines
award_emoji:
- awardable
- user
......
......@@ -113,7 +113,7 @@ describe PipelineSerializer do
it "verifies number of queries" do
recorded = ActiveRecord::QueryRecorder.new { subject }
expect(recorded.count).to be_within(1).of(61)
expect(recorded.count).to be_within(1).of(62)
expect(recorded.cached_count).to eq(0)
end
......
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