-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync labels between repositories (#2)
Closes #2
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |