Skip to content

Commit

Permalink
Add prune option (#48)
Browse files Browse the repository at this point in the history
* Add prune option

prune option indicate if unmanaged labels should be removed,
by default is set to true

* Fix action description

Co-authored-by: micnncim <micnncim@gmail.com>
  • Loading branch information
slawekjaranowski and micnncim authored Oct 30, 2020
1 parent 7f0d8cf commit 0e9c510
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<job_id>.steps.with`.
The default file path is `.github/labels.yml`, but you can specify any file path with `jobs.<job_id>.steps.with.manifest`.

To create manifest of the current labels easily, using [label-exporter](https://github.com/micnncim/label-exporter) is recommended.

Expand Down Expand Up @@ -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.<job_id>.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

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion cmd/action-label-syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"strconv"
"strings"

"github.com/micnncim/action-label-syncer/pkg/github"
Expand Down Expand Up @@ -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)
}
Expand Down
33 changes: 20 additions & 13 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package github

import (
"context"
"fmt"
"io/ioutil"

"github.com/google/go-github/github"
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
})
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit 0e9c510

Please sign in to comment.