From 98547135a6d37b11b641feb399eec17721fe0bc0 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sun, 29 Nov 2020 20:33:01 +0900 Subject: [PATCH] feat: support to configure label color --- config/config.go | 4 ++++ main.go | 12 ++++++++---- notifier/github/client.go | 12 ++++++++---- notifier/github/github.go | 8 ++++++++ notifier/github/notify.go | 24 ++++++++++++++++++++++-- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index b80f79c..ebfce85 100644 --- a/config/config.go +++ b/config/config.go @@ -91,22 +91,26 @@ type Plan struct { // WhenAddOrUpdateOnly is a configuration to notify the plan result contains new or updated in place resources type WhenAddOrUpdateOnly struct { Label string `yaml:"label,omitempty"` + Color string `yaml:"color,omitempty"` } // WhenDestroy is a configuration to notify the plan result contains destroy operation type WhenDestroy struct { Label string `yaml:"label,omitempty"` Template string `yaml:"template,omitempty"` + Color string `yaml:"color,omitempty"` } // WhenNoChange is a configuration to add a label when the plan result contains no change type WhenNoChanges struct { Label string `yaml:"label,omitempty"` + Color string `yaml:"color,omitempty"` } // WhenPlanError is a configuration to notify the plan result returns an error type WhenPlanError struct { Label string `yaml:"label,omitempty"` + Color string `yaml:"color,omitempty"` } // Apply is a terraform apply config diff --git a/main.go b/main.go index 6573635..99f0944 100644 --- a/main.go +++ b/main.go @@ -122,10 +122,14 @@ func (t *tfnotify) Run(ctx context.Context) error { DestroyWarningTemplate: t.destroyWarningTemplate, WarnDestroy: t.warnDestroy, ResultLabels: github.ResultLabels{ - AddOrUpdateLabel: t.config.Terraform.Plan.WhenAddOrUpdateOnly.Label, - DestroyLabel: t.config.Terraform.Plan.WhenDestroy.Label, - NoChangesLabel: t.config.Terraform.Plan.WhenNoChanges.Label, - PlanErrorLabel: t.config.Terraform.Plan.WhenPlanError.Label, + AddOrUpdateLabel: t.config.Terraform.Plan.WhenAddOrUpdateOnly.Label, + DestroyLabel: t.config.Terraform.Plan.WhenDestroy.Label, + NoChangesLabel: t.config.Terraform.Plan.WhenNoChanges.Label, + PlanErrorLabel: t.config.Terraform.Plan.WhenPlanError.Label, + AddOrUpdateLabelColor: t.config.Terraform.Plan.WhenAddOrUpdateOnly.Color, + DestroyLabelColor: t.config.Terraform.Plan.WhenDestroy.Color, + NoChangesLabelColor: t.config.Terraform.Plan.WhenNoChanges.Color, + PlanErrorLabelColor: t.config.Terraform.Plan.WhenPlanError.Color, }, }) if err != nil { diff --git a/notifier/github/client.go b/notifier/github/client.go index 1c62bca..90ccffc 100644 --- a/notifier/github/client.go +++ b/notifier/github/client.go @@ -120,10 +120,14 @@ func (pr *PullRequest) IsNumber() bool { // ResultLabels represents the labels to add to the PR depending on the plan result type ResultLabels struct { - AddOrUpdateLabel string - DestroyLabel string - NoChangesLabel string - PlanErrorLabel string + AddOrUpdateLabel string + DestroyLabel string + NoChangesLabel string + PlanErrorLabel string + AddOrUpdateLabelColor string + DestroyLabelColor string + NoChangesLabelColor string + PlanErrorLabelColor string } // HasAnyLabelDefined returns true if any of the internal labels are set diff --git a/notifier/github/github.go b/notifier/github/github.go index d5d77d4..43e0db7 100644 --- a/notifier/github/github.go +++ b/notifier/github/github.go @@ -14,6 +14,7 @@ type API interface { IssuesListComments(ctx context.Context, number int, opt *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error) IssuesAddLabels(ctx context.Context, number int, labels []string) ([]*github.Label, *github.Response, error) IssuesRemoveLabel(ctx context.Context, number int, label string) (*github.Response, error) + IssuesUpdateLabel(ctx context.Context, label, color string) (*github.Label, *github.Response, error) RepositoriesCreateComment(ctx context.Context, sha string, comment *github.RepositoryComment) (*github.RepositoryComment, *github.Response, error) RepositoriesListCommits(ctx context.Context, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) RepositoriesGetCommit(ctx context.Context, sha string) (*github.RepositoryCommit, *github.Response, error) @@ -55,6 +56,13 @@ func (g *GitHub) IssuesRemoveLabel(ctx context.Context, number int, label string return g.Client.Issues.RemoveLabelForIssue(ctx, g.owner, g.repo, number, label) } +// IssuesUpdateLabel is a wrapper of https://pkg.go.dev/github.com/google/go-github/github#IssuesService.EditLabel +func (g *GitHub) IssuesUpdateLabel(ctx context.Context, label, color string) (*github.Label, *github.Response, error) { + return g.Client.Issues.EditLabel(ctx, g.owner, g.repo, label, &github.Label{ + Color: &color, + }) +} + // RepositoriesCreateComment is a wrapper of https://godoc.org/github.com/google/go-github/github#RepositoriesService.CreateComment func (g *GitHub) RepositoriesCreateComment(ctx context.Context, sha string, comment *github.RepositoryComment) (*github.RepositoryComment, *github.Response, error) { return g.Client.Repositories.CreateComment(ctx, g.owner, g.repo, sha, comment) diff --git a/notifier/github/notify.go b/notifier/github/notify.go index 7dfcf8f..f79f66e 100644 --- a/notifier/github/notify.go +++ b/notifier/github/notify.go @@ -38,20 +38,27 @@ func (g *NotifyService) Notify(ctx context.Context, body string) (exit int, err if err != nil { return result.ExitCode, err } - var labelToAdd string + var ( + labelToAdd string + labelColor string + ) if result.HasAddOrUpdateOnly { labelToAdd = cfg.ResultLabels.AddOrUpdateLabel + labelColor = cfg.ResultLabels.AddOrUpdateLabelColor } else if result.HasDestroy { labelToAdd = cfg.ResultLabels.DestroyLabel + labelColor = cfg.ResultLabels.DestroyLabelColor } else if result.HasNoChanges { labelToAdd = cfg.ResultLabels.NoChangesLabel + labelColor = cfg.ResultLabels.NoChangesLabelColor } else if result.HasPlanError { labelToAdd = cfg.ResultLabels.PlanErrorLabel + labelColor = cfg.ResultLabels.PlanErrorLabelColor } if labelToAdd != "" { - _, _, err = g.client.API.IssuesAddLabels( + labels, _, err := g.client.API.IssuesAddLabels( context.Background(), cfg.PR.Number, []string{labelToAdd}, @@ -59,6 +66,19 @@ func (g *NotifyService) Notify(ctx context.Context, body string) (exit int, err if err != nil { return result.ExitCode, err } + if labelColor != "" { + // set the color of label + for _, label := range labels { + if labelToAdd == label.GetName() { + if label.GetColor() != labelColor { + _, _, err := g.client.API.IssuesUpdateLabel(context.Background(), labelToAdd, labelColor) + if err != nil { + return result.ExitCode, err + } + } + } + } + } } } }