Skip to content

Commit cfe8fc4

Browse files
committed
Sync labels between repositories (#2)
Closes #2
1 parent fc69a4f commit cfe8fc4

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

client/github/github_labels.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"github.com/google/go-github/v67/github"
6+
"propromo/utils"
7+
)
8+
9+
func ListGitHubLabels(repo utils.GitRepo) ([]*github.Label, error) {
10+
ctx := context.Background()
11+
12+
labels, _, err := client.Issues.ListLabels(ctx, repo.Owner, repo.Repository, nil)
13+
return labels, err
14+
}
15+
16+
func CreateGitHubLabel(token string, repo utils.GitRepo, label *github.Label) (*github.Label, error) {
17+
ctx := context.Background()
18+
client := github.NewClient(nil).WithAuthToken(token)
19+
20+
label, _, err := client.Issues.CreateLabel(ctx, repo.Owner, repo.Repository, label)
21+
return label, err
22+
}

cmd/github/sync.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var targets []string
1616

1717
func init() {
1818
syncCmd.AddCommand(milestonesCmd)
19+
syncCmd.AddCommand(labelsCmd)
1920
}
2021

2122
func getToken() string {

cmd/github/sync_labels.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
githubmodel "github.com/google/go-github/v67/github"
7+
"github.com/spf13/cobra"
8+
"propromo/client/github"
9+
"propromo/cmdutils"
10+
"propromo/utils"
11+
)
12+
13+
var labelsCmd = &cobra.Command{
14+
Use: "labels",
15+
Short: "Sync labels from one repo to others",
16+
Run: func(command *cobra.Command, args []string) {
17+
fmt.Println("Syncing from", source, "to", targets)
18+
19+
sourceRepo, err := utils.ParseGitRepo(source)
20+
cobra.CheckErr(err)
21+
22+
labels, err := github.ListGitHubLabels(sourceRepo)
23+
cobra.CheckErr(err)
24+
25+
repos := make([]utils.GitRepo, len(targets))
26+
for i, target := range targets {
27+
repo, err := utils.ParseGitRepo(target)
28+
cobra.CheckErr(err)
29+
repos[i] = repo
30+
}
31+
32+
token := getToken()
33+
for _, repo := range repos {
34+
for _, label := range labels {
35+
_, err := github.CreateGitHubLabel(token, repo, &githubmodel.Label{
36+
Name: label.Name,
37+
Description: label.Description,
38+
Color: label.Color,
39+
})
40+
41+
var ghErr *githubmodel.ErrorResponse
42+
if errors.As(err, &ghErr) && len(ghErr.Errors) == 1 && ghErr.Errors[0].Code == "already_exists" {
43+
cmdutils.Logger.Info("Label already exists!", "repo", repo.String(), "label", *label.Name)
44+
continue
45+
}
46+
cmdutils.Logger.Info("Label successfully created", "repo", repo.String(), "label", *label.Name)
47+
}
48+
}
49+
},
50+
}
51+
52+
func init() {
53+
syncFlags(labelsCmd)
54+
}

0 commit comments

Comments
 (0)