|
| 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