Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use gh auth token for default GITHUB_TOKEN secret #2651

Merged
merged 1 commit into from
Feb 9, 2025
Merged
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
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"github.com/nektos/act/pkg/artifacts"
"github.com/nektos/act/pkg/common"
"github.com/nektos/act/pkg/container"
"github.com/nektos/act/pkg/gh"
"github.com/nektos/act/pkg/model"
"github.com/nektos/act/pkg/runner"
)
Expand Down Expand Up @@ -412,6 +413,15 @@
log.Debugf("Loading secrets from %s", input.Secretfile())
secrets := newSecrets(input.secrets)
_ = readEnvs(input.Secretfile(), secrets)
hasGitHubToken := false
for k := range secrets {
if strings.EqualFold(k, "GITHUB_TOKEN") {
hasGitHubToken = true
}

Check warning on line 420 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L416-L420

Added lines #L416 - L420 were not covered by tests
}
if !hasGitHubToken {
secrets["GITHUB_TOKEN"], _ = gh.GetToken(ctx, "")
}

Check warning on line 424 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L422-L424

Added lines #L422 - L424 were not covered by tests

log.Debugf("Loading vars from %s", input.Varfile())
vars := newSecrets(input.vars)
Expand Down
40 changes: 40 additions & 0 deletions pkg/gh/gh.go
Original file line number Diff line number Diff line change
@@ -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
}

Check warning on line 17 in pkg/gh/gh.go

View check run for this annotation

Codecov / codecov/patch

pkg/gh/gh.go#L16-L17

Added lines #L16 - L17 were not covered by tests

// 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()
}

Check warning on line 37 in pkg/gh/gh.go

View check run for this annotation

Codecov / codecov/patch

pkg/gh/gh.go#L34-L37

Added lines #L34 - L37 were not covered by tests

return token, nil

Check warning on line 39 in pkg/gh/gh.go

View check run for this annotation

Codecov / codecov/patch

pkg/gh/gh.go#L39

Added line #L39 was not covered by tests
}
11 changes: 11 additions & 0 deletions pkg/gh/gh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gh

import (
"context"
"testing"
)

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