Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
if _, hasGitHubToken := secrets["GITHUB_TOKEN"]; !hasGitHubToken {
ctx, cancel := common.EarlyCancelContext(ctx)
defer cancel()
secrets["GITHUB_TOKEN"], _ = gh.GetToken(ctx, "")
secrets["GITHUB_TOKEN"], _ = gh.GetToken(ctx, "", input.githubInstance)
}

log.Debugf("Loading vars from %s", input.Varfile())
Expand Down
9 changes: 7 additions & 2 deletions pkg/gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ import (
"bufio"
"bytes"
"context"
"errors"
"os/exec"
)

func GetToken(ctx context.Context, workingDirectory string) (string, error) {
func GetToken(ctx context.Context, workingDirectory string, hostname string) (string, error) {
var token string

if hostname == "" {
return "", errors.New("hostname must not be empty")
}

// Locate the 'gh' executable
path, err := exec.LookPath("gh")
if err != nil {
return "", err
}

// Command setup
cmd := exec.CommandContext(ctx, path, "auth", "token")
cmd := exec.CommandContext(ctx, path, "auth", "token", "--hostname", hostname)
cmd.Dir = workingDirectory

// Capture the output
Expand Down
14 changes: 13 additions & 1 deletion pkg/gh/gh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ package gh
import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetToken(t *testing.T) {
token, _ := GetToken(context.TODO(), "")
token, _ := GetToken(context.TODO(), "", "github.com")
t.Log(token)
}

func TestGetTokenWithUnmatchedHostname(t *testing.T) {
_, err := GetToken(context.TODO(), "", "notgithub.example.com")
assert.Error(t, err)
}

func TestGetTokenWithNoHostname(t *testing.T) {
_, err := GetToken(context.TODO(), "", "")
assert.EqualError(t, err, "hostname must not be empty")
}