diff --git a/tool/actions-plan-preview/main.go b/tool/actions-plan-preview/main.go index 34f321eff6..9562fdda56 100644 --- a/tool/actions-plan-preview/main.go +++ b/tool/actions-plan-preview/main.go @@ -141,36 +141,21 @@ func main() { doComment(failureBadgeURL + "\nUnable to run plan-preview for a closed pull request.") return } + + minimizePreviousComment(ctx, ghGraphQLClient, event) + body := makeCommentBody(event, result) - doComment(failureBadgeURL + "\n" + body) + doComment(body) + + log.Println("Successfully minimized last plan-preview result on pull request") log.Println("plan-preview result has error") os.Exit(1) } - // Find comments we sent before - comment, err := findLatestPlanPreviewComment(ctx, ghGraphQLClient, event.Owner, event.Repo, event.PRNumber) - if err != nil { - log.Printf("Unable to find the previous comment to minimize (%v)\n", err) - } + minimizePreviousComment(ctx, ghGraphQLClient, event) body := makeCommentBody(event, result) doComment(body) - - if comment == nil { - return - } - - if bool(comment.IsMinimized) { - log.Printf("Previous plan-preview comment has already minimized. So don't minimize anything\n") - return - } - - if err := minimizeComment(ctx, ghGraphQLClient, comment.ID, "OUTDATED"); err != nil { - log.Printf("warning: cannot minimize comment: %s\n", err.Error()) - return - } - - log.Printf("Successfully minimized last plan-preview result on pull request\n") } type arguments struct { diff --git a/tool/actions-plan-preview/planpreview.go b/tool/actions-plan-preview/planpreview.go index 48a7d2f3e3..70582bc709 100644 --- a/tool/actions-plan-preview/planpreview.go +++ b/tool/actions-plan-preview/planpreview.go @@ -19,6 +19,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/shurcooL/githubv4" "log" "os" "os/exec" @@ -331,3 +332,27 @@ func generateTerraformShortPlanDetails(details string) (string, error) { } return details[start:], nil } + +func minimizePreviousComment(ctx context.Context, ghGraphQLClient *githubv4.Client, event *githubEvent) { + // Find comments we sent before + comment, err := findLatestPlanPreviewComment(ctx, ghGraphQLClient, event.Owner, event.Repo, event.PRNumber) + if err != nil { + log.Printf("Unable to find the previous comment to minimize (%v)\n", err) + } + + if comment == nil { + return + } + + if bool(comment.IsMinimized) { + log.Println("Previous plan-preview comment has already minimized. So don't minimize anything") + return + } + + if err := minimizeComment(ctx, ghGraphQLClient, comment.ID, "OUTDATED"); err != nil { + log.Printf("warning: cannot minimize comment: %s\n", err.Error()) + return + } + + log.Println("Successfully minimized last plan-preview result on pull request") +}