Use gh auth token for default GITHUB_TOKEN secret (#2651)

* initial version
This commit is contained in:
ChristopherHX
2025-02-09 04:24:32 +01:00
committed by GitHub
parent 101132dc86
commit cb26fd8670
3 changed files with 61 additions and 0 deletions

40
pkg/gh/gh.go Normal file
View File

@@ -0,0 +1,40 @@
package gh
import (
"bufio"
"bytes"
"context"
"os/exec"
)
func GetToken(ctx context.Context, workingDirectory string) (string, error) {
var token string
// Locate the 'gh' executable
path, err := exec.LookPath("gh")
if err != nil {
return "", err
}
// Command setup
cmd := exec.CommandContext(ctx, path, "auth", "token")
cmd.Dir = workingDirectory
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
// Run the command
err = cmd.Run()
if err != nil {
return "", err
}
// Read the first line of the output
scanner := bufio.NewScanner(&out)
if scanner.Scan() {
token = scanner.Text()
}
return token, nil
}

11
pkg/gh/gh_test.go Normal file
View File

@@ -0,0 +1,11 @@
package gh
import (
"context"
"testing"
)
func TestGetToken(t *testing.T) {
token, _ := GetToken(context.TODO(), "")
t.Log(token)
}