Skip to content

Commit

Permalink
shorten too long github comment
Browse files Browse the repository at this point in the history
  • Loading branch information
FeLvi-zzz committed Jul 16, 2022
1 parent ea3a4c1 commit e2aaf44
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
25 changes: 22 additions & 3 deletions notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}
19 changes: 19 additions & 0 deletions notifier/github/notify_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github

import (
"fmt"
"testing"

"github.com/mercari/tfnotify/terraform"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit e2aaf44

Please sign in to comment.