Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ func run(c *cli.Context) {
RedisClient: sysutil.Redis{Client: rdb},
Runner: &runner,
Log: logger.With("logger", "pr-planner"),
WebserverURL: c.String("oidc-callback-url"),
}

// setup subscription for key set
Expand Down
52 changes: 25 additions & 27 deletions prplanner/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,16 @@ var (
autoPlanDisabledTml = "Auto plan is disabled for this PR.\n" +
"Please post `@terraform-applier plan <module_name>` as comment if you want to request terraform plan for a particular module."

requestAcknowledgedMsgTml = "Received terraform plan request\n" +
"```\n" +
"Cluster: %s\n" +
"Module: %s\n" +
"Path: %s\n" +
"Commit ID: %s\n" +
"Requested At: %s\n" +
"```\n" +
"Do not edit this comment. This message will be updated once the plan run is completed.\n" +
"To manually trigger plan again please post `@terraform-applier plan %s` as comment."

runOutputMsgTml = "Terraform run output for\n" +
"```\n" +
"Cluster: %s\n" +
"Module: %s\n" +
"Path: %s\n" +
"Commit ID: %s\n" +
"```\n" +
requestAcknowledgedMsgTml = "### Received terraform plan request for `%s`\n" +
"🏷️ **Commit:** %s | 🕒 **Requested At:** %s | 🔗 [View in %s dashboard](%s)\n\n" +
"*(Do not edit this comment. This message will be updated once the plan run is completed.)*\n" +
">To manually trigger plan again please post `@terraform-applier plan %s` as comment."

runOutputMsgTml = "### Terraform Plan Output for `%s`\n" +
"🏷️ **Commit:** %s | 🔗 [View in %s dashboard](%s)\n\n" +
"> To manually trigger plan again please post `@terraform-applier plan %s` as comment.\n" +
"<details><summary><b>%s Run Status: %s, Run Summary: %s</b></summary>" +
"\n\n```terraform\n%s\n```\n</details>\n" +
"\n> To manually trigger plan again please post `@terraform-applier plan %s` as comment."
"\n\n```terraform\n%s\n```\n</details>\n"
)

type MsgType string
Expand Down Expand Up @@ -110,13 +99,15 @@ func parsePlanReqMsg(commentBody string) string {
return ""
}

func requestAcknowledgedMsg(cluster, module, path, commitID string, reqAt *metav1.Time) string {
display := fmt.Sprintf(requestAcknowledgedMsgTml, cluster, module, path, commitID, reqAt.Format(time.RFC3339), path)
func requestAcknowledgedMsg(cluster string, module types.NamespacedName, path, commitID string, reqAt *metav1.Time, webserverURL string) string {
moduleURL := webserverURL + "/#" + module.Namespace + "_" + module.Name

display := fmt.Sprintf(requestAcknowledgedMsgTml, module.Name, commitID, reqAt.Format(time.RFC3339), cluster, moduleURL, path)

meta := CommentMetadata{
Type: MsgTypePlanRequest,
Cluster: cluster,
Module: module,
Module: module.String(),
Path: path,
CommitID: commitID,
ReqAt: reqAt.Format(time.RFC3339),
Expand Down Expand Up @@ -146,7 +137,7 @@ func parseRunOutputMsg(comment string) (cluster string, module types.NamespacedN
return meta.Cluster, parseNamespaceName(meta.Module), meta.Path, meta.CommitID
}

func runOutputMsg(cluster, module, path string, run *v1beta1.Run) string {
func runOutputMsg(cluster string, module types.NamespacedName, path string, run *v1beta1.Run, webserverURL string) string {
// https://github.com/orgs/community/discussions/27190
characterLimit := 65000

Expand All @@ -160,19 +151,26 @@ func runOutputMsg(cluster, module, path string, run *v1beta1.Run) string {
runOutput = run.InitOutput + "\n" + run.Output
}

msgTml := runOutputMsgTml
if !run.PlanOnly {
msgTml = strings.Replace(msgTml, "Terraform Plan Output", "Terraform Apply Output", 1)
}

runes := []rune(runOutput)

if len(runes) > characterLimit {
runOutput = "Plan output has reached the max character limit of " + fmt.Sprintf("%d", characterLimit) + " characters. " +
runOutput = "Plan output has reached the max character limit of " + fmt.Sprintf("%d", characterLimit) + " characters.\n" +
"The output is truncated from the top.\n" + string(runes[(len(runes)-characterLimit):])
}

display := fmt.Sprintf(runOutputMsgTml, cluster, module, path, run.CommitHash, statusSymbol, run.Status, run.Summary, runOutput, path)
moduleURL := webserverURL + "/#" + module.Namespace + "_" + module.Name

display := fmt.Sprintf(msgTml, module.Name, run.CommitHash, cluster, moduleURL, path, statusSymbol, run.Status, run.Summary, runOutput)

meta := CommentMetadata{
Type: MsgTypeRunOutput,
Cluster: cluster,
Module: module,
Module: module.String(),
Path: path,
CommitID: run.CommitHash,
}
Expand Down
93 changes: 36 additions & 57 deletions prplanner/msg_test.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions prplanner/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (p *Planner) uploadRequestOutput(ctx context.Context, pr *pr) {
}

payload := prComment{
Body: runOutputMsg(p.ClusterEnvName, moduleNamespacedName.String(), path, run),
Body: runOutputMsg(p.ClusterEnvName, moduleNamespacedName, path, run, p.WebserverURL),
}

_, err = p.github.postComment(pr.BaseRepository.Owner.Login, pr.BaseRepository.Name, comment.DatabaseID, pr.Number, payload)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (p *Planner) processRedisKeySetMsg(ctx context.Context, ch <-chan *redis.Me
}

comment := prComment{
Body: runOutputMsg(p.ClusterEnvName, run.Module.String(), module.Spec.Path, run),
Body: runOutputMsg(p.ClusterEnvName, run.Module, module.Spec.Path, run, p.WebserverURL),
}

repo, err := giturl.Parse(module.Spec.RepoURL)
Expand Down
1 change: 1 addition & 0 deletions prplanner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Planner struct {
github GithubInterface
Interval time.Duration
Log *slog.Logger
WebserverURL string
}

func (p *Planner) Init(ctx context.Context, ghApp sysutil.CredsProvider, ch <-chan *redis.Message) error {
Expand Down
2 changes: 1 addition & 1 deletion prplanner/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (p *Planner) addNewRequest(module *tfaplv1beta1.Module, pr *pr, commitID st
req := module.NewRunRequest(tfaplv1beta1.PRPlan, "")

commentBody := prComment{
Body: requestAcknowledgedMsg(p.ClusterEnvName, module.NamespacedName().String(), module.Spec.Path, commitID, req.RequestedAt),
Body: requestAcknowledgedMsg(p.ClusterEnvName, module.NamespacedName(), module.Spec.Path, commitID, req.RequestedAt, p.WebserverURL),
}

commentID, err := p.github.postComment(pr.BaseRepository.Owner.Login, pr.BaseRepository.Name, 0, pr.Number, commentBody)
Expand Down
54 changes: 27 additions & 27 deletions prplanner/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestCheckPRCommits(t *testing.T) {
planner.RedisClient = testRedis

p := generateMockPR(123, "ref1",
[]string{"random comment", runOutputMsg("default", "foo/two", "foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}), "random comment"},
[]string{"random comment", runOutputMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}, "link"), "random comment"},
)

commitsInfo := []repository.CommitInfo{
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestCheckPRCommits(t *testing.T) {
planner.RedisClient = testRedis

p := generateMockPR(123, "ref1",
[]string{"random comment", runOutputMsg("default", "foo/two", "foo/two", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}), "random comment"},
[]string{"random comment", runOutputMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "foo/two", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}, "link"), "random comment"},
)
commitsInfo := []repository.CommitInfo{
{Hash: "hash3", ChangedFiles: []string{"foo/two", "foo/three"}},
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestCheckPRCommits(t *testing.T) {
planner.RedisClient = testRedis

p := generateMockPR(123, "ref1",
[]string{"random comment", runOutputMsg("diff-cluster", "foo/two", "foo/two", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}), "random comment"},
[]string{"random comment", runOutputMsg("diff-cluster", types.NamespacedName{Name: "two", Namespace: "foo"}, "foo/two", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}, "link"), "random comment"},
)
commitsInfo := []repository.CommitInfo{
{Hash: "hash3", ChangedFiles: []string{"foo/two", "foo/three"}},
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestCheckPRCommits(t *testing.T) {
planner.RedisClient = testRedis

p := generateMockPR(123, "ref1",
[]string{"random comment", requestAcknowledgedMsg("default", "foo/two", "foo/two", "hash3", &metav1.Time{Time: time.Now()}), "random comment"},
[]string{"random comment", requestAcknowledgedMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "foo/two", "hash3", &metav1.Time{Time: time.Now()}, "link"), "random comment"},
)
commitsInfo := []repository.CommitInfo{
{Hash: "hash3", ChangedFiles: []string{"foo/two", "foo/three"}},
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestCheckPRCommits(t *testing.T) {
planner.RedisClient = testRedis

p := generateMockPR(123, "ref1",
[]string{"random comment", requestAcknowledgedMsg("diff-cluster", "foo/two", "foo/two", "hash3", &metav1.Time{Time: time.Now()}), "random comment"},
[]string{"random comment", requestAcknowledgedMsg("diff-cluster", types.NamespacedName{Name: "two", Namespace: "foo"}, "foo/two", "hash3", &metav1.Time{Time: time.Now()}, "link"), "random comment"},
)
commitsInfo := []repository.CommitInfo{
{Hash: "hash3", ChangedFiles: []string{"foo/two", "foo/three"}},
Expand Down Expand Up @@ -429,7 +429,7 @@ func TestCheckPRCommits(t *testing.T) {
planner.RedisClient = testRedis

p := generateMockPR(123, "ref1",
[]string{"random comment", runOutputMsg("default", "foo/two", "foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}), "random comment"},
[]string{"random comment", runOutputMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "some output"}, "link"), "random comment"},
)
commitsInfo := []repository.CommitInfo{
{Hash: "hash3", ChangedFiles: []string{"foo/two", "foo/three"}},
Expand Down Expand Up @@ -577,8 +577,8 @@ func Test_checkPRCommentsForPlanRequests(t *testing.T) {
pr := generateMockPR(123, "ref1",
[]string{
"@terraform-applier plan two",
requestAcknowledgedMsg("default", "foo/two", "path/foo/two", "hash2", mustParseMetaTime("2023-04-02T15:04:05Z")),
requestAcknowledgedMsg("default", "foo/three", "path/foo/three", "hash3", mustParseMetaTime("2023-04-02T15:04:05Z")),
requestAcknowledgedMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "path/foo/two", "hash2", mustParseMetaTime("2023-04-02T15:04:05Z"), "link"),
requestAcknowledgedMsg("default", types.NamespacedName{Name: "three", Namespace: "foo"}, "path/foo/three", "hash3", mustParseMetaTime("2023-04-02T15:04:05Z"), "link"),
},
)

Expand All @@ -600,8 +600,8 @@ func Test_checkPRCommentsForPlanRequests(t *testing.T) {
pr := generateMockPR(123, "ref1",
[]string{
"@terraform-applier plan path/foo/two",
requestAcknowledgedMsg("default", "foo/two", "path/foo/two", "hash2", mustParseMetaTime("2023-04-02T15:04:05Z")),
requestAcknowledgedMsg("default", "foo/three", "path/foo/three", "hash3", mustParseMetaTime("2023-04-02T15:04:05Z")),
requestAcknowledgedMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "path/foo/two", "hash2", mustParseMetaTime("2023-04-02T15:04:05Z"), "link"),
requestAcknowledgedMsg("default", types.NamespacedName{Name: "three", Namespace: "foo"}, "path/foo/three", "hash3", mustParseMetaTime("2023-04-02T15:04:05Z"), "link"),
},
)

Expand All @@ -619,8 +619,8 @@ func Test_checkPRCommentsForPlanRequests(t *testing.T) {
pr := generateMockPR(123, "ref1",
[]string{
"@terraform-applier plan two",
runOutputMsg("default", "foo/two", "path/foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}),
runOutputMsg("default", "foo/three", "path/foo/three", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}),
runOutputMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "path/foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}, "link"),
runOutputMsg("default", types.NamespacedName{Name: "three", Namespace: "foo"}, "path/foo/three", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}, "link"),
},
)

Expand All @@ -638,8 +638,8 @@ func Test_checkPRCommentsForPlanRequests(t *testing.T) {
pr := generateMockPR(123, "ref1",
[]string{
"@terraform-applier plan path/foo/two",
runOutputMsg("default", "foo/two", "path/foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}),
runOutputMsg("default", "foo/three", "path/foo/three", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}),
runOutputMsg("default", types.NamespacedName{Name: "two", Namespace: "foo"}, "path/foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}, "link"),
runOutputMsg("default", types.NamespacedName{Name: "three", Namespace: "foo"}, "path/foo/three", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}, "link"),
},
)

Expand Down Expand Up @@ -789,8 +789,8 @@ func Test_checkPRCommentsForPlanRequests(t *testing.T) {
pr := generateMockPR(123, "ref1",
[]string{
"@terraform-applier plan two",
requestAcknowledgedMsg("diff-cluster", "foo/two", "path/foo/two", "hash2", mustParseMetaTime("2023-04-02T15:04:05Z")),
requestAcknowledgedMsg("default", "foo/three", "path/foo/three", "hash3", mustParseMetaTime("2023-04-02T15:04:05Z")),
requestAcknowledgedMsg("diff-cluster", types.NamespacedName{Name: "two", Namespace: "foo"}, "path/foo/two", "hash2", mustParseMetaTime("2023-04-02T15:04:05Z"), "link"),
requestAcknowledgedMsg("default", types.NamespacedName{Name: "three", Namespace: "foo"}, "path/foo/three", "hash3", mustParseMetaTime("2023-04-02T15:04:05Z"), "link"),
},
)

Expand Down Expand Up @@ -841,8 +841,8 @@ func Test_checkPRCommentsForPlanRequests(t *testing.T) {
pr := generateMockPR(123, "ref1",
[]string{
"@terraform-applier plan two",
runOutputMsg("diff-cluster", "foo/two", "path/foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}),
runOutputMsg("default", "foo/three", "path/foo/three", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}),
runOutputMsg("diff-cluster", types.NamespacedName{Name: "two", Namespace: "foo"}, "path/foo/two", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}, "link"),
runOutputMsg("default", types.NamespacedName{Name: "three", Namespace: "foo"}, "path/foo/three", &tfaplv1beta1.Run{CommitHash: "hash3", Summary: "Plan: x to add, x to change, x to destroy.", Output: "tf plan output"}, "link"),
},
)

Expand Down Expand Up @@ -954,7 +954,7 @@ func Test_isPlanOutputPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: runOutputMsg("default", "foo/one", "foo/one", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy."}),
Body: runOutputMsg("default", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/one", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy."}, "link"),
},
}}},
cluster: "default",
Expand All @@ -972,7 +972,7 @@ func Test_isPlanOutputPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: runOutputMsg("diff-cluster", "foo/one", "foo/one", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy."}),
Body: runOutputMsg("diff-cluster", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/one", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy."}, "link"),
},
}}},
cluster: "default",
Expand All @@ -990,7 +990,7 @@ func Test_isPlanOutputPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: runOutputMsg("default", "one", "foo/one", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy."}),
Body: runOutputMsg("default", types.NamespacedName{Name: "one", Namespace: ""}, "foo/one", &tfaplv1beta1.Run{CommitHash: "hash2", Summary: "Plan: x to add, x to change, x to destroy."}, "link"),
},
}}},
cluster: "default",
Expand Down Expand Up @@ -1116,7 +1116,7 @@ func Test_isPlanRequestAckPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: requestAcknowledgedMsg("default", "foo/one", "foo/one", "hash2", &metav1.Time{Time: time.Now()}),
Body: requestAcknowledgedMsg("default", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/one", "hash2", &metav1.Time{Time: time.Now()}, "link"),
},
}}},
cluster: "default",
Expand All @@ -1133,7 +1133,7 @@ func Test_isPlanRequestAckPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: requestAcknowledgedMsg("diff-cluster", "foo/one", "foo/one", "hash2", &metav1.Time{Time: time.Now()}),
Body: requestAcknowledgedMsg("diff-cluster", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/one", "hash2", &metav1.Time{Time: time.Now()}, "link"),
},
}}},
cluster: "default",
Expand All @@ -1150,7 +1150,7 @@ func Test_isPlanRequestAckPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: requestAcknowledgedMsg("default", "foo/one", "foo/one", "hash2", &metav1.Time{Time: time.Now().Add(-30 * time.Minute)}),
Body: requestAcknowledgedMsg("default", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/one", "hash2", &metav1.Time{Time: time.Now().Add(-30 * time.Minute)}, "link"),
},
}}},
cluster: "default",
Expand All @@ -1167,7 +1167,7 @@ func Test_isPlanRequestAckPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: requestAcknowledgedMsg("default", "foo/one", "foo/one", "hash2", &metav1.Time{Time: time.Now().Add(5 * time.Minute)}),
Body: requestAcknowledgedMsg("default", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/one", "hash2", &metav1.Time{Time: time.Now().Add(5 * time.Minute)}, "link"),
},
}}},
cluster: "default",
Expand All @@ -1184,7 +1184,7 @@ func Test_isPlanRequestAckPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: requestAcknowledgedMsg("default", "foo/one", "foo/one", "hash2", &metav1.Time{Time: time.Now()}),
Body: requestAcknowledgedMsg("default", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/one", "hash2", &metav1.Time{Time: time.Now()}, "link"),
},
}}},
cluster: "default",
Expand All @@ -1201,7 +1201,7 @@ func Test_isPlanRequestAckPostedForCommit(t *testing.T) {
}{Nodes: []prComment{
{
DatabaseID: 01234567,
Body: requestAcknowledgedMsg("default", "foo/two", "foo/two", "hash3", &metav1.Time{Time: time.Now()}),
Body: requestAcknowledgedMsg("default", types.NamespacedName{Name: "one", Namespace: "foo"}, "foo/two", "hash3", &metav1.Time{Time: time.Now()}, "link"),
},
}}},
cluster: "default",
Expand Down
Loading