Skip to content

Commit

Permalink
Sync labels between repositories (#2)
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
SIMULATAN committed Dec 15, 2024
1 parent fc69a4f commit cfe8fc4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
22 changes: 22 additions & 0 deletions client/github/github_labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package github

import (
"context"
"github.com/google/go-github/v67/github"
"propromo/utils"
)

func ListGitHubLabels(repo utils.GitRepo) ([]*github.Label, error) {
ctx := context.Background()

labels, _, err := client.Issues.ListLabels(ctx, repo.Owner, repo.Repository, nil)
return labels, err
}

func CreateGitHubLabel(token string, repo utils.GitRepo, label *github.Label) (*github.Label, error) {
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)

label, _, err := client.Issues.CreateLabel(ctx, repo.Owner, repo.Repository, label)
return label, err
}
1 change: 1 addition & 0 deletions cmd/github/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var targets []string

func init() {
syncCmd.AddCommand(milestonesCmd)
syncCmd.AddCommand(labelsCmd)
}

func getToken() string {
Expand Down
54 changes: 54 additions & 0 deletions cmd/github/sync_labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"errors"
"fmt"
githubmodel "github.com/google/go-github/v67/github"
"github.com/spf13/cobra"
"propromo/client/github"
"propromo/cmdutils"
"propromo/utils"
)

var labelsCmd = &cobra.Command{
Use: "labels",
Short: "Sync labels from one repo to others",
Run: func(command *cobra.Command, args []string) {
fmt.Println("Syncing from", source, "to", targets)

sourceRepo, err := utils.ParseGitRepo(source)
cobra.CheckErr(err)

labels, err := github.ListGitHubLabels(sourceRepo)
cobra.CheckErr(err)

repos := make([]utils.GitRepo, len(targets))
for i, target := range targets {
repo, err := utils.ParseGitRepo(target)
cobra.CheckErr(err)
repos[i] = repo
}

token := getToken()
for _, repo := range repos {
for _, label := range labels {
_, err := github.CreateGitHubLabel(token, repo, &githubmodel.Label{
Name: label.Name,
Description: label.Description,
Color: label.Color,
})

var ghErr *githubmodel.ErrorResponse
if errors.As(err, &ghErr) && len(ghErr.Errors) == 1 && ghErr.Errors[0].Code == "already_exists" {
cmdutils.Logger.Info("Label already exists!", "repo", repo.String(), "label", *label.Name)
continue
}
cmdutils.Logger.Info("Label successfully created", "repo", repo.String(), "label", *label.Name)
}
}
},
}

func init() {
syncFlags(labelsCmd)
}

0 comments on commit cfe8fc4

Please sign in to comment.