Skip to content

Commit

Permalink
Allow label renaming
Browse files Browse the repository at this point in the history
Introduce the "alias" attribute for labels so that label-syncer can
rename labels, rather than deleting/re-creating them. This preserves
labels on existing issues and pull requests.

Closes micnncim#59
  • Loading branch information
larsks committed May 27, 2021
1 parent 3abd5ab commit f7052b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ The default file path is `.github/labels.yml`, but you can specify any file path

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

### Renaming labels

If you want to rename a label, you can set an `alias` in the manifest.
For example, if you want to rename the label `bug` to `Type: bug`, you
would use a manifest like this:

```yaml
- name: Type: bug
alias: bug
description: Something isn't working
color: d73a4a
```

Renaming labels makes it easier to adopt a new taxonomy if you have
issues and pull requests using the old label names. Since you're
renaming labels rather than deleting and creating new ones, existing
pull requests and issues will keep their labels, but will adopt the
new name.

### Create Workflow

An example workflow is here.
Expand Down
26 changes: 19 additions & 7 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Client struct {

type Label struct {
Name string `yaml:"name"`
Alias string `yaml:"alias"`
Description string `yaml:"description"`
Color string `yaml:"color"`
}
Expand Down Expand Up @@ -59,8 +60,12 @@ func NewClient(token string) *Client {

func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []Label, prune bool) error {
labelMap := make(map[string]Label)
aliasMap := make(map[string]Label)
for _, l := range labels {
labelMap[l.Name] = l
if l.Alias != "" {
aliasMap[l.Alias] = l
}
}

currentLabels, err := c.getLabels(ctx, owner, repo)
Expand All @@ -79,8 +84,9 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La
for _, currentLabel := range currentLabels {
currentLabel := currentLabel
eg.Go(func() error {
_, ok := labelMap[currentLabel.Name]
if ok {
_, name_ok := labelMap[currentLabel.Name]
_, alias_ok := aliasMap[currentLabel.Name]
if (alias_ok && !name_ok) || name_ok {
return nil
}
return c.deleteLabel(ctx, owner, repo, currentLabel.Name)
Expand All @@ -96,12 +102,18 @@ func (c *Client) SyncLabels(ctx context.Context, owner, repo string, labels []La
for _, l := range labels {
l := l
eg.Go(func() error {
labelName := l.Name
currentLabel, ok := currentLabelMap[l.Name]
if !ok {
return c.createLabel(ctx, owner, repo, l)
currentLabel, ok = currentLabelMap[l.Alias]
if !ok {
return c.createLabel(ctx, owner, repo, l)
}
labelName = l.Alias
}
if currentLabel.Description != l.Description || currentLabel.Color != l.Color {
return c.updateLabel(ctx, owner, repo, l)
fmt.Printf("labelName=%s, label=%+v\n", labelName, l)
if currentLabel.Description != l.Description || currentLabel.Color != l.Color || currentLabel.Name != l.Name {
return c.updateLabel(ctx, owner, repo, labelName, l)
}
fmt.Printf("label: %+v not changed on %s/%s\n", l, owner, repo)
return nil
Expand Down Expand Up @@ -147,13 +159,13 @@ func (c *Client) getLabels(ctx context.Context, owner, repo string) ([]Label, er
return labels, nil
}

func (c *Client) updateLabel(ctx context.Context, owner, repo string, label Label) error {
func (c *Client) updateLabel(ctx context.Context, owner, repo, labelName string, label Label) error {
l := &github.Label{
Name: &label.Name,
Description: &label.Description,
Color: &label.Color,
}
_, _, err := c.githubClient.Issues.EditLabel(ctx, owner, repo, label.Name, l)
_, _, err := c.githubClient.Issues.EditLabel(ctx, owner, repo, labelName, l)
fmt.Printf("label %+v updated on: %s/%s\n", label, owner, repo)
return err
}
Expand Down

0 comments on commit f7052b4

Please sign in to comment.