Skip to content

Commit

Permalink
Create minimize previous comment function
Browse files Browse the repository at this point in the history
Signed-off-by: TakumaKurosawa <[email protected]>
  • Loading branch information
TakumaKurosawa committed Dec 6, 2023
1 parent 24f77b5 commit 84dcc11
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 42 deletions.
44 changes: 2 additions & 42 deletions tool/actions-plan-preview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,60 +142,20 @@ func main() {
return
}

// 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 {
log.Println("plan-preview result has error")
os.Exit(1)
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")
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 {
Expand Down
25 changes: 25 additions & 0 deletions tool/actions-plan-preview/planpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/shurcooL/githubv4"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -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.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")
}

0 comments on commit 84dcc11

Please sign in to comment.