Skip to content

Commit

Permalink
Move notifier package out of deploy package (#609)
Browse files Browse the repository at this point in the history
PR here is to begin extracting out some of the logic that should be
shared by both the PR and Deploy workflows. This does the following:

- Moves notifier pkg out of deploy pkg
- Creates an Info struct in the Notifier pkg to replace using the
deploy-based concept (i.e. `DeploymentInfo`) for the Notify interface
(this is only a change for the Github CheckRun notifier)
- Turn `BuildCheckRunTitle` to a fxn that supports passing in both the
workflow mode and root name

Combined, these changes allow us to create a new state receiver that can
process pr mode generated state events from the TF workflow. These
events can be defined as the now generic `notifier.Info` objects used by
the Github CheckRun notifier to update the commit's atlantis/pr check
run. Can't think of a better way to approach this, so feel free to offer
suggestions if you have any.
  • Loading branch information
samrabelachew authored Apr 19, 2023
1 parent d527dbc commit 4cf5f41
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package queue
import (
"context"
"fmt"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"

key "github.com/runatlantis/atlantis/server/neptune/context"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/deployment"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
terraformWorkflow "github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/version"
Expand Down Expand Up @@ -179,7 +179,7 @@ func (p *Deployer) updateCheckRun(ctx workflow.Context, deployRequest terraformW
})

request := notifier.GithubCheckRunRequest{
Title: terraformWorkflow.BuildCheckRunTitle(deployRequest.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(deployRequest.Root.Name),
Sha: deployRequest.Commit.Revision,
State: state,
Repo: deployRequest.Repo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"
"testing"
"time"

Expand All @@ -12,7 +13,6 @@ import (
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/deployment"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
model "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision/queue"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/version"
Expand Down Expand Up @@ -400,7 +400,7 @@ func TestDeployer_CompareCommit_SkipDeploy(t *testing.T) {
Info: deploymentInfo,
LatestDeploy: latestDeployedRevision,
ExpectedGHRequest: notifier.GithubCheckRunRequest{
Title: terraform.BuildCheckRunTitle(deploymentInfo.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(deploymentInfo.Root.Name),
State: github.CheckRunFailure,
Repo: repo,
Summary: queue.DirectionBehindSummary,
Expand Down Expand Up @@ -439,7 +439,7 @@ func TestDeployer_CompareCommit_SkipDeploy(t *testing.T) {
Info: deploymentInfo,
LatestDeploy: latestDeployedRevision,
ExpectedGHRequest: notifier.GithubCheckRunRequest{
Title: terraform.BuildCheckRunTitle(deploymentInfo.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(deploymentInfo.Root.Name),
State: github.CheckRunFailure,
Repo: repo,
Summary: queue.RerunNotIdenticalSummary,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package queue
import (
"fmt"
key "github.com/runatlantis/atlantis/server/neptune/context"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"

"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
"go.temporal.io/sdk/workflow"
)

Expand Down Expand Up @@ -35,7 +34,7 @@ func (u *LockStateUpdater) UpdateQueuedRevisions(ctx workflow.Context, queue *De

for _, i := range infos {
request := notifier.GithubCheckRunRequest{
Title: terraform.BuildCheckRunTitle(i.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(i.Root.Name),
Sha: i.Commit.Revision,
State: state,
Repo: i.Repo,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package queue_test

import (
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"
"testing"
"time"

"github.com/google/uuid"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
tfActivity "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision/queue"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/metrics"
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestLockStateUpdater_unlocked_new_version(t *testing.T) {
}

updateCheckRunRequest := activities.UpdateCheckRunRequest{
Title: terraform.BuildCheckRunTitle(info.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(info.Root.Name),
State: github.CheckRunQueued,
Repo: info.Repo,
ID: info.CheckRunID,
Expand All @@ -65,7 +65,7 @@ func TestLockStateUpdater_unlocked_new_version(t *testing.T) {
env.ExecuteWorkflow(testUpdaterWorkflow, updaterReq{
Queue: []terraform.DeploymentInfo{info},
ExpectedRequest: notifier.GithubCheckRunRequest{
Title: terraform.BuildCheckRunTitle(info.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(info.Root.Name),
State: github.CheckRunQueued,
Repo: info.Repo,
Sha: info.Commit.Revision,
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestLockStateUpdater_locked_new_version(t *testing.T) {
}

updateCheckRunRequest := activities.UpdateCheckRunRequest{
Title: terraform.BuildCheckRunTitle(info.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(info.Root.Name),
State: github.CheckRunActionRequired,
Repo: info.Repo,
ID: info.CheckRunID,
Expand All @@ -122,7 +122,7 @@ func TestLockStateUpdater_locked_new_version(t *testing.T) {
Revision: "1234",
},
ExpectedRequest: notifier.GithubCheckRunRequest{
Title: terraform.BuildCheckRunTitle(info.Root.Name),
Title: notifier.BuildDeployCheckRunTitle(info.Root.Name),
State: github.CheckRunActionRequired,
Repo: info.Repo,
Summary: "This deploy is locked from a manual deployment for revision [1234](https://github.com/some-org/some-repo/commit/1234). Unlock to proceed.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package queue

import (
"fmt"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"

key "github.com/runatlantis/atlantis/server/neptune/context"

Expand All @@ -11,7 +12,6 @@ import (
internalContext "github.com/runatlantis/atlantis/server/neptune/context"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/deployment"
tfModel "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/metrics"
"github.com/runatlantis/atlantis/server/neptune/workflows/plugins"
Expand Down Expand Up @@ -91,6 +91,7 @@ func NewWorker(
notifiers := []terraform.WorkflowNotifier{
&notifier.CheckRunNotifier{
CheckRunSessionCache: githubCheckRunCache,
Mode: tfModel.Deploy,
},
}

Expand Down
4 changes: 2 additions & 2 deletions server/neptune/workflows/internal/deploy/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package revision
import (
"context"
"fmt"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"

"github.com/runatlantis/atlantis/server/events/metrics"
key "github.com/runatlantis/atlantis/server/neptune/context"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/runatlantis/atlantis/server/neptune/workflows/activities"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
activity "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/request"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/request/converter"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision/queue"
Expand Down Expand Up @@ -146,7 +146,7 @@ func (n *Receiver) createCheckRun(ctx workflow.Context, id, revision string, roo
}

cid, err := n.checkRunClient.CreateOrUpdate(ctx, id, notifier.GithubCheckRunRequest{
Title: terraform.BuildCheckRunTitle(root.Name),
Title: notifier.BuildDeployCheckRunTitle(root.Name),
Sha: revision,
Repo: repo,
Summary: summary,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package revision_test

import (
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"
"testing"
"time"

"github.com/google/uuid"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/request"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision/queue"
Expand Down
11 changes: 8 additions & 3 deletions server/neptune/workflows/internal/deploy/terraform/deployment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package terraform

import (
"fmt"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"

"github.com/runatlantis/atlantis/server/neptune/workflows/activities/deployment"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
Expand Down Expand Up @@ -49,6 +49,11 @@ func (i DeploymentInfo) BuildPersistableInfo() *deployment.Info {
}
}

func BuildCheckRunTitle(rootName string) string {
return fmt.Sprintf("atlantis/deploy: %s", rootName)
func (i DeploymentInfo) ToInternalInfo() notifier.Info {
return notifier.Info{
ID: i.ID,
Commit: i.Commit,
RootName: i.Root.Name,
Repo: i.Repo,
}
}
5 changes: 3 additions & 2 deletions server/neptune/workflows/internal/deploy/terraform/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package terraform
import (
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/metrics"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"

"github.com/runatlantis/atlantis/server/neptune/workflows/internal/terraform/state"
"github.com/runatlantis/atlantis/server/neptune/workflows/plugins"
"go.temporal.io/sdk/workflow"
)

type WorkflowNotifier interface {
Notify(workflow.Context, DeploymentInfo, *state.Workflow) error
Notify(workflow.Context, notifier.Info, *state.Workflow) error
}

type StateReceiver struct {
Expand Down Expand Up @@ -39,7 +40,7 @@ func (n *StateReceiver) Receive(ctx workflow.Context, c workflow.ReceiveChannel,
}

for _, notifier := range n.InternalNotifiers {
if err := notifier.Notify(ctx, deploymentInfo, workflowState); err != nil {
if err := notifier.Notify(ctx, deploymentInfo.ToInternalInfo(), workflowState); err != nil {
workflow.GetMetricsHandler(ctx).Counter("notifier_failure").Inc(1)
workflow.GetLogger(ctx).Error(errors.Wrap(err, "notifying workflow state change").Error())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terraform_test

import (
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"
"net/url"
"testing"
"time"
Expand All @@ -17,13 +18,13 @@ import (
)

type testNotifier struct {
expectedInfo internalTerraform.DeploymentInfo
expectedInfo notifier.Info
expectedState *state.Workflow
expectedT *testing.T
called bool
}

func (n *testNotifier) Notify(ctx workflow.Context, info internalTerraform.DeploymentInfo, s *state.Workflow) error {
func (n *testNotifier) Notify(ctx workflow.Context, info notifier.Info, s *state.Workflow) error {
assert.Equal(n.expectedT, n.expectedInfo, info)
assert.Equal(n.expectedT, n.expectedState, s)

Expand Down Expand Up @@ -66,7 +67,7 @@ func testStateReceiveWorkflow(ctx workflow.Context, r stateReceiveRequest) (stat
ch := workflow.NewChannel(ctx)

notifier := &testNotifier{
expectedInfo: r.DeploymentInfo,
expectedInfo: r.DeploymentInfo.ToInternalInfo(),
expectedState: r.State,
expectedT: r.T,
}
Expand Down
2 changes: 1 addition & 1 deletion server/neptune/workflows/internal/deploy/workflow.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package deploy

import (
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"
"time"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision/queue"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package notifier

import (
"context"

"fmt"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github/markdown"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/terraform/state"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
Expand Down Expand Up @@ -128,21 +129,36 @@ type checkRunClient interface {

type CheckRunNotifier struct {
CheckRunSessionCache checkRunClient
Mode terraform.WorkflowMode
}

type Info struct {
ID uuid.UUID
Commit github.Commit
RootName string
Repo github.Repo
}

func (n *CheckRunNotifier) Notify(ctx workflow.Context, deploymentInfo terraform.DeploymentInfo, workflowState *state.Workflow) error {
return errors.Wrap(n.updateCheckRun(ctx, workflowState, deploymentInfo), "updating check run")
func (n *CheckRunNotifier) Notify(ctx workflow.Context, info Info, workflowState *state.Workflow) error {
return errors.Wrap(n.updateCheckRun(ctx, workflowState, info), "updating check run")
}

func (n *CheckRunNotifier) updateCheckRun(ctx workflow.Context, workflowState *state.Workflow, deploymentInfo terraform.DeploymentInfo) error {
func (n *CheckRunNotifier) updateCheckRun(ctx workflow.Context, workflowState *state.Workflow, info Info) error {
summary := markdown.RenderWorkflowStateTmpl(workflowState)
checkRunState := determineCheckRunState(workflowState)

var title string
if n.Mode == terraform.Deploy {
title = BuildDeployCheckRunTitle(info.RootName)
} else {
title = BuildPlanCheckRunTitle(info.RootName)
}

request := GithubCheckRunRequest{
Title: terraform.BuildCheckRunTitle(deploymentInfo.Root.Name),
Sha: deploymentInfo.Commit.Revision,
Title: title,
Sha: info.Commit.Revision,
State: checkRunState,
Repo: deploymentInfo.Repo,
Repo: info.Repo,
Summary: summary,
}

Expand All @@ -160,7 +176,7 @@ func (n *CheckRunNotifier) updateCheckRun(ctx workflow.Context, workflowState *s
})
}

_, err := n.CheckRunSessionCache.CreateOrUpdate(ctx, deploymentInfo.ID.String(), request)
_, err := n.CheckRunSessionCache.CreateOrUpdate(ctx, info.ID.String(), request)
return err
}

Expand Down Expand Up @@ -196,3 +212,11 @@ func determineCheckRunState(workflowState *state.Workflow) github.CheckRunState
func waitingForActionOn(job *state.Job) bool {
return job != nil && job.Status == state.WaitingJobStatus && len(job.OnWaitingActions.Actions) > 0
}

func BuildDeployCheckRunTitle(rootName string) string {
return fmt.Sprintf("atlantis/deploy: %s", rootName)
}

func BuildPlanCheckRunTitle(rootName string) string {
return fmt.Sprintf("atlantis/plan: %s", rootName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package notifier_test

import (
"context"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"
"testing"
"time"

"github.com/runatlantis/atlantis/server/neptune/workflows/activities"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/notifier"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.temporal.io/sdk/testsuite"
Expand Down
Loading

0 comments on commit 4cf5f41

Please sign in to comment.