Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
feat: support to configure label color
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jan 1, 2021
1 parent dff7572 commit 9854713
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
4 changes: 4 additions & 0 deletions config/config.go
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions main.go
Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions notifier/github/client.go
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions notifier/github/github.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions notifier/github/notify.go
Expand Up @@ -38,27 +38,47 @@ 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},
)
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
}
}
}
}
}
}
}
}
Expand Down

0 comments on commit 9854713

Please sign in to comment.