From 8d4a77e622e3d6326071e0affa6b2c1771bc4d02 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Wed, 28 Oct 2020 16:20:50 +0100 Subject: [PATCH 1/2] Add prune option prune option indicate if unmanaged labels should be removed, by default is set to true --- README.md | 8 ++++++-- action.yml | 4 ++++ cmd/action-label-syncer/main.go | 9 ++++++++- pkg/github/github.go | 33 ++++++++++++++++++++------------- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6c5ad37..55de445 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ By using this workflow, you can sync current labels with labels configured in a ![](docs/assets/screenshot.png) -The default file path is `.github/labels.yml`, but you can specify any file path with `jobs..steps.with`. +The default file path is `.github/labels.yml`, but you can specify any file path with `jobs..steps.with.manifest`. To create manifest of the current labels easily, using [label-exporter](https://github.com/micnncim/label-exporter) is recommended. @@ -57,7 +57,11 @@ jobs: manifest: path/to/manifest/labels.yml ``` -If a label color changes, the same label is updated with the new color. If a label name changes, the previous label is deleted. All issues and PRs that were previously labeled with this label are now unlabeled. +If a label color changes, the same label is updated with the new color. If a label name changes, the previous label is deleted by default. +Also all existing labels which not listed in `manifest` will be deleted by default. +All issues and PRs that were previously labeled with this label are now unlabeled. + +You can add `jobs..steps.with.prune: false` in order to preserver all existing labels which is not mentioned in `manifest`, in this case when a label will be renamed old label will be not deleted. ## Sync labels on another repository diff --git a/action.yml b/action.yml index 08868b5..f938198 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: token: description: "An alternative GitHub token to use instead" required: false + prune: + description: "remove unmanaged labels from repository" + required: false + default: true runs: using: "docker" image: "Dockerfile" diff --git a/cmd/action-label-syncer/main.go b/cmd/action-label-syncer/main.go index 8106956..a4119bc 100644 --- a/cmd/action-label-syncer/main.go +++ b/cmd/action-label-syncer/main.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "os" + "strconv" "strings" "github.com/micnncim/action-label-syncer/pkg/github" @@ -48,8 +49,14 @@ func main() { } owner, repo := slugs[0], slugs[1] + prune, err := strconv.ParseBool(os.Getenv("INPUT_PRUNE")) + if err != nil { + fmt.Fprintf(os.Stderr, "unable to parse prune: %v\n", err) + os.Exit(1) + } + ctx := context.Background() - if err := client.SyncLabels(ctx, owner, repo, labels); err != nil { + if err := client.SyncLabels(ctx, owner, repo, labels, prune); err != nil { fmt.Fprintf(os.Stderr, "unable to sync labels: %v\n", err) os.Exit(1) } diff --git a/pkg/github/github.go b/pkg/github/github.go index ffe4ef2..361a450 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -16,6 +16,7 @@ package github import ( "context" + "fmt" "io/ioutil" "github.com/google/go-github/github" @@ -56,7 +57,7 @@ func NewClient(token string) *Client { } } -func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []Label) error { +func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []Label, prune bool) error { labelMap := make(map[string]Label) for _, l := range labels { labelMap[l.Name] = l @@ -74,19 +75,21 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La eg := errgroup.Group{} // Delete labels. - for _, currentLabel := range currentLabels { - currentLabel := currentLabel - eg.Go(func() error { - _, ok := labelMap[currentLabel.Name] - if ok { - return nil - } - return c.deleteLabel(ctx, owner, repo, currentLabel.Name) - }) - } + if prune { + for _, currentLabel := range currentLabels { + currentLabel := currentLabel + eg.Go(func() error { + _, ok := labelMap[currentLabel.Name] + if ok { + return nil + } + return c.deleteLabel(ctx, owner, repo, currentLabel.Name) + }) + } - if err := eg.Wait(); err != nil { - return err + if err := eg.Wait(); err != nil { + return err + } } // Create and/or update labels. @@ -100,6 +103,7 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La if currentLabel.Description != l.Description || currentLabel.Color != l.Color { return c.updateLabel(ctx, owner, repo, l) } + fmt.Printf("label: %+v not changed on %s/%s\n", l, owner, repo) return nil }) } @@ -114,6 +118,7 @@ func (c *Client) createLabel(ctx context.Context, owner, repo string, label Labe Color: &label.Color, } _, _, err := c.githubClient.Issues.CreateLabel(ctx, owner, repo, l) + fmt.Printf("label: %+v created on: %s/%s\n", label, owner, repo) return err } @@ -149,10 +154,12 @@ func (c *Client) updateLabel(ctx context.Context, owner, repo string, label Labe Color: &label.Color, } _, _, err := c.githubClient.Issues.EditLabel(ctx, owner, repo, label.Name, l) + fmt.Printf("label %+v updated on: %s/%s\n", label, owner, repo) return err } func (c *Client) deleteLabel(ctx context.Context, owner, repo, name string) error { _, err := c.githubClient.Issues.DeleteLabel(ctx, owner, repo, name) + fmt.Printf("label: %s deleted from: %s/%s\n", name, owner, repo) return err } From 9198ceb29504af0e33ffdc610880091305ad78ef Mon Sep 17 00:00:00 2001 From: micnncim Date: Fri, 30 Oct 2020 14:54:43 +0900 Subject: [PATCH 2/2] Fix action description --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index f938198..260fafe 100644 --- a/action.yml +++ b/action.yml @@ -13,7 +13,7 @@ inputs: description: "An alternative GitHub token to use instead" required: false prune: - description: "remove unmanaged labels from repository" + description: "Remove unmanaged labels from repository" required: false default: true runs: