Skip to content

Commit 566f702

Browse files
committed
Improve ignore globbing and cache GH app tokens
1 parent 1a9a36b commit 566f702

4 files changed

Lines changed: 34 additions & 7 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/Microsoft/go-winio v0.6.2 // indirect
1616
github.com/ProtonMail/go-crypto v1.1.6 // indirect
1717
github.com/alicebob/miniredis/v2 v2.36.1 // indirect
18+
github.com/bmatcuk/doublestar/v4 v4.10.0 // indirect
1819
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1920
github.com/cloudflare/circl v1.6.1 // indirect
2021
github.com/cyphar/filepath-securejoin v0.4.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI
1111
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
1212
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
1313
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
14+
github.com/bmatcuk/doublestar/v4 v4.10.0 h1:zU9WiOla1YA122oLM6i4EXvGW62DvKZVxIe6TYWexEs=
15+
github.com/bmatcuk/doublestar/v4 v4.10.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
1416
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
1517
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
1618
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=

internal/gitauth/github_app.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ import (
88
"net/http"
99
"os"
1010
"strings"
11+
"sync"
1112
"time"
1213

1314
"github.com/cbrown132/driftd/internal/config"
1415
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
1516
"github.com/golang-jwt/jwt/v5"
1617
)
1718

19+
type appTokenCache struct {
20+
mu sync.Mutex
21+
token string
22+
expiry time.Time
23+
}
24+
25+
var tokenCache sync.Map
26+
1827
func githubAppAuth(ctx context.Context, cfg *config.GitAuthConfig) (*githttp.BasicAuth, error) {
1928
if cfg.GitHubApp == nil {
2029
return nil, fmt.Errorf("github_app config required")
@@ -38,6 +47,18 @@ func githubAppToken(ctx context.Context, cfg *config.GitHubAppConfig) (string, e
3847
return "", fmt.Errorf("github_app app_id and installation_id required")
3948
}
4049

50+
cacheKey := fmt.Sprintf("%d:%d", cfg.AppID, cfg.InstallationID)
51+
if cached, ok := tokenCache.Load(cacheKey); ok {
52+
c := cached.(*appTokenCache)
53+
c.mu.Lock()
54+
if c.token != "" && time.Until(c.expiry) > 2*time.Minute {
55+
token := c.token
56+
c.mu.Unlock()
57+
return token, nil
58+
}
59+
c.mu.Unlock()
60+
}
61+
4162
key, err := loadPrivateKey(cfg)
4263
if err != nil {
4364
return "", err
@@ -87,6 +108,10 @@ func githubAppToken(ctx context.Context, cfg *config.GitHubAppConfig) (string, e
87108
if body.Token == "" {
88109
return "", fmt.Errorf("github app token missing in response")
89110
}
111+
112+
expiry := time.Now().Add(58 * time.Minute)
113+
c := &appTokenCache{token: body.Token, expiry: expiry}
114+
tokenCache.Store(cacheKey, c)
90115
return body.Token, nil
91116
}
92117

internal/stack/discover.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"io/fs"
55
"path/filepath"
66
"strings"
7+
8+
"github.com/bmatcuk/doublestar/v4"
79
)
810

911
var defaultIgnore = []string{
@@ -86,13 +88,10 @@ func shouldIgnore(path string, patterns []string) bool {
8688
}
8789

8890
func matchGlob(pattern, path string) bool {
89-
if strings.Contains(pattern, "**") {
90-
parts := strings.Split(pattern, "**")
91-
if len(parts) == 2 {
92-
return strings.HasPrefix(path, strings.TrimSuffix(parts[0], "/")) &&
93-
strings.HasSuffix(path, strings.TrimPrefix(parts[1], "/"))
94-
}
91+
ok, err := doublestar.Match(pattern, path)
92+
if err == nil && ok {
93+
return true
9594
}
96-
ok, _ := filepath.Match(pattern, path)
95+
ok, _ = filepath.Match(pattern, path)
9796
return ok
9897
}

0 commit comments

Comments
 (0)