Skip to content

implements enhancement #65 exclude labels #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ All issues and PRs that were previously labeled with this label are now unlabele

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.

An other option is to exclude labels from syncing. If you enter `jobs.<job_id>.steps.with.labelExcludePattern: feature:.*` will exclude labels like `feature: new login` from syncing/deleting.

If you want a "dry test run" you can use `jobs.<job_id>.steps.with.dryRun: true` to simulate the create/update or delete action.
## Sync labels on another repository

It is also possible to specify a repository or repositories as an input to the action. This is useful if you want to store your labels somewhere centrally and modify multiple repository labels.
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ inputs:
description: "Remove unmanaged labels from repository"
required: false
default: true
labelExcludePattern:
description: "Regular expression to exclude labels from syncing"
required: false
dryRun:
description: "No label is create/updated or deleted"
required: false
default: false
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 @@ -27,9 +27,14 @@ import (
)

func main() {

fmt.Println("START SYNCER!!!")

if err := run(context.Background()); err != nil {
log.Fatal(err)
}

fmt.Println("END SYNCER!!!")
}

func run(ctx context.Context) error {
Expand All @@ -55,6 +60,13 @@ func run(ctx context.Context) error {
repository = os.Getenv("GITHUB_REPOSITORY")
}

labelExcludePattern := os.Getenv("INPUT_LABELEXCLUDEPATTERN")

dryRun, err := strconv.ParseBool(os.Getenv("INPUT_DRYRUN"))
if err != nil {
return fmt.Errorf("unable to parse dryRun: %w", err)
}

// Doesn't run concurrently to avoid GitHub API rate limit.
for _, r := range strings.Split(repository, "\n") {
if len(r) == 0 {
Expand All @@ -67,7 +79,7 @@ func run(ctx context.Context) error {
}
owner, repo := s[0], s[1]

if err := client.SyncLabels(ctx, owner, repo, labels, prune); err != nil {
if err := client.SyncLabels(ctx, owner, repo, labels, prune, labelExcludePattern, dryRun); err != nil {
err = multierr.Append(err, fmt.Errorf("unable to sync labels: %w", err))
}
}
Expand Down
53 changes: 45 additions & 8 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"io/ioutil"
"regexp"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -57,7 +58,7 @@ func NewClient(token string) *Client {
}
}

func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []Label, prune bool) error {
func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []Label, prune bool, labelExcludePattern string, dryRun bool) error {
labelMap := make(map[string]Label)
for _, l := range labels {
labelMap[l.Name] = l
Expand All @@ -67,6 +68,27 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La
if err != nil {
return err
}

// Exclude current lables from syncing
if len(labelExcludePattern) != 0 {
fmt.Println("Exclude labels via this pattern: " + labelExcludePattern)
var cleanedArray []Label
for _, l := range currentLabels {
labelName := l.Name
matchExclude, err := regexp.MatchString(labelExcludePattern, labelName)
if err != nil {
return err
}
if matchExclude {
fmt.Printf("Exclude Label %+v\n", l)
} else {
fmt.Printf("Sync Label %+v\n", l)
cleanedArray = append(cleanedArray, l)
}
}
currentLabels = cleanedArray
}

currentLabelMap := make(map[string]Label)
for _, l := range currentLabels {
currentLabelMap[l.Name] = l
Expand All @@ -83,7 +105,12 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La
if ok {
return nil
}
return c.deleteLabel(ctx, owner, repo, currentLabel.Name)
if dryRun {
fmt.Printf("DRYRUN: Delete Label %+v\n", currentLabel)
} else {
return c.deleteLabel(ctx, owner, repo, currentLabel.Name)
}
return nil
})
}

Expand All @@ -98,12 +125,22 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La
eg.Go(func() error {
currentLabel, ok := currentLabelMap[l.Name]
if !ok {
return c.createLabel(ctx, owner, repo, l)
if dryRun {
fmt.Printf("DRYRUN: Create Label %+v\n", l)
return nil
} else {
return c.createLabel(ctx, owner, repo, l)
}
}
if currentLabel.Description != l.Description || currentLabel.Color != l.Color {
return c.updateLabel(ctx, owner, repo, l)
if dryRun {
fmt.Printf("DRYRUN: Update Label %+v\n", l)
return nil
} else {
return c.updateLabel(ctx, owner, repo, l)
}
}
fmt.Printf("label: %+v not changed on %s/%s\n", l, owner, repo)
fmt.Printf("not changed label: %+v on %s/%s\n", l, owner, repo)
return nil
})
}
Expand All @@ -118,7 +155,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)
fmt.Printf("created label: %+v on: %s/%s\n", label, owner, repo)
return err
}

Expand Down Expand Up @@ -154,12 +191,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)
fmt.Printf("updated label %+v 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)
fmt.Printf("deleted label: %s from: %s/%s\n", name, owner, repo)
return err
}