Skip to content

Commit

Permalink
#4 support authentication for listing labels & milestones
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
SIMULATAN committed Dec 15, 2024
1 parent 7aa8009 commit add90d9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
21 changes: 19 additions & 2 deletions client/github/github_client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package github

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

var client = github.NewClient(nil)
func NewGitHubClient(token string) *github.Client {
return github.NewClient(nil).WithAuthToken(token)
}

func NewGitHubClientWithOptionalToken(token *string) *github.Client {
client := github.NewClient(nil)
if token != nil {
nonNilToken := *token
if nonNilToken == "" {
cmdutils.Logger.Fatal("Empty token passed. Either supply `nil` or a valid GitHub PAT.")
}
client = client.WithAuthToken(nonNilToken)
}
return client
}
5 changes: 3 additions & 2 deletions client/github/github_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"propromo/utils"
)

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

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)
client := NewGitHubClient(token)

label, _, err := client.Issues.CreateLabel(ctx, repo.Owner, repo.Repository, label)
return label, err
Expand Down
5 changes: 3 additions & 2 deletions client/github/github_milestones.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"propromo/utils"
)

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

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

func CreateGitHubMilestone(token string, repo utils.GitRepo, milestone *github.Milestone) (*github.Milestone, error) {
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client := NewGitHubClient(token)

milestones, _, err := client.Issues.CreateMilestone(ctx, repo.Owner, repo.Repository, milestone)
return milestones, err
Expand Down
5 changes: 3 additions & 2 deletions cmd/github/sync_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ var labelsCmd = &cobra.Command{
Use: "labels",
Short: "Sync labels from one repo to others",
Run: func(command *cobra.Command, args []string) {
token := getToken()

fmt.Println("Syncing from", source, "to", targets)

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

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

token := getToken()
for _, repo := range repos {
for _, label := range labels {
_, err := github.CreateGitHubLabel(token, repo, &githubmodel.Label{
Expand Down
4 changes: 2 additions & 2 deletions cmd/github/sync_milestones.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ var milestonesCmd = &cobra.Command{
Use: "milestones",
Short: "Sync milestones from one repo to others",
Run: func(command *cobra.Command, args []string) {
token := getToken()
fmt.Println("Syncing from", source, "to", targets)

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

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

token := getToken()
for _, repo := range repos {
for _, milestone := range milestones {
_, err := github.CreateGitHubMilestone(token, repo, &githubmodel.Milestone{
Expand Down

0 comments on commit add90d9

Please sign in to comment.