Skip to content

Commit e2aaf44

Browse files
committed
shorten too long github comment
1 parent ea3a4c1 commit e2aaf44

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

notifier/github/notify.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package github
22

33
import (
44
"context"
5-
"github.com/mercari/tfnotify/terraform"
65
"net/http"
6+
"unicode/utf8"
7+
8+
"github.com/mercari/tfnotify/terraform"
79
)
810

911
// NotifyService handles communication with the notification related
@@ -70,7 +72,7 @@ func (g *NotifyService) Notify(body string) (exit int, err error) {
7072
Link: cfg.CI,
7173
UseRawOutput: cfg.UseRawOutput,
7274
})
73-
body, err = template.Execute()
75+
body, err = templateExecute(template)
7476
if err != nil {
7577
return result.ExitCode, err
7678
}
@@ -113,7 +115,7 @@ func (g *NotifyService) notifyDestroyWarning(body string, result terraform.Parse
113115
Link: cfg.CI,
114116
UseRawOutput: cfg.UseRawOutput,
115117
})
116-
body, err := destroyWarningTemplate.Execute()
118+
body, err := templateExecute(destroyWarningTemplate)
117119
if err != nil {
118120
return err
119121
}
@@ -144,3 +146,20 @@ func (g *NotifyService) removeResultLabels() error {
144146

145147
return nil
146148
}
149+
150+
func templateExecute(template terraform.Template) (string, error) {
151+
body, err := template.Execute()
152+
if err != nil {
153+
return "", err
154+
}
155+
156+
if utf8.RuneCountInString(body) <= 65536 {
157+
return body, nil
158+
}
159+
160+
templateValues := template.GetValue()
161+
templateValues.Body = "Body is too long. Please check the CI result."
162+
163+
template.SetValue(templateValues)
164+
return template.Execute()
165+
}

notifier/github/notify_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/mercari/tfnotify/terraform"
@@ -85,6 +86,24 @@ func TestNotifyNotify(t *testing.T) {
8586
ok: true,
8687
exitCode: 0,
8788
},
89+
{
90+
// valid, isPR, and cannot comment details because body is too long
91+
config: Config{
92+
Token: "token",
93+
Owner: "owner",
94+
Repo: "repo",
95+
PR: PullRequest{
96+
Revision: "",
97+
Number: 1,
98+
Message: "message",
99+
},
100+
Parser: terraform.NewPlanParser(),
101+
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
102+
},
103+
body: fmt.Sprintf("Plan: 1 to add \n%065537s", "0"),
104+
ok: true,
105+
exitCode: 0,
106+
},
88107
{
89108
// valid, and isRevision
90109
config: Config{

0 commit comments

Comments
 (0)