Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrapCode no longer trancate long output if tfcmt outputs the result to a local file #1309

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/notifier/localfile/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (g *NotifyService) Apply(_ context.Context, param *notifier.ParamExec) erro
cfg := g.client.Config
parser := g.client.Config.Parser
template := g.client.Config.Template
template.IsLocal = true
var errMsgs []string

result := parser.Parse(param.CombinedOutput)
Expand Down
1 change: 1 addition & 0 deletions pkg/notifier/localfile/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (g *NotifyService) Plan(_ context.Context, param *notifier.ParamExec) error
cfg := g.client.Config
parser := g.client.Config.Parser
template := g.client.Config.Template
template.IsLocal = true
var errMsgs []string

result := parser.Parse(param.CombinedOutput)
Expand Down
39 changes: 26 additions & 13 deletions pkg/terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type CommonTemplate struct {

// Template is a default template for terraform commands
type Template struct {
IsLocal bool
Template string
CommonTemplate
}
Expand Down Expand Up @@ -142,16 +143,7 @@ func avoidHTMLEscape(text string) htmltemplate.HTML {
return htmltemplate.HTML(text) //nolint:gosec
}

func wrapCode(text string) interface{} {
if len(text) > 60000 { //nolint:mnd
text = text[:20000] + `

# ...
# ... The maximum length of GitHub Comment is 65536, so the content is omitted by tfcmt.
# ...

` + text[len(text)-20000:]
}
func wrapCodeWithoutTrim(text string) interface{} {
if strings.Contains(text, "```") {
if strings.Contains(text, "~~~") {
return htmltemplate.HTML(`<pre><code>` + htmltemplate.HTMLEscapeString(text) + `</code></pre>`) //nolint:gosec
Expand All @@ -161,10 +153,31 @@ func wrapCode(text string) interface{} {
return htmltemplate.HTML("\n```hcl\n" + text + "\n```\n") //nolint:gosec
}

func generateOutput(kind, template string, data map[string]interface{}, useRawOutput bool) (string, error) {
func trim(text string) string {
if len(text) <= 60000 { //nolint:mnd
return text
}
return text[:20000] + `

# ...
# ... The maximum length of GitHub Comment is 65536, so the content is omitted by tfcmt.
# ...

` + text[len(text)-20000:]
}

func wrapCode(text string) interface{} {
return wrapCodeWithoutTrim(trim(text))
}

func (t *Template) generateOutput(kind, template string, data map[string]interface{}) (string, error) {
var b bytes.Buffer
wrapCode := wrapCode
if t.IsLocal {
wrapCode = wrapCodeWithoutTrim
}

if useRawOutput {
if t.UseRawOutput {
tpl, err := texttemplate.New(kind).Funcs(texttemplate.FuncMap{
"avoidHTMLEscape": avoidHTMLEscape,
"wrapCode": wrapCode,
Expand Down Expand Up @@ -277,7 +290,7 @@ _This feature was introduced from [Terraform v0.15.4](https://github.com/hashico
templates[k] = v
}

resp, err := generateOutput("default", addTemplates(t.Template, templates), data, t.UseRawOutput)
resp, err := t.generateOutput("default", addTemplates(t.Template, templates), data)
if err != nil {
return "", err
}
Expand Down