Skip to content

Commit 6101068

Browse files
ijinsuzuki-shunsukeautofix-ci[bot]
authored
fix: Enable PR label updates alongside file output for plan operations (#1758)
* fix: Enable PR label updates alongside file output for plan operations * [autofix.ci] apply automated fixes * refactor: replace interface{} with any * refactor: make updateGitHubLabels private * refactor: rename a method * fix: fix log * refactor: repalce type any with a concrete type * refactor: simplify code * refactor: simplify code * refactor: remove NotifyService.updateLabels * fix: update labels even though OutputFile is empty * fix: include error messages about labels in output file * fix: fix debug log --------- Co-authored-by: Shunsuke Suzuki <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Shunsuke Suzuki <[email protected]>
1 parent 5cf74a1 commit 6101068

File tree

5 files changed

+58
-30
lines changed

5 files changed

+58
-30
lines changed

pkg/controller/controller.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,6 @@ func (c *Controller) getNotifier(ctx context.Context) (notifier.Notifier, error)
131131
}
132132
labels = a
133133
}
134-
// Write output to file instead of github comment
135-
if c.Config.Output != "" {
136-
client, err := localfile.NewClient(&localfile.Config{
137-
OutputFile: c.Config.Output,
138-
Parser: c.Parser,
139-
UseRawOutput: c.Config.Terraform.UseRawOutput,
140-
CI: c.Config.CI.Link,
141-
Template: c.Template,
142-
ParseErrorTemplate: c.ParseErrorTemplate,
143-
Vars: c.Config.Vars,
144-
EmbeddedVarNames: c.Config.EmbeddedVarNames,
145-
Templates: c.Config.Templates,
146-
Masks: c.Config.Masks,
147-
})
148-
if err != nil {
149-
return nil, err
150-
}
151-
return client.Notify, nil
152-
}
153134
client, err := github.NewClient(ctx, &github.Config{
154135
BaseURL: c.Config.GHEBaseURL,
155136
GraphQLEndpoint: c.Config.GHEGraphQLEndpoint,
@@ -176,5 +157,25 @@ func (c *Controller) getNotifier(ctx context.Context) (notifier.Notifier, error)
176157
if err != nil {
177158
return nil, err
178159
}
160+
// Write output to file instead of github comment
161+
if c.Config.Output != "" {
162+
client, err := localfile.NewClient(&localfile.Config{
163+
OutputFile: c.Config.Output,
164+
Parser: c.Parser,
165+
UseRawOutput: c.Config.Terraform.UseRawOutput,
166+
CI: c.Config.CI.Link,
167+
Template: c.Template,
168+
ParseErrorTemplate: c.ParseErrorTemplate,
169+
Vars: c.Config.Vars,
170+
EmbeddedVarNames: c.Config.EmbeddedVarNames,
171+
Templates: c.Config.Templates,
172+
Masks: c.Config.Masks,
173+
DisableLabel: c.Config.Terraform.Plan.DisableLabel,
174+
}, client.Notify)
175+
if err != nil {
176+
return nil, err
177+
}
178+
return client.Notify, nil
179+
}
179180
return client.Notify, nil
180181
}

pkg/notifier/github/label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/suzuki-shunsuke/tfcmt/v4/pkg/terraform"
1010
)
1111

12-
func (g *NotifyService) updateLabels(ctx context.Context, result terraform.ParseResult) []string { //nolint:cyclop
12+
func (g *NotifyService) UpdateLabels(ctx context.Context, result terraform.ParseResult) []string { //nolint:cyclop
1313
cfg := g.client.Config
1414
var (
1515
labelToAdd string

pkg/notifier/github/plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (g *NotifyService) Plan(ctx context.Context, param *notifier.ParamExec) err
3636
}
3737

3838
if cfg.PR.IsNumber() && cfg.ResultLabels.HasAnyLabelDefined() {
39-
errMsgs = append(errMsgs, g.updateLabels(ctx, result)...)
39+
errMsgs = append(errMsgs, g.UpdateLabels(ctx, result)...)
4040
}
4141

4242
if cfg.IgnoreWarning {

pkg/notifier/localfile/client.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package localfile
22

33
import (
4+
"context"
5+
46
"github.com/suzuki-shunsuke/tfcmt/v4/pkg/config"
7+
"github.com/suzuki-shunsuke/tfcmt/v4/pkg/notifier/github"
58
"github.com/suzuki-shunsuke/tfcmt/v4/pkg/terraform"
69
)
710

@@ -13,8 +16,9 @@ type Client struct {
1316

1417
common service
1518

16-
Notify *NotifyService
17-
Output *OutputService
19+
Notify *NotifyService
20+
Output *OutputService
21+
labeler Labeler
1822
}
1923

2024
// Config is a configuration for local file
@@ -30,16 +34,34 @@ type Config struct {
3034
CI string
3135
UseRawOutput bool
3236
Masks []*config.Mask
37+
38+
// For labeling
39+
DisableLabel bool
40+
}
41+
42+
type GitHubLabelConfig struct {
43+
BaseURL string
44+
GraphQLEndpoint string
45+
Owner string
46+
Repo string
47+
PRNumber int
48+
Revision string
49+
Labels github.ResultLabels
3350
}
3451

3552
type service struct {
3653
client *Client
3754
}
3855

56+
type Labeler interface {
57+
UpdateLabels(ctx context.Context, result terraform.ParseResult) []string
58+
}
59+
3960
// NewClient returns Client initialized with Config
40-
func NewClient(cfg *Config) (*Client, error) {
61+
func NewClient(cfg *Config, labeler Labeler) (*Client, error) {
4162
c := &Client{
42-
Config: cfg,
63+
Config: cfg,
64+
labeler: labeler,
4365
}
4466

4567
c.common.client = c

pkg/notifier/localfile/plan.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// Plan posts comment optimized for notifications
14-
func (g *NotifyService) Plan(_ context.Context, param *notifier.ParamExec) error {
14+
func (g *NotifyService) Plan(ctx context.Context, param *notifier.ParamExec) error {
1515
cfg := g.client.Config
1616
parser := g.client.Config.Parser
1717
template := g.client.Config.Template
@@ -29,6 +29,14 @@ func (g *NotifyService) Plan(_ context.Context, param *notifier.ParamExec) error
2929
}
3030
}
3131

32+
logE := logrus.WithFields(logrus.Fields{
33+
"program": "tfcmt",
34+
})
35+
if !cfg.DisableLabel {
36+
logE.Debugf("updating labels")
37+
errMsgs = append(errMsgs, g.client.labeler.UpdateLabels(ctx, result)...)
38+
}
39+
3240
template.SetValue(terraform.CommonTemplate{
3341
Result: result.Result,
3442
ChangedResult: result.ChangedResult,
@@ -57,15 +65,12 @@ func (g *NotifyService) Plan(_ context.Context, param *notifier.ParamExec) error
5765
return err
5866
}
5967

60-
logE := logrus.WithFields(logrus.Fields{
61-
"program": "tfcmt",
62-
})
63-
6468
body = mask.Mask(body, g.client.Config.Masks)
6569

6670
logE.Debug("write a plan output to a file")
6771
if err := g.client.Output.WriteToFile(body, cfg.OutputFile); err != nil {
6872
return fmt.Errorf("write a plan output to a file: %w", err)
6973
}
74+
7075
return nil
7176
}

0 commit comments

Comments
 (0)