diff --git a/pkg/jobutil/jobutil.go b/pkg/jobutil/jobutil.go index 466d1fbce..6d8002c3b 100644 --- a/pkg/jobutil/jobutil.go +++ b/pkg/jobutil/jobutil.go @@ -96,7 +96,7 @@ func createRefs(pr *scm.PullRequest, baseSHA string, prRefFmt string) v1alpha1.R // NewPresubmit converts a config.Presubmit into a builder.PipelineOptions. // The builder.Refs are configured correctly per the pr, baseSHA. // The eventGUID becomes a gitprovider.EventGUID label. -func NewPresubmit(logger *logrus.Entry, pr *scm.PullRequest, baseSHA string, job job.Presubmit, eventGUID string, prRefFmt string) v1alpha1.LighthouseJob { +func NewPresubmit(logger *logrus.Entry, pr *scm.PullRequest, baseSHA string, job job.Presubmit, eventGUID string, prRefFmt string) (v1alpha1.LighthouseJob, error) { refs := createRefs(pr, baseSHA, prRefFmt) labels := make(map[string]string) for k, v := range job.Labels { @@ -107,12 +107,19 @@ func NewPresubmit(logger *logrus.Entry, pr *scm.PullRequest, baseSHA string, job annotations[k] = v } labels[scmprovider.EventGUID] = eventGUID - return NewLighthouseJob(PresubmitSpec(logger, job, refs), labels, annotations) + preSubmitSpec, err := PresubmitSpec(logger, job, refs) + if err != nil { + return v1alpha1.LighthouseJob{}, err + } + return NewLighthouseJob(preSubmitSpec, labels, annotations), nil } // PresubmitSpec initializes a PipelineOptionsSpec for a given presubmit job. -func PresubmitSpec(logger *logrus.Entry, p job.Presubmit, refs v1alpha1.Refs) v1alpha1.LighthouseJobSpec { - pjs := specFromJobBase(logger, p.Base) +func PresubmitSpec(logger *logrus.Entry, p job.Presubmit, refs v1alpha1.Refs) (v1alpha1.LighthouseJobSpec, error) { + pjs, err := specFromJobBase(logger, p.Base) + if err != nil { + return pjs, err + } pjs.Type = job.PresubmitJob pjs.Context = p.Context pjs.RerunCommand = p.RerunCommand @@ -124,12 +131,12 @@ func PresubmitSpec(logger *logrus.Entry, p job.Presubmit, refs v1alpha1.Refs) v1 } } - return pjs + return pjs, nil } // PostsubmitSpec initializes a PipelineOptionsSpec for a given postsubmit job. func PostsubmitSpec(logger *logrus.Entry, p job.Postsubmit, refs v1alpha1.Refs) v1alpha1.LighthouseJobSpec { - pjs := specFromJobBase(logger, p.Base) + pjs, _ := specFromJobBase(logger, p.Base) pjs.Type = job.PostsubmitJob pjs.Context = p.Context pjs.Refs = completePrimaryRefs(refs, p.Base) @@ -145,7 +152,7 @@ func PostsubmitSpec(logger *logrus.Entry, p job.Postsubmit, refs v1alpha1.Refs) // PeriodicSpec initializes a PipelineOptionsSpec for a given periodic job. func PeriodicSpec(logger *logrus.Entry, p job.Periodic) v1alpha1.LighthouseJobSpec { - pjs := specFromJobBase(logger, p.Base) + pjs, _ := specFromJobBase(logger, p.Base) pjs.Type = job.PeriodicJob return pjs @@ -153,7 +160,7 @@ func PeriodicSpec(logger *logrus.Entry, p job.Periodic) v1alpha1.LighthouseJobSp // BatchSpec initializes a PipelineOptionsSpec for a given batch job and ref spec. func BatchSpec(logger *logrus.Entry, p job.Presubmit, refs v1alpha1.Refs) v1alpha1.LighthouseJobSpec { - pjs := specFromJobBase(logger, p.Base) + pjs, _ := specFromJobBase(logger, p.Base) pjs.Type = job.BatchJob pjs.Context = p.Context pjs.Refs = completePrimaryRefs(refs, p.Base) @@ -161,13 +168,22 @@ func BatchSpec(logger *logrus.Entry, p job.Presubmit, refs v1alpha1.Refs) v1alph return pjs } -func specFromJobBase(logger *logrus.Entry, jb job.Base) v1alpha1.LighthouseJobSpec { +func specFromJobBase(logger *logrus.Entry, jb job.Base) (v1alpha1.LighthouseJobSpec, error) { // if we have not yet loaded the PipelineRunSpec then lets do it now if jb.PipelineRunSpec == nil { logger = logger.WithField("JobName", jb.Name) err := jb.LoadPipeline(logger) if err != nil { logger.WithError(err).Warn("failed to lazy load the PipelineRunSpec") + return v1alpha1.LighthouseJobSpec{ + Agent: jb.Agent, + Job: jb.Name, + Namespace: *jb.Namespace, + MaxConcurrency: jb.MaxConcurrency, + PodSpec: jb.Spec, + PipelineRunSpec: jb.PipelineRunSpec, + PipelineRunParams: jb.PipelineRunParams, + }, err } } var namespace string @@ -182,7 +198,7 @@ func specFromJobBase(logger *logrus.Entry, jb job.Base) v1alpha1.LighthouseJobSp PodSpec: jb.Spec, PipelineRunSpec: jb.PipelineRunSpec, PipelineRunParams: jb.PipelineRunParams, - } + }, nil } func completePrimaryRefs(refs v1alpha1.Refs, jb job.Base) *v1alpha1.Refs { diff --git a/pkg/jobutil/jobutil_test.go b/pkg/jobutil/jobutil_test.go index a0a19b5bb..353c0c05f 100644 --- a/pkg/jobutil/jobutil_test.go +++ b/pkg/jobutil/jobutil_test.go @@ -205,7 +205,7 @@ func TestPresubmitSpec(t *testing.T) { } for _, tc := range tests { - actual := PresubmitSpec(logger, tc.p, tc.refs) + actual, _ := PresubmitSpec(logger, tc.p, tc.refs) if expected := tc.expected; !reflect.DeepEqual(actual, expected) { t.Errorf("%s: actual %#v != expected %#v", tc.name, actual, expected) } @@ -639,7 +639,7 @@ func TestSpecFromJobBase(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - pj := specFromJobBase(logger, tc.jobBase) + pj, _ := specFromJobBase(logger, tc.jobBase) if err := tc.verify(pj); err != nil { t.Fatalf("Verification failed: %v", err) } diff --git a/pkg/keeper/keeper.go b/pkg/keeper/keeper.go index afd026da1..7ca4fddfe 100644 --- a/pkg/keeper/keeper.go +++ b/pkg/keeper/keeper.go @@ -1205,7 +1205,7 @@ func (c *DefaultController) trigger(sp subpool, presubmits map[int][]job.Presubm triggeredContexts.Insert(ps.Context) var spec v1alpha1.LighthouseJobSpec if len(prs) == 1 { - spec = jobutil.PresubmitSpec(c.logger, ps, refs) + spec, _ = jobutil.PresubmitSpec(c.logger, ps, refs) } else { spec = jobutil.BatchSpec(c.logger, ps, refs) } diff --git a/pkg/plugins/override/override.go b/pkg/plugins/override/override.go index 95e832b70..e0aa19717 100644 --- a/pkg/plugins/override/override.go +++ b/pkg/plugins/override/override.go @@ -206,7 +206,7 @@ Only the following contexts were expected: return spc.CreateComment(org, repo, number, e.IsPR, plugins.FormatResponseRaw(e.Body, e.Link, spc.QuoteAuthorForComment(user), resp)) } - pj := jobutil.NewPresubmit(log, pr, baseSHA, *pre, e.GUID, spc.PRRefFmt()) + pj, _ := jobutil.NewPresubmit(log, pr, baseSHA, *pre, e.GUID, spc.PRRefFmt()) now := metav1.Now() pj.Status = v1alpha1.LighthouseJobStatus{ State: v1alpha1.SuccessState, diff --git a/pkg/plugins/trigger/trigger.go b/pkg/plugins/trigger/trigger.go index 2447b26ba..e4280700c 100644 --- a/pkg/plugins/trigger/trigger.go +++ b/pkg/plugins/trigger/trigger.go @@ -281,7 +281,10 @@ func runRequested(c Client, pr *scm.PullRequest, requestedJobs []job.Presubmit, var errors []error for _, job := range requestedJobs { c.Logger.Infof("Starting %s build.", job.Name) - pj := jobutil.NewPresubmit(c.Logger, pr, baseSHA, job, eventGUID, c.SCMProviderClient.PRRefFmt()) + pj, err := jobutil.NewPresubmit(c.Logger, pr, baseSHA, job, eventGUID, c.SCMProviderClient.PRRefFmt()) + if err != nil { + errors = append(errors, err) + } c.Logger.WithFields(jobutil.LighthouseJobFields(&pj)).Info("Creating a new LighthouseJob.") if _, err := c.LauncherClient.Launch(&pj); err != nil { c.Logger.WithError(err).Error("Failed to create LighthouseJob.") @@ -291,6 +294,17 @@ func runRequested(c Client, pr *scm.PullRequest, requestedJobs []job.Presubmit, } } } + + if len(errors) > 0 { + org, repo, _ := orgRepoAuthor(*pr) + errorString := "" + for k := range errors { + errorString += "*" + errors[k].Error() + "\n" + } + message := "failed to trigger Pull Request pipeline\n" + errorString + c.SCMProviderClient.CreateComment(org, repo, pr.Number, true, message) + } + return errorutil.NewAggregate(errors...) } diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 70c11c23a..5c59f1160 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -526,7 +526,7 @@ func (o *WebhooksController) createHookServer() (*Server, error) { Metrics: promMetrics, ServerURL: serverURL, InRepoCache: cache, - //TokenGenerator: secretAgent.GetTokenGenerator(o.webhookSecretFile), + // TokenGenerator: secretAgent.GetTokenGenerator(o.webhookSecretFile), } return server, nil }