From 0e48ff9a1748b5890fbb20291dd538f96af18339 Mon Sep 17 00:00:00 2001 From: Nish Krishnan Date: Wed, 19 Apr 2023 13:30:10 -0700 Subject: [PATCH] Add TriggerInfo to gateway request population. (#610) This is necessary as our workflows need to differentiate between force applies and normal applies (for auditing). Also this data model makes a bit more sense than scattering rerun and force in the request itself. --- server/neptune/gateway/api/deploy.go | 4 +++- server/neptune/gateway/deploy/deployer.go | 3 +-- server/neptune/gateway/deploy/signaler.go | 7 +++++-- .../neptune/gateway/deploy/signaler_test.go | 21 ++++++++++++++++--- .../gateway/event/check_run_handler.go | 6 ++++-- .../gateway/event/check_suite_handler.go | 6 ++++-- .../neptune/gateway/event/comment_handler.go | 6 +++++- .../gateway/event/comment_handler_test.go | 10 +++++++-- .../gateway/event/push_event_handler.go | 4 +++- server/neptune/workflows/deploy.go | 1 + .../workflows/internal/deploy/request/root.go | 14 +++++++++++-- 11 files changed, 64 insertions(+), 18 deletions(-) diff --git a/server/neptune/gateway/api/deploy.go b/server/neptune/gateway/api/deploy.go index dc3e18d8e..549262d19 100644 --- a/server/neptune/gateway/api/deploy.go +++ b/server/neptune/gateway/api/deploy.go @@ -51,7 +51,9 @@ func (c *DeployHandler) Handle(ctx context.Context, r request.Deploy) error { CloneDepth: 1, }, - Trigger: workflows.ManualTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.ManualTrigger, + }, }) }, ) diff --git a/server/neptune/gateway/deploy/deployer.go b/server/neptune/gateway/deploy/deployer.go index 53c39d1b8..dfd1ad5a8 100644 --- a/server/neptune/gateway/deploy/deployer.go +++ b/server/neptune/gateway/deploy/deployer.go @@ -50,8 +50,7 @@ type RootDeployOptions struct { // TODO: Remove this from this struct, consumers shouldn't need to know about this // instead we should just inject implementations of RepoFetcher to handle different scenarios RepoFetcherOptions *github.RepoFetcherOptions - Trigger workflows.Trigger - Rerun bool + TriggerInfo workflows.DeployTriggerInfo } func (d *RootDeployer) Deploy(ctx context.Context, deployOptions RootDeployOptions) error { diff --git a/server/neptune/gateway/deploy/signaler.go b/server/neptune/gateway/deploy/signaler.go index 81d3c8848..0a6d21df5 100644 --- a/server/neptune/gateway/deploy/signaler.go +++ b/server/neptune/gateway/deploy/signaler.go @@ -61,8 +61,11 @@ func (d *WorkflowSignaler) SignalWithStartWorkflow(ctx context.Context, rootCfg TrackedFiles: rootCfg.WhenModified, TfVersion: tfVersion, PlanMode: d.generatePlanMode(rootCfg), - Trigger: rootDeployOptions.Trigger, - Rerun: rootDeployOptions.Rerun, + TriggerInfo: rootDeployOptions.TriggerInfo, + + // todo: remove once we stop using this in our workflows. + Trigger: rootDeployOptions.TriggerInfo.Type, + Rerun: rootDeployOptions.TriggerInfo.Rerun, }, Repo: workflows.Repo{ URL: repo.CloneURL, diff --git a/server/neptune/gateway/deploy/signaler_test.go b/server/neptune/gateway/deploy/signaler_test.go index ae0486544..5048be220 100644 --- a/server/neptune/gateway/deploy/signaler_test.go +++ b/server/neptune/gateway/deploy/signaler_test.go @@ -123,6 +123,9 @@ func TestSignalWithStartWorkflow_Success(t *testing.T) { TfVersion: version.String(), PlanMode: workflows.NormalPlanMode, Trigger: workflows.MergeTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.MergeTrigger, + }, }, InitiatingUser: workflows.User{ Name: user.Username, @@ -160,7 +163,9 @@ func TestSignalWithStartWorkflow_Success(t *testing.T) { Revision: sha, Branch: branch, Sender: user, - Trigger: workflows.MergeTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.MergeTrigger, + }, } run, err := deploySignaler.SignalWithStartWorkflow(context.Background(), &rootCfg, rootDeployOptions) assert.NoError(t, err) @@ -198,6 +203,9 @@ func TestSignalWithStartWorkflow_Success(t *testing.T) { TfVersion: version.String(), PlanMode: workflows.DestroyPlanMode, Trigger: workflows.MergeTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.MergeTrigger, + }, }, InitiatingUser: workflows.User{ Name: user.Username, @@ -238,7 +246,9 @@ func TestSignalWithStartWorkflow_Success(t *testing.T) { Revision: sha, Branch: branch, Sender: user, - Trigger: workflows.MergeTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.MergeTrigger, + }, } run, err := deploySignaler.SignalWithStartWorkflow(context.Background(), &rootCfg, rootDeployOptions) assert.NoError(t, err) @@ -294,6 +304,9 @@ func TestSignalWithStartWorkflow_Failure(t *testing.T) { TfVersion: version.String(), PlanMode: workflows.NormalPlanMode, Trigger: workflows.MergeTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.MergeTrigger, + }, }, InitiatingUser: workflows.User{ Name: user.Username, @@ -332,7 +345,9 @@ func TestSignalWithStartWorkflow_Failure(t *testing.T) { Revision: sha, Branch: branch, Sender: user, - Trigger: workflows.MergeTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.MergeTrigger, + }, } run, err := deploySignaler.SignalWithStartWorkflow(context.Background(), &rootCfg, rootDeployOptions) assert.Error(t, err) diff --git a/server/neptune/gateway/event/check_run_handler.go b/server/neptune/gateway/event/check_run_handler.go index be9976c69..abab0967c 100644 --- a/server/neptune/gateway/event/check_run_handler.go +++ b/server/neptune/gateway/event/check_run_handler.go @@ -167,8 +167,10 @@ func (h *CheckRunHandler) buildRoot(ctx context.Context, event CheckRun, rootNam Revision: event.HeadSha, Sender: event.User, InstallationToken: event.InstallationToken, - Trigger: workflows.ManualTrigger, - Rerun: true, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.ManualTrigger, + Rerun: true, + }, } return errors.Wrap(h.RootDeployer.Deploy(ctx, deployOptions), "deploying workflow") } diff --git a/server/neptune/gateway/event/check_suite_handler.go b/server/neptune/gateway/event/check_suite_handler.go index 8aadd5d7d..8c9c32a24 100644 --- a/server/neptune/gateway/event/check_suite_handler.go +++ b/server/neptune/gateway/event/check_suite_handler.go @@ -45,8 +45,10 @@ func (h *CheckSuiteHandler) handle(ctx context.Context, event CheckSuite) error Revision: event.HeadSha, Sender: event.Sender, InstallationToken: event.InstallationToken, - Trigger: workflows.ManualTrigger, - Rerun: true, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.ManualTrigger, + Rerun: true, + }, } return h.RootDeployer.Deploy(ctx, rootDeployOptions) } diff --git a/server/neptune/gateway/event/comment_handler.go b/server/neptune/gateway/event/comment_handler.go index df32ecbde..9e0fae13f 100644 --- a/server/neptune/gateway/event/comment_handler.go +++ b/server/neptune/gateway/event/comment_handler.go @@ -276,7 +276,11 @@ func (p *CommentEventWorkerProxy) forceApplyPlatformMode(ctx context.Context, ev OptionalPullNum: event.Pull.Num, Sender: event.User, InstallationToken: event.InstallationToken, - Trigger: workflows.ManualTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.ManualTrigger, + Force: true, + }, } + return p.rootDeployer.Deploy(ctx, rootDeployOptions) } diff --git a/server/neptune/gateway/event/comment_handler_test.go b/server/neptune/gateway/event/comment_handler_test.go index 814121766..a1738e7cd 100644 --- a/server/neptune/gateway/event/comment_handler_test.go +++ b/server/neptune/gateway/event/comment_handler_test.go @@ -169,7 +169,10 @@ func TestCommentEventWorkerProxy_HandleForceApply_BothModes(t *testing.T) { OptionalPullNum: testPull.Num, Sender: commentEvent.User, InstallationToken: commentEvent.InstallationToken, - Trigger: workflows.ManualTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.ManualTrigger, + Force: true, + }, }, } commentCreator := &mockCommentCreator{} @@ -243,7 +246,10 @@ func TestCommentEventWorkerProxy_HandleForceApply_AllPlatform(t *testing.T) { OptionalPullNum: testPull.Num, Sender: commentEvent.User, InstallationToken: commentEvent.InstallationToken, - Trigger: workflows.ManualTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.ManualTrigger, + Force: true, + }, }, } commentCreator := &mockCommentCreator{ diff --git a/server/neptune/gateway/event/push_event_handler.go b/server/neptune/gateway/event/push_event_handler.go index a9a2af503..de68915b7 100644 --- a/server/neptune/gateway/event/push_event_handler.go +++ b/server/neptune/gateway/event/push_event_handler.go @@ -85,7 +85,9 @@ func (p *PushHandler) handle(ctx context.Context, event Push) error { RepoFetcherOptions: &github.RepoFetcherOptions{ CloneDepth: 5, }, - Trigger: workflows.MergeTrigger, + TriggerInfo: workflows.DeployTriggerInfo{ + Type: workflows.MergeTrigger, + }, } return p.RootDeployer.Deploy(ctx, rootDeployOptions) } diff --git a/server/neptune/workflows/deploy.go b/server/neptune/workflows/deploy.go index 8e86e9e6f..158e60c9a 100644 --- a/server/neptune/workflows/deploy.go +++ b/server/neptune/workflows/deploy.go @@ -22,6 +22,7 @@ type AppCredentials = request.AppCredentials type User = request.User type PlanMode = request.PlanMode type Trigger = request.Trigger +type DeployTriggerInfo = request.TriggerInfo const DestroyPlanMode = request.DestroyPlanMode const NormalPlanMode = request.NormalPlanMode diff --git a/server/neptune/workflows/internal/deploy/request/root.go b/server/neptune/workflows/internal/deploy/request/root.go index 9291630c0..0776cf8b2 100644 --- a/server/neptune/workflows/internal/deploy/request/root.go +++ b/server/neptune/workflows/internal/deploy/request/root.go @@ -7,6 +7,12 @@ const ( NormalPlanMode PlanMode = "normal" ) +type TriggerInfo struct { + Type Trigger + Force bool + Rerun bool +} + type Trigger string const ( @@ -34,8 +40,12 @@ type Root struct { TfVersion string PlanMode PlanMode PlanApproval PlanApproval - Trigger Trigger - Rerun bool + TriggerInfo TriggerInfo + + // todo: keeping for backwards compatibility with existing workflows + // remove once we cutover to using TriggerInfo + Trigger Trigger + Rerun bool } type Job struct {