Skip to content

Commit

Permalink
feat(atc): support "set-pipeline: self".
Browse files Browse the repository at this point in the history
This is a follow-up change of PR#4708 to add "set-pipeline: self".
The RFC is concourse/rfcs#31.

Signed-off-by: Chao Li <[email protected]>
  • Loading branch information
evanchaoli committed Dec 6, 2019
1 parent c77d756 commit 6bc185b
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 196 deletions.
3 changes: 2 additions & 1 deletion atc/exec/set_pipeline_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"github.com/concourse/concourse/vars"
)

// SetPipelineStep sets a pipeline to current team. This step takes pipeline
// SetPipelineStep sets a pipeline to current team. If pipeline_name specified
// is "self", then it will self set the current pipeline. This step takes pipeline
// configure file and var files from some resource in the pipeline, like git.
type SetPipelineStep struct {
planID atc.PlanID
Expand Down
4 changes: 2 additions & 2 deletions atc/scheduler/buildstarter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type BuildStarter interface {
//go:generate counterfeiter . BuildFactory

type BuildFactory interface {
Create(atc.JobConfig, atc.ResourceConfigs, atc.VersionedResourceTypes, []db.BuildInput) (atc.Plan, error)
Create(db.Job, atc.ResourceConfigs, atc.VersionedResourceTypes, []db.BuildInput) (atc.Plan, error)
}

func NewBuildStarter(
Expand Down Expand Up @@ -182,7 +182,7 @@ func (s *buildStarter) tryStartNextPendingBuild(
})
}

plan, err := s.factory.Create(job.Config(), resourceConfigs, resourceTypes, buildInputs)
plan, err := s.factory.Create(job, resourceConfigs, resourceTypes, buildInputs)
if err != nil {
// Don't use ErrorBuild because it logs a build event, and this build hasn't started
if err = nextPendingBuild.Finish(db.BuildStatusErrored); err != nil {
Expand Down
18 changes: 9 additions & 9 deletions atc/scheduler/buildstarter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ var _ = Describe("BuildStarter", func() {
Expect(tryStartErr).NotTo(HaveOccurred())
})

Context("when creaing a build plan", func() {
Context("when creating a build plan", func() {
BeforeEach(func() {
job.GetNextBuildInputsReturns([]db.BuildInput{}, true, nil)
fakePipeline.CheckPausedReturns(false, nil)
Expand Down Expand Up @@ -471,8 +471,8 @@ var _ = Describe("BuildStarter", func() {

It("stops creating builds for job", func() {
Expect(fakeFactory.CreateCallCount()).To(Equal(1))
actualJobConfig, actualResourceConfigs, actualResourceTypes, actualBuildInputs := fakeFactory.CreateArgsForCall(0)
Expect(actualJobConfig).To(Equal(atc.JobConfig{Name: "some-job"}))
actualJob, actualResourceConfigs, actualResourceTypes, actualBuildInputs := fakeFactory.CreateArgsForCall(0)
Expect(actualJob).To(Equal(job))
Expect(actualResourceConfigs).To(Equal(atc.ResourceConfigs{{Name: "some-resource"}}))
Expect(actualResourceTypes).To(Equal(versionedResourceTypes))
Expect(actualBuildInputs).To(Equal([]db.BuildInput{{Name: "some-input"}}))
Expand Down Expand Up @@ -515,20 +515,20 @@ var _ = Describe("BuildStarter", func() {

It("creates build plans for all builds", func() {
Expect(fakeFactory.CreateCallCount()).To(Equal(3))
actualJobConfig, actualResourceConfigs, actualResourceTypes, actualBuildInputs := fakeFactory.CreateArgsForCall(0)
Expect(actualJobConfig).To(Equal(atc.JobConfig{Name: "some-job"}))
actualJob, actualResourceConfigs, actualResourceTypes, actualBuildInputs := fakeFactory.CreateArgsForCall(0)
Expect(actualJob).To(Equal(job))
Expect(actualResourceConfigs).To(Equal(atc.ResourceConfigs{{Name: "some-resource"}}))
Expect(actualResourceTypes).To(Equal(versionedResourceTypes))
Expect(actualBuildInputs).To(Equal([]db.BuildInput{{Name: "some-input"}}))

actualJobConfig, actualResourceConfigs, actualResourceTypes, actualBuildInputs = fakeFactory.CreateArgsForCall(1)
Expect(actualJobConfig).To(Equal(atc.JobConfig{Name: "some-job"}))
actualJob, actualResourceConfigs, actualResourceTypes, actualBuildInputs = fakeFactory.CreateArgsForCall(1)
Expect(actualJob).To(Equal(job))
Expect(actualResourceConfigs).To(Equal(atc.ResourceConfigs{{Name: "some-resource"}}))
Expect(actualResourceTypes).To(Equal(versionedResourceTypes))
Expect(actualBuildInputs).To(Equal([]db.BuildInput{{Name: "some-input"}}))

actualJobConfig, actualResourceConfigs, actualResourceTypes, actualBuildInputs = fakeFactory.CreateArgsForCall(2)
Expect(actualJobConfig).To(Equal(atc.JobConfig{Name: "some-job"}))
actualJob, actualResourceConfigs, actualResourceTypes, actualBuildInputs = fakeFactory.CreateArgsForCall(2)
Expect(actualJob).To(Equal(job))
Expect(actualResourceConfigs).To(Equal(atc.ResourceConfigs{{Name: "some-resource"}}))
Expect(actualResourceTypes).To(Equal(versionedResourceTypes))
Expect(actualBuildInputs).To(Equal([]db.BuildInput{{Name: "some-input"}}))
Expand Down
31 changes: 17 additions & 14 deletions atc/scheduler/factory/build_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var ErrResourceNotFound = errors.New("resource not found")
//go:generate counterfeiter . BuildFactory

type BuildFactory interface {
Create(atc.JobConfig, atc.ResourceConfigs, atc.VersionedResourceTypes, []db.BuildInput) (atc.Plan, error)
Create(db.Job, atc.ResourceConfigs, atc.VersionedResourceTypes, []db.BuildInput) (atc.Plan, error)
}

type buildFactory struct {
Expand All @@ -26,7 +26,7 @@ func NewBuildFactory(planFactory atc.PlanFactory) BuildFactory {
}

func (factory *buildFactory) Create(
job atc.JobConfig,
job db.Job,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
inputs []db.BuildInput,
Expand All @@ -38,20 +38,20 @@ func (factory *buildFactory) Create(

return factory.applyHooks(job, constructionParams{
plan: plan,
hooks: job.Hooks(),
hooks: job.Config().Hooks(),
resources: resources,
resourceTypes: resourceTypes,
inputs: inputs,
})
}

func (factory *buildFactory) constructPlanFromJob(
job atc.JobConfig,
job db.Job,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
inputs []db.BuildInput,
) (atc.Plan, error) {
planSequence := job.Plan
planSequence := job.Config().Plan

if len(planSequence) == 1 {
return factory.constructPlanFromConfig(
Expand All @@ -67,7 +67,7 @@ func (factory *buildFactory) constructPlanFromJob(
}

func (factory *buildFactory) do(
job atc.JobConfig,
job db.Job,
planSequence atc.PlanSequence,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
Expand Down Expand Up @@ -95,7 +95,7 @@ func (factory *buildFactory) do(
}

func (factory *buildFactory) constructPlanFromConfig(
job atc.JobConfig,
job db.Job,
planConfig atc.PlanConfig,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
Expand Down Expand Up @@ -134,7 +134,7 @@ func (factory *buildFactory) constructPlanFromConfig(
}

func (factory *buildFactory) constructUnhookedPlan(
job atc.JobConfig,
job db.Job,
planConfig atc.PlanConfig,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
Expand Down Expand Up @@ -251,6 +251,9 @@ func (factory *buildFactory) constructUnhookedPlan(

case planConfig.SetPipeline != "":
name := planConfig.SetPipeline
if name == "self" {
name = job.PipelineName()
}
plan = factory.planFactory.NewPlan(atc.SetPipelinePlan{
Name: name,
File: planConfig.ConfigPath,
Expand Down Expand Up @@ -337,7 +340,7 @@ type constructionParams struct {
inputs []db.BuildInput
}

func (factory *buildFactory) applyHooks(job atc.JobConfig, cp constructionParams) (atc.Plan, error) {
func (factory *buildFactory) applyHooks(job db.Job, cp constructionParams) (atc.Plan, error) {
var err error

cp, err = factory.abortIfPresent(job, cp)
Expand Down Expand Up @@ -368,7 +371,7 @@ func (factory *buildFactory) applyHooks(job atc.JobConfig, cp constructionParams
return cp.plan, nil
}

func (factory *buildFactory) successIfPresent(job atc.JobConfig, cp constructionParams) (constructionParams, error) {
func (factory *buildFactory) successIfPresent(job db.Job, cp constructionParams) (constructionParams, error) {
if cp.hooks.Success != nil {

nextPlan, err := factory.constructPlanFromConfig(
Expand All @@ -390,7 +393,7 @@ func (factory *buildFactory) successIfPresent(job atc.JobConfig, cp construction
return cp, nil
}

func (factory *buildFactory) failureIfPresent(job atc.JobConfig, cp constructionParams) (constructionParams, error) {
func (factory *buildFactory) failureIfPresent(job db.Job, cp constructionParams) (constructionParams, error) {
if cp.hooks.Failure != nil {
nextPlan, err := factory.constructPlanFromConfig(
job,
Expand All @@ -412,7 +415,7 @@ func (factory *buildFactory) failureIfPresent(job atc.JobConfig, cp construction
return cp, nil
}

func (factory *buildFactory) ensureIfPresent(job atc.JobConfig, cp constructionParams) (constructionParams, error) {
func (factory *buildFactory) ensureIfPresent(job db.Job, cp constructionParams) (constructionParams, error) {
if cp.hooks.Ensure != nil {
nextPlan, err := factory.constructPlanFromConfig(
job,
Expand All @@ -433,7 +436,7 @@ func (factory *buildFactory) ensureIfPresent(job atc.JobConfig, cp constructionP
return cp, nil
}

func (factory *buildFactory) abortIfPresent(job atc.JobConfig, cp constructionParams) (constructionParams, error) {
func (factory *buildFactory) abortIfPresent(job db.Job, cp constructionParams) (constructionParams, error) {
if cp.hooks.Abort != nil {
nextPlan, err := factory.constructPlanFromConfig(
job,
Expand All @@ -455,7 +458,7 @@ func (factory *buildFactory) abortIfPresent(job atc.JobConfig, cp constructionPa
return cp, nil
}

func (factory *buildFactory) errorIfPresent(job atc.JobConfig, cp constructionParams) (constructionParams, error) {
func (factory *buildFactory) errorIfPresent(job db.Job, cp constructionParams) (constructionParams, error) {
if cp.hooks.Error != nil {
nextPlan, err := factory.constructPlanFromConfig(
job,
Expand Down
41 changes: 29 additions & 12 deletions atc/scheduler/factory/factory_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package factory_test

import (
"github.com/concourse/concourse/atc"
"github.com/concourse/concourse/atc/db/dbfakes"
"github.com/concourse/concourse/atc/scheduler/factory"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -15,6 +16,8 @@ var _ = Describe("Factory Aggregate", func() {
resourceTypes atc.VersionedResourceTypes
actualPlanFactory atc.PlanFactory
expectedPlanFactory atc.PlanFactory

fakeJob *dbfakes.FakeJob
)

BeforeEach(func() {
Expand All @@ -41,11 +44,13 @@ var _ = Describe("Factory Aggregate", func() {
Version: atc.Version{"some": "version"},
},
}

fakeJob = new(dbfakes.FakeJob)
})

Context("when I have one aggregate", func() {
It("returns the correct plan", func() {
actual, err := buildFactory.Create(atc.JobConfig{
BeforeEach(func() {
fakeJob.ConfigReturns(atc.JobConfig{
Plan: atc.PlanSequence{
{
Aggregate: &atc.PlanSequence{
Expand All @@ -58,7 +63,10 @@ var _ = Describe("Factory Aggregate", func() {
},
},
},
}, resources, resourceTypes, nil)
})
})
It("returns the correct plan", func() {
actual, err := buildFactory.Create(fakeJob, resources, resourceTypes, nil)
Expect(err).NotTo(HaveOccurred())

expected := expectedPlanFactory.NewPlan(atc.AggregatePlan{
Expand All @@ -76,8 +84,8 @@ var _ = Describe("Factory Aggregate", func() {
})

Context("when I have nested aggregates", func() {
It("returns the correct plan", func() {
actual, err := buildFactory.Create(atc.JobConfig{
BeforeEach(func() {
fakeJob.ConfigReturns(atc.JobConfig{
Plan: atc.PlanSequence{
{
Aggregate: &atc.PlanSequence{
Expand All @@ -97,7 +105,10 @@ var _ = Describe("Factory Aggregate", func() {
},
},
},
}, resources, resourceTypes, nil)
})
})
It("returns the correct plan", func() {
actual, err := buildFactory.Create(fakeJob, resources, resourceTypes, nil)
Expect(err).NotTo(HaveOccurred())

expected := expectedPlanFactory.NewPlan(atc.AggregatePlan{
Expand All @@ -121,8 +132,8 @@ var _ = Describe("Factory Aggregate", func() {
})

Context("when I have an aggregate with hooks", func() {
It("returns the correct plan", func() {
actual, err := buildFactory.Create(atc.JobConfig{
BeforeEach(func() {
fakeJob.ConfigReturns(atc.JobConfig{
Plan: atc.PlanSequence{
{
Aggregate: &atc.PlanSequence{
Expand All @@ -135,7 +146,10 @@ var _ = Describe("Factory Aggregate", func() {
},
},
},
}, resources, resourceTypes, nil)
})
})
It("returns the correct plan", func() {
actual, err := buildFactory.Create(fakeJob, resources, resourceTypes, nil)
Expect(err).NotTo(HaveOccurred())

expected := expectedPlanFactory.NewPlan(atc.AggregatePlan{
Expand All @@ -155,8 +169,8 @@ var _ = Describe("Factory Aggregate", func() {
})

Context("when I have a hook on an aggregate", func() {
It("returns the correct plan", func() {
actual, err := buildFactory.Create(atc.JobConfig{
BeforeEach(func() {
fakeJob.ConfigReturns(atc.JobConfig{
Plan: atc.PlanSequence{
{
Aggregate: &atc.PlanSequence{
Expand All @@ -169,7 +183,10 @@ var _ = Describe("Factory Aggregate", func() {
},
},
},
}, resources, resourceTypes, nil)
})
})
It("returns the correct plan", func() {
actual, err := buildFactory.Create(fakeJob, resources, resourceTypes, nil)
Expect(err).NotTo(HaveOccurred())

expected := expectedPlanFactory.NewPlan(atc.OnSuccessPlan{
Expand Down
Loading

0 comments on commit 6bc185b

Please sign in to comment.