Skip to content

Commit

Permalink
new option - deleteUnManaged
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Oct 28, 2020
1 parent 7f0d8cf commit 19660b4
Show file tree
Hide file tree
Showing 4 changed files with 44 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.deleteUnManaged: 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
deleteUnManaged:
description: "delete unmanaged labels from repository"
required: false
default: true
runs:
using: "docker"
image: "Dockerfile"
Expand Down
14 changes: 13 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,19 @@ func main() {
}
owner, repo := slugs[0], slugs[1]

deleteUnManagedStr := os.Getenv("INPUT_DELETEUNMANAGED")
if len(deleteUnManagedStr) == 0 {
deleteUnManagedStr = "true"
}

deleteUnManaged, err := strconv.ParseBool(deleteUnManagedStr)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to parse deleteUnManaged: %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, deleteUnManaged); err != nil {
fmt.Fprintf(os.Stderr, "unable to sync labels: %v\n", err)
os.Exit(1)
}
Expand Down
34 changes: 21 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, deleteUnManaged 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 deleteUnManaged {
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 @@ -108,12 +112,14 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La
}

func (c *Client) createLabel(ctx context.Context, owner, repo string, label Label) error {

l := &github.Label{
Name: &label.Name,
Description: &label.Description,
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 +155,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 19660b4

Please sign in to comment.