Skip to content

Commit

Permalink
add revisions on queue to checkrun summary
Browse files Browse the repository at this point in the history
  • Loading branch information
tlin4194 committed Oct 25, 2024
1 parent 5368355 commit 4a5c4a0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
12 changes: 12 additions & 0 deletions server/neptune/workflows/internal/deploy/revision/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package queue
import (
"container/list"
"fmt"
"strings"

activity "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
Expand Down Expand Up @@ -85,6 +86,17 @@ func (q *Deploy) Push(msg terraform.DeploymentInfo) {
q.queue.Push(msg, Low)
}

func (q *Deploy) GetQueuedRevisionsSummary() string {
var revisions []string
if q.IsEmpty() {
return "No other revisions ahead In queue."
}
for _, deploy := range q.Scan() {
revisions = append(revisions, deploy.Commit.Revision)
}
return fmt.Sprintf("Revisions in queue: %s", strings.Join(revisions, ", "))
}

// priority is a simple 2 priority queue implementation
// priority is determined before an item enters a queue and does not change
type priority struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package queue

import (
"fmt"

key "github.com/runatlantis/atlantis/server/neptune/context"
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/notifier"

Expand All @@ -24,12 +25,13 @@ func (u *LockStateUpdater) UpdateQueuedRevisions(ctx workflow.Context, queue *De

var actions []github.CheckRunAction
var summary string
var revisionsSummary string = queue.GetQueuedRevisionsSummary()
state := github.CheckRunQueued
if lock.Status == LockedStatus {
actions = append(actions, github.CreateUnlockAction())
state = github.CheckRunActionRequired
revisionLink := github.BuildRevisionURLMarkdown(repoFullName, lock.Revision)
summary = fmt.Sprintf("This deploy is locked from a manual deployment for revision %s. Unlock to proceed.", revisionLink)
summary = fmt.Sprintf("This deploy is locked from a manual deployment for revision %s. Unlock to proceed.\n%s", revisionLink, revisionsSummary)
}

for _, i := range infos {
Expand Down
6 changes: 4 additions & 2 deletions server/neptune/workflows/internal/deploy/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Queue interface {
GetLockState() queue.LockState
SetLockForMergedItems(ctx workflow.Context, state queue.LockState)
Scan() []terraform.DeploymentInfo
GetQueuedRevisionsSummary() string
}

type DeploymentStore interface {
Expand Down Expand Up @@ -136,14 +137,15 @@ func (n *Receiver) Receive(c workflow.ReceiveChannel, more bool) {
func (n *Receiver) createCheckRun(ctx workflow.Context, id, revision string, root activity.Root, repo github.Repo) int64 {
lock := n.queue.GetLockState()
var actions []github.CheckRunAction
summary := "This deploy is queued and will be processed as soon as possible."
var revisionsSummary string = n.queue.GetQueuedRevisionsSummary()
summary := "This deploy is queued and will be processed as soon as possible.\n" + revisionsSummary
state := github.CheckRunQueued

if lock.Status == queue.LockedStatus && (root.TriggerInfo.Type == activity.MergeTrigger) {
actions = append(actions, github.CreateUnlockAction())
state = github.CheckRunActionRequired
revisionLink := github.BuildRevisionURLMarkdown(repo.GetFullName(), lock.Revision)
summary = fmt.Sprintf("This deploy is locked from a manual deployment for revision %s. Unlock to proceed.", revisionLink)
summary = fmt.Sprintf("This deploy is locked from a manual deployment for revision %s. Unlock to proceed.\n%s", revisionLink, revisionsSummary)
}

cid, err := n.checkRunClient.CreateOrUpdate(ctx, id, notifier.GithubCheckRunRequest{
Expand Down
74 changes: 57 additions & 17 deletions server/neptune/workflows/internal/deploy/revision/revision_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package revision_test

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

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

"github.com/google/uuid"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
Expand All @@ -13,6 +16,7 @@ import (
"github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/revision/queue"
terraformWorkflow "github.com/runatlantis/atlantis/server/neptune/workflows/internal/deploy/terraform"
"github.com/stretchr/testify/assert"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/testsuite"
"go.temporal.io/sdk/workflow"
)
Expand All @@ -23,8 +27,11 @@ type testCheckRunClient struct {
}

func (t *testCheckRunClient) CreateOrUpdate(ctx workflow.Context, deploymentID string, request notifier.GithubCheckRunRequest) (int64, error) {
assert.Equal(t.expectedT, t.expectedRequest, request)

ok := assert.Equal(t.expectedT, t.expectedRequest, request)
if !ok {
return 1, temporal.NewApplicationError("failing workflow", "myType")
// t.expectedT.Exit("CreateOrUpdate had unexpected request")
}
return 1, nil
}

Expand All @@ -49,6 +56,21 @@ func (q *testQueue) SetLockForMergedItems(ctx workflow.Context, state queue.Lock
q.Lock = state
}

func (q *testQueue) IsEmpty() bool {
return q.Queue == nil || len(q.Queue) == 0
}

func (q *testQueue) GetQueuedRevisionsSummary() string {
var revisions []string
if q.IsEmpty() {
return "No other revisions ahead In queue."
}
for _, deploy := range q.Scan() {
revisions = append(revisions, deploy.Commit.Revision)
}
return fmt.Sprintf("Revisions in queue: %s", strings.Join(revisions, ", "))
}

type testWorker struct {
Current queue.CurrentDeployment
}
Expand Down Expand Up @@ -137,10 +159,11 @@ func TestEnqueue(t *testing.T) {
env.ExecuteWorkflow(testWorkflow, req{
ID: id,
ExpectedRequest: notifier.GithubCheckRunRequest{
Title: "atlantis/deploy: root",
Sha: rev,
Repo: github.Repo{Name: "nish"},
State: github.CheckRunQueued,
Title: "atlantis/deploy: root",
Sha: rev,
Repo: github.Repo{Name: "nish"},
State: github.CheckRunQueued,
Summary: "This deploy is queued and will be processed as soon as possible.\nNo other revisions ahead In queue.",
},
ExpectedT: t,
})
Expand Down Expand Up @@ -197,10 +220,11 @@ func TestEnqueue_ManualTrigger(t *testing.T) {
env.ExecuteWorkflow(testWorkflow, req{
ID: id,
ExpectedRequest: notifier.GithubCheckRunRequest{
Title: "atlantis/deploy: root",
Sha: rev,
Repo: github.Repo{Name: "nish"},
State: github.CheckRunQueued,
Title: "atlantis/deploy: root",
Sha: rev,
Repo: github.Repo{Name: "nish"},
State: github.CheckRunQueued,
Summary: "This deploy is queued and will be processed as soon as possible.\nNo other revisions ahead In queue.",
},
ExpectedT: t,
})
Expand Down Expand Up @@ -263,10 +287,11 @@ func TestEnqueue_ManualTrigger_QueueAlreadyLocked(t *testing.T) {
Revision: "123334444555",
},
ExpectedRequest: notifier.GithubCheckRunRequest{
Title: "atlantis/deploy: root",
Sha: rev,
Repo: github.Repo{Name: "nish"},
State: github.CheckRunQueued,
Title: "atlantis/deploy: root",
Sha: rev,
Repo: github.Repo{Name: "nish"},
State: github.CheckRunQueued,
Summary: "This deploy is queued and will be processed as soon as possible.\nNo other revisions ahead In queue.",
},
ExpectedT: t,
})
Expand Down Expand Up @@ -321,8 +346,22 @@ func TestEnqueue_MergeTrigger_QueueAlreadyLocked(t *testing.T) {

id := uuid.Must(uuid.NewUUID())

deploymentInfo := terraformWorkflow.DeploymentInfo{
Commit: github.Commit{
Revision: "123334444555",
Branch: "locking-branch",
},
CheckRunID: 0,
Root: terraform.Root{Name: "root", TriggerInfo: terraform.TriggerInfo{
Type: terraform.MergeTrigger,
}, Trigger: terraform.MergeTrigger},
ID: id,
Repo: github.Repo{Name: "nish"},
}

env.ExecuteWorkflow(testWorkflow, req{
ID: id,
ID: id,
InitialElements: []terraformWorkflow.DeploymentInfo{deploymentInfo},
Lock: queue.LockState{
// ensure that the lock gets updated
Status: queue.LockedStatus,
Expand All @@ -332,7 +371,7 @@ func TestEnqueue_MergeTrigger_QueueAlreadyLocked(t *testing.T) {
Title: "atlantis/deploy: root",
Sha: rev,
Repo: github.Repo{Name: "nish"},
Summary: "This deploy is locked from a manual deployment for revision [123334444555](https://github.com//nish/commit/123334444555). Unlock to proceed.",
Summary: "This deploy is locked from a manual deployment for revision [123334444555](https://github.com//nish/commit/123334444555). Unlock to proceed.\nRevisions in queue: 12333444455",
Actions: []github.CheckRunAction{github.CreateUnlockAction()},
State: github.CheckRunActionRequired,
},
Expand All @@ -346,6 +385,7 @@ func TestEnqueue_MergeTrigger_QueueAlreadyLocked(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, []terraformWorkflow.DeploymentInfo{
deploymentInfo,
{
Commit: github.Commit{
Revision: rev,
Expand Down

0 comments on commit 4a5c4a0

Please sign in to comment.