From e2aaf44154682ecc35d32695c127ad18e6e43dcf Mon Sep 17 00:00:00 2001 From: Soh Takada <34182539+FeLvi-zzz@users.noreply.github.com> Date: Sat, 16 Jul 2022 14:30:04 +0900 Subject: [PATCH] shorten too long github comment --- notifier/github/notify.go | 25 ++++++++++++++++++++++--- notifier/github/notify_test.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/notifier/github/notify.go b/notifier/github/notify.go index b9a54a3..d966b70 100644 --- a/notifier/github/notify.go +++ b/notifier/github/notify.go @@ -2,8 +2,10 @@ package github import ( "context" - "github.com/mercari/tfnotify/terraform" "net/http" + "unicode/utf8" + + "github.com/mercari/tfnotify/terraform" ) // NotifyService handles communication with the notification related @@ -70,7 +72,7 @@ func (g *NotifyService) Notify(body string) (exit int, err error) { Link: cfg.CI, UseRawOutput: cfg.UseRawOutput, }) - body, err = template.Execute() + body, err = templateExecute(template) if err != nil { return result.ExitCode, err } @@ -113,7 +115,7 @@ func (g *NotifyService) notifyDestroyWarning(body string, result terraform.Parse Link: cfg.CI, UseRawOutput: cfg.UseRawOutput, }) - body, err := destroyWarningTemplate.Execute() + body, err := templateExecute(destroyWarningTemplate) if err != nil { return err } @@ -144,3 +146,20 @@ func (g *NotifyService) removeResultLabels() error { return nil } + +func templateExecute(template terraform.Template) (string, error) { + body, err := template.Execute() + if err != nil { + return "", err + } + + if utf8.RuneCountInString(body) <= 65536 { + return body, nil + } + + templateValues := template.GetValue() + templateValues.Body = "Body is too long. Please check the CI result." + + template.SetValue(templateValues) + return template.Execute() +} diff --git a/notifier/github/notify_test.go b/notifier/github/notify_test.go index b3753cb..93aeaee 100644 --- a/notifier/github/notify_test.go +++ b/notifier/github/notify_test.go @@ -1,6 +1,7 @@ package github import ( + "fmt" "testing" "github.com/mercari/tfnotify/terraform" @@ -85,6 +86,24 @@ func TestNotifyNotify(t *testing.T) { ok: true, exitCode: 0, }, + { + // valid, isPR, and cannot comment details because body is too long + config: Config{ + Token: "token", + Owner: "owner", + Repo: "repo", + PR: PullRequest{ + Revision: "", + Number: 1, + Message: "message", + }, + Parser: terraform.NewPlanParser(), + Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate), + }, + body: fmt.Sprintf("Plan: 1 to add \n%065537s", "0"), + ok: true, + exitCode: 0, + }, { // valid, and isRevision config: Config{