Skip to content

Commit f82f723

Browse files
committed
fix: align github scm polling with legacy optimizations
1 parent 6b521a1 commit f82f723

4 files changed

Lines changed: 612 additions & 104 deletions

File tree

‎backend/internal/adapters/scm/github/auth.go‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"crypto/sha256"
66
"encoding/hex"
77
"errors"
8-
"os"
98
"os/exec"
109
"strings"
10+
"sync"
1111
)
1212

1313
// TokenSource supplies GitHub bearer tokens. It is intentionally tiny so tests
14-
// can inject a static token and production can use env/gh fallback.
14+
// can inject a static token and production can use gh auth.
1515
type TokenSource interface {
1616
Token(ctx context.Context) (string, error)
1717
}
@@ -27,30 +27,37 @@ func (s StaticTokenSource) Token(context.Context) (string, error) {
2727

2828
var ErrNoToken = errors.New("github scm: no token")
2929

30-
type EnvTokenSource struct {
31-
// EnvVars are checked before GITHUB_TOKEN. Values may name env vars whose
32-
// values hold tokens, preserving the issue's configured-project-token order.
33-
EnvVars []string
34-
AllowGH bool
30+
// GHTokenSource uses `gh auth token` as the sole default production credential
31+
// source. The token is memoized because every REST/GraphQL request asks the
32+
// client for a token.
33+
type GHTokenSource struct {
34+
mu sync.Mutex
35+
token string
36+
Command func(context.Context) ([]byte, error)
3537
}
3638

37-
func (s EnvTokenSource) Token(ctx context.Context) (string, error) {
38-
for _, name := range s.EnvVars {
39-
if v := strings.TrimSpace(os.Getenv(name)); v != "" {
40-
return v, nil
39+
func (s *GHTokenSource) Token(ctx context.Context) (string, error) {
40+
s.mu.Lock()
41+
defer s.mu.Unlock()
42+
if s.token != "" {
43+
return s.token, nil
44+
}
45+
run := s.Command
46+
if run == nil {
47+
run = func(ctx context.Context) ([]byte, error) {
48+
return exec.CommandContext(ctx, "gh", "auth", "token").Output()
4149
}
4250
}
43-
if v := strings.TrimSpace(os.Getenv("GITHUB_TOKEN")); v != "" {
44-
return v, nil
51+
out, err := run(ctx)
52+
if err != nil {
53+
return "", err
4554
}
46-
if s.AllowGH {
47-
cmd := exec.CommandContext(ctx, "gh", "auth", "token")
48-
out, err := cmd.Output()
49-
if err == nil && strings.TrimSpace(string(out)) != "" {
50-
return strings.TrimSpace(string(out)), nil
51-
}
55+
token := strings.TrimSpace(string(out))
56+
if token == "" {
57+
return "", ErrNoToken
5258
}
53-
return "", ErrNoToken
59+
s.token = token
60+
return token, nil
5461
}
5562

5663
func credentialHash(token string) string {

‎backend/internal/adapters/scm/github/client.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewClient(opts ClientOptions) *Client {
4444
c.httpClient = http.DefaultClient
4545
}
4646
if c.tokens == nil {
47-
c.tokens = EnvTokenSource{AllowGH: true}
47+
c.tokens = &GHTokenSource{}
4848
}
4949
if c.restBase == "" {
5050
c.restBase = defaultRESTBaseURL

0 commit comments

Comments
 (0)