Skip to content

Commit add90d9

Browse files
committed
#4 support authentication for listing labels & milestones
Fixes #4
1 parent 7aa8009 commit add90d9

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

client/github/github_client.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package github
22

3-
import "github.com/google/go-github/v67/github"
3+
import (
4+
"github.com/google/go-github/v67/github"
5+
"propromo/cmdutils"
6+
)
47

5-
var client = github.NewClient(nil)
8+
func NewGitHubClient(token string) *github.Client {
9+
return github.NewClient(nil).WithAuthToken(token)
10+
}
11+
12+
func NewGitHubClientWithOptionalToken(token *string) *github.Client {
13+
client := github.NewClient(nil)
14+
if token != nil {
15+
nonNilToken := *token
16+
if nonNilToken == "" {
17+
cmdutils.Logger.Fatal("Empty token passed. Either supply `nil` or a valid GitHub PAT.")
18+
}
19+
client = client.WithAuthToken(nonNilToken)
20+
}
21+
return client
22+
}

client/github/github_labels.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import (
66
"propromo/utils"
77
)
88

9-
func ListGitHubLabels(repo utils.GitRepo) ([]*github.Label, error) {
9+
func ListGitHubLabels(token *string, repo utils.GitRepo) ([]*github.Label, error) {
1010
ctx := context.Background()
11+
client := NewGitHubClientWithOptionalToken(token)
1112

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

1617
func CreateGitHubLabel(token string, repo utils.GitRepo, label *github.Label) (*github.Label, error) {
1718
ctx := context.Background()
18-
client := github.NewClient(nil).WithAuthToken(token)
19+
client := NewGitHubClient(token)
1920

2021
label, _, err := client.Issues.CreateLabel(ctx, repo.Owner, repo.Repository, label)
2122
return label, err

client/github/github_milestones.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import (
66
"propromo/utils"
77
)
88

9-
func ListGitHubMilestones(repo utils.GitRepo) ([]*github.Milestone, error) {
9+
func ListGitHubMilestones(token *string, repo utils.GitRepo) ([]*github.Milestone, error) {
1010
ctx := context.Background()
11+
client := NewGitHubClientWithOptionalToken(token)
1112

1213
milestones, _, err := client.Issues.ListMilestones(ctx, repo.Owner, repo.Repository, nil)
1314
return milestones, err
1415
}
1516

1617
func CreateGitHubMilestone(token string, repo utils.GitRepo, milestone *github.Milestone) (*github.Milestone, error) {
1718
ctx := context.Background()
18-
client := github.NewClient(nil).WithAuthToken(token)
19+
client := NewGitHubClient(token)
1920

2021
milestones, _, err := client.Issues.CreateMilestone(ctx, repo.Owner, repo.Repository, milestone)
2122
return milestones, err

cmd/github/sync_labels.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ var labelsCmd = &cobra.Command{
1414
Use: "labels",
1515
Short: "Sync labels from one repo to others",
1616
Run: func(command *cobra.Command, args []string) {
17+
token := getToken()
18+
1719
fmt.Println("Syncing from", source, "to", targets)
1820

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

22-
labels, err := github.ListGitHubLabels(sourceRepo)
24+
labels, err := github.ListGitHubLabels(&token, sourceRepo)
2325
cobra.CheckErr(err)
2426
if len(labels) == 0 {
2527
cmdutils.Logger.Warn("No Labels in source repo found, aborting sync.")
@@ -33,7 +35,6 @@ var labelsCmd = &cobra.Command{
3335
repos[i] = repo
3436
}
3537

36-
token := getToken()
3738
for _, repo := range repos {
3839
for _, label := range labels {
3940
_, err := github.CreateGitHubLabel(token, repo, &githubmodel.Label{

cmd/github/sync_milestones.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ var milestonesCmd = &cobra.Command{
1414
Use: "milestones",
1515
Short: "Sync milestones from one repo to others",
1616
Run: func(command *cobra.Command, args []string) {
17+
token := getToken()
1718
fmt.Println("Syncing from", source, "to", targets)
1819

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

22-
milestones, err := github.ListGitHubMilestones(sourceRepo)
23+
milestones, err := github.ListGitHubMilestones(&token, sourceRepo)
2324
cobra.CheckErr(err)
2425
if len(milestones) == 0 {
2526
cmdutils.Logger.Warn("No Milestones in source repo found, aborting sync.")
@@ -33,7 +34,6 @@ var milestonesCmd = &cobra.Command{
3334
repos[i] = repo
3435
}
3536

36-
token := getToken()
3737
for _, repo := range repos {
3838
for _, milestone := range milestones {
3939
_, err := github.CreateGitHubMilestone(token, repo, &githubmodel.Milestone{

0 commit comments

Comments
 (0)