Skip to content

Commit

Permalink
[MER-2188] Use gh auth token if it's available (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
draftcode authored Aug 22, 2023
1 parent 07e2de0 commit 1070c30
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
3 changes: 1 addition & 2 deletions cmd/av/commit_amend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"emperror.dev/errors"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/colors"
Expand Down Expand Up @@ -69,7 +68,7 @@ var commitAmendCmd = &cobra.Command{
tx := db.WriteTx()
defer tx.Abort()

client, err := getClient(config.Av.GitHub.Token)
client, err := getGitHubClient()
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/av/commit_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"emperror.dev/errors"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/colors"
Expand Down Expand Up @@ -88,7 +87,7 @@ func commitCreate(repo *git.Repo, currentBranchName string, flags struct {
tx := db.WriteTx()
defer tx.Abort()

client, err := getClient(config.Av.GitHub.Token)
client, err := getGitHubClient()
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/av/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"emperror.dev/errors"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/gh"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/cleanup"
Expand Down Expand Up @@ -45,7 +44,7 @@ var fetchCmd = &cobra.Command{
}
branches := tx.AllBranches()

client, err := getClient(config.Av.GitHub.Token)
client, err := getGitHubClient()
if err != nil {
return err
}
Expand Down
7 changes: 1 addition & 6 deletions cmd/av/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

"emperror.dev/errors"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/cleanup"
"github.com/sirupsen/logrus"
Expand All @@ -31,10 +29,7 @@ var initCmd = &cobra.Command{
})
defer cu.Cleanup()

if config.Av.GitHub.Token == "" {
return errors.New("github token must be set")
}
client, err := getClient(config.Av.GitHub.Token)
client, err := getGitHubClient()
if err != nil {
return err
}
Expand Down
25 changes: 24 additions & 1 deletion cmd/av/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -149,7 +152,27 @@ func checkCliVersion() {
var once sync.Once
var lazyGithubClient *gh.Client

func getClient(token string) (*gh.Client, error) {
func discoverGitHubAPIToken() string {
if config.Av.GitHub.Token != "" {
return config.Av.GitHub.Token
}
if ghCli, err := exec.LookPath("gh"); err == nil {
var stdout bytes.Buffer
cmd := exec.Command(ghCli, "auth", "token")
cmd.Stdout = &stdout
cmd.Stderr = nil
if err := cmd.Run(); err == nil {
return strings.TrimSpace(stdout.String())
}
}
return ""
}

func getGitHubClient() (*gh.Client, error) {
token := discoverGitHubAPIToken()
if token == "" {
return nil, errors.New("github token must be set")
}
var err error
once.Do(func() {
lazyGithubClient, err = gh.NewClient(token)
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/pr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Examples:
if err != nil {
return errors.WrapIf(err, "failed to determine current branch")
}
client, err := getClient(config.Av.GitHub.Token)
client, err := getGitHubClient()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/stack_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var stackSubmitCmd = &cobra.Command{

// ensure pull requests for each branch in the stack
ctx := context.Background()
client, err := getClient(config.Av.GitHub.Token)
client, err := getGitHubClient()
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/av/stack_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"emperror.dev/errors"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/config"
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/colors"
Expand Down Expand Up @@ -241,8 +240,7 @@ base branch.
// Either way (--continue or not), we sync all subsequent branches

logrus.WithField("branches", branchesToSync).Debug("determined branches to sync")
//var resErr error
client, err := getClient(config.Av.GitHub.Token)
client, err := getGitHubClient()
if err != nil {
return err
}
Expand Down

0 comments on commit 1070c30

Please sign in to comment.