Skip to content

Commit 587c100

Browse files
authored
Merge pull request #14 from codeGROOVE-dev/others
change api to use prx.Fetch
2 parents f6dbdf2 + b904457 commit 587c100

39 files changed

+3342
-852
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ issues:
1313
max-issues-per-linter: 0
1414
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
1515
max-same-issues: 0
16+
exclude-rules:
17+
- path: pkg/prx/types/
18+
text: "var-naming: avoid meaningless package names"
1619

1720
formatters:
1821
enable:
@@ -188,6 +191,8 @@ linters:
188191
disabled: true
189192
- name: bare-return
190193
disabled: true
194+
- name: var-naming
195+
disabled: true # types is a perfectly valid package name
191196

192197
rowserrcheck:
193198
# database/sql is always checked.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Go library for fetching pull request data from GitHub, GitLab, and Gitea/Codeber
55
## Quick Start
66

77
```go
8-
import "github.com/codeGROOVE-dev/prx/pkg/prx/fetch"
8+
import "github.com/codeGROOVE-dev/prx/pkg/prx"
99

10-
data, err := fetch.Fetch(ctx, "https://github.com/golang/go/pull/12345")
10+
data, err := prx.Fetch(ctx, "https://github.com/golang/go/pull/12345")
1111
// Works with: GitHub, GitLab, Codeberg, self-hosted instances
1212
```
1313
Auto-detects platform and resolves authentication from `GITHUB_TOKEN`/`GITLAB_TOKEN`/`GITEA_TOKEN` or CLI tools (`gh`, `glab`, `tea`, `berg`).

cmd/prx/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/codeGROOVE-dev/prx/pkg/prx/gitea"
1919
"github.com/codeGROOVE-dev/prx/pkg/prx/github"
2020
"github.com/codeGROOVE-dev/prx/pkg/prx/gitlab"
21+
"github.com/codeGROOVE-dev/prx/pkg/prx/types"
2122
)
2223

2324
var (
@@ -59,7 +60,7 @@ func run() error {
5960

6061
prURL := flag.Arg(0)
6162

62-
parsed, err := prx.ParseURL(prURL)
63+
parsed, err := types.ParseURL(prURL)
6364
if err != nil {
6465
return fmt.Errorf("invalid PR URL: %w", err)
6566
}
@@ -74,7 +75,7 @@ func run() error {
7475
token, err := resolver.Resolve(ctx, platform, parsed.Host)
7576
// Authentication is optional for public repos on GitLab/Gitea/Codeberg
7677
// Only GitHub strictly requires authentication for most API calls
77-
tokenOptional := parsed.Platform != prx.PlatformGitHub
78+
tokenOptional := parsed.Platform != types.PlatformGitHub
7879

7980
if err != nil && !tokenOptional {
8081
return fmt.Errorf("authentication failed: %w", err)
@@ -89,13 +90,13 @@ func run() error {
8990
}
9091

9192
// Create platform-specific client
92-
var prxPlatform prx.Platform
93+
var prxPlatform types.Platform
9394
switch parsed.Platform {
94-
case prx.PlatformGitHub:
95+
case types.PlatformGitHub:
9596
prxPlatform = github.NewPlatform(tokenValue)
96-
case prx.PlatformGitLab:
97+
case types.PlatformGitLab:
9798
prxPlatform = gitlab.NewPlatform(tokenValue, gitlab.WithBaseURL("https://"+parsed.Host))
98-
case prx.PlatformCodeberg:
99+
case types.PlatformCodeberg:
99100
prxPlatform = gitea.NewCodebergPlatform(tokenValue)
100101
default:
101102
// Self-hosted Gitea
@@ -108,7 +109,7 @@ func run() error {
108109
opts = append(opts, prx.WithLogger(slog.Default()))
109110
}
110111
if *noCache {
111-
opts = append(opts, prx.WithCacheStore(null.New[string, prx.PullRequestData]()))
112+
opts = append(opts, prx.WithCacheStore(null.New[string, types.PullRequestData]()))
112113
}
113114

114115
client := prx.NewClientWithPlatform(prxPlatform, opts...)

cmd/prx_compare/main.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/codeGROOVE-dev/prx/pkg/prx"
1616
"github.com/codeGROOVE-dev/prx/pkg/prx/github"
17+
"github.com/codeGROOVE-dev/prx/pkg/prx/types"
1718
)
1819

1920
const (
@@ -63,7 +64,7 @@ func main() {
6364
fmt.Println("\nFull data saved to rest_output.json and graphql_output.json")
6465
}
6566

66-
func comparePullRequestData(rest, graphql *prx.PullRequestData) {
67+
func comparePullRequestData(rest, graphql *types.PullRequestData) {
6768
// Compare PullRequest fields
6869
fmt.Println("=== Pull Request Metadata ===")
6970
comparePullRequest(&rest.PullRequest, &graphql.PullRequest)
@@ -73,7 +74,7 @@ func comparePullRequestData(rest, graphql *prx.PullRequestData) {
7374
compareEvents(rest.Events, graphql.Events)
7475
}
7576

76-
func comparePullRequest(rest, graphql *prx.PullRequest) {
77+
func comparePullRequest(rest, graphql *types.PullRequest) {
7778
differences, matches := compareFields(rest, graphql)
7879

7980
if len(differences) > 0 {
@@ -86,7 +87,7 @@ func comparePullRequest(rest, graphql *prx.PullRequest) {
8687
fmt.Printf("\nMatching fields: %s\n", strings.Join(matches, ", "))
8788
}
8889

89-
func compareFields(rest, graphql *prx.PullRequest) (differences, matches []string) {
90+
func compareFields(rest, graphql *types.PullRequest) (differences, matches []string) {
9091
restVal := reflect.ValueOf(*rest)
9192
graphqlVal := reflect.ValueOf(*graphql)
9293
restType := restVal.Type()
@@ -131,7 +132,7 @@ func comparePointerField(name string, restField, graphqlField reflect.Value) str
131132
return ""
132133
}
133134

134-
func compareCheckSummary(rest, graphql *prx.PullRequest) {
135+
func compareCheckSummary(rest, graphql *types.PullRequest) {
135136
if rest.CheckSummary == nil || graphql.CheckSummary == nil {
136137
return
137138
}
@@ -149,7 +150,7 @@ func compareCheckSummary(rest, graphql *prx.PullRequest) {
149150
compareCheckSummaryMaps(rest.CheckSummary, graphql.CheckSummary)
150151
}
151152

152-
func compareCheckSummaryMaps(rest, graphql *prx.CheckSummary) {
153+
func compareCheckSummaryMaps(rest, graphql *types.CheckSummary) {
153154
compareSummaryMap("Success", rest.Success, graphql.Success)
154155
compareSummaryMap("Failing", rest.Failing, graphql.Failing)
155156
compareSummaryMap("Pending", rest.Pending, graphql.Pending)
@@ -186,7 +187,7 @@ func compareStatusMaps(rest, graphql map[string]string) {
186187
}
187188
}
188189

189-
func compareEvents(restEvents, graphqlEvents []prx.Event) {
190+
func compareEvents(restEvents, graphqlEvents []types.Event) {
190191
// Count events by type
191192
restCounts := countEventsByType(restEvents)
192193
graphqlCounts := countEventsByType(graphqlEvents)
@@ -200,13 +201,13 @@ func compareEvents(restEvents, graphqlEvents []prx.Event) {
200201
allTypes[k] = true
201202
}
202203

203-
var types []string
204+
var eventTypes []string
204205
for t := range allTypes {
205-
types = append(types, t)
206+
eventTypes = append(eventTypes, t)
206207
}
207-
sort.Strings(types)
208+
sort.Strings(eventTypes)
208209

209-
for _, eventType := range types {
210+
for _, eventType := range eventTypes {
210211
restCount := restCounts[eventType]
211212
graphqlCount := graphqlCounts[eventType]
212213
if restCount != graphqlCount {
@@ -226,7 +227,7 @@ func compareEvents(restEvents, graphqlEvents []prx.Event) {
226227
restByType := groupEventsByType(restEvents)
227228
graphqlByType := groupEventsByType(graphqlEvents)
228229

229-
for _, eventType := range types {
230+
for _, eventType := range eventTypes {
230231
restTypeEvents := restByType[eventType]
231232
graphqlTypeEvents := graphqlByType[eventType]
232233

@@ -294,23 +295,23 @@ func compareEvents(restEvents, graphqlEvents []prx.Event) {
294295
}
295296
}
296297

297-
func countEventsByType(events []prx.Event) map[string]int {
298+
func countEventsByType(events []types.Event) map[string]int {
298299
counts := make(map[string]int)
299300
for i := range events {
300301
counts[events[i].Kind]++
301302
}
302303
return counts
303304
}
304305

305-
func groupEventsByType(events []prx.Event) map[string][]prx.Event {
306-
grouped := make(map[string][]prx.Event)
306+
func groupEventsByType(events []types.Event) map[string][]types.Event {
307+
grouped := make(map[string][]types.Event)
307308
for i := range events {
308309
grouped[events[i].Kind] = append(grouped[events[i].Kind], events[i])
309310
}
310311
return grouped
311312
}
312313

313-
func extractWriteAccess(events []prx.Event) map[string]int {
314+
func extractWriteAccess(events []types.Event) map[string]int {
314315
access := make(map[string]int)
315316
for i := range events {
316317
e := &events[i]
@@ -324,7 +325,7 @@ func extractWriteAccess(events []prx.Event) map[string]int {
324325
return access
325326
}
326327

327-
func extractBots(events []prx.Event) map[string]bool {
328+
func extractBots(events []types.Event) map[string]bool {
328329
bots := make(map[string]bool)
329330
for i := range events {
330331
e := &events[i]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ require (
77
github.com/codeGROOVE-dev/fido/pkg/store/localfs v1.10.0
88
github.com/codeGROOVE-dev/fido/pkg/store/null v1.10.0
99
github.com/codeGROOVE-dev/retry v1.3.1
10+
gopkg.in/yaml.v3 v3.0.1
1011
)
1112

1213
require (
1314
github.com/codeGROOVE-dev/fido/pkg/store/compress v1.10.0 // indirect
1415
github.com/klauspost/compress v1.18.2 // indirect
1516
github.com/puzpuzpuz/xsync/v4 v4.2.0 // indirect
16-
gopkg.in/yaml.v3 v3.0.1 // indirect
1717
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU
1414
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
1515
github.com/puzpuzpuz/xsync/v4 v4.2.0 h1:dlxm77dZj2c3rxq0/XNvvUKISAmovoXF4a4qM6Wvkr0=
1616
github.com/puzpuzpuz/xsync/v4 v4.2.0/go.mod h1:VJDmTCJMBt8igNxnkQd86r+8KUeN1quSfNKu5bLYFQo=
17+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1718
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1819
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1920
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/prx/client.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/codeGROOVE-dev/fido"
2020
"github.com/codeGROOVE-dev/fido/pkg/store/localfs"
21+
"github.com/codeGROOVE-dev/prx/pkg/prx/types"
2122
)
2223

2324
const (
@@ -29,13 +30,13 @@ const (
2930

3031
// PRStore is the interface for PR cache storage backends.
3132
// This is an alias for fido.Store with the appropriate type parameters.
32-
type PRStore = fido.Store[string, PullRequestData]
33+
type PRStore = fido.Store[string, types.PullRequestData]
3334

3435
// Client provides methods to fetch pull request events from various platforms.
3536
type Client struct {
36-
platform Platform
37+
platform types.Platform
3738
logger *slog.Logger
38-
prCache *fido.TieredCache[string, PullRequestData]
39+
prCache *fido.TieredCache[string, types.PullRequestData]
3940
}
4041

4142
// Option is a function that configures a Client.
@@ -67,13 +68,13 @@ func WithCacheStore(store PRStore) Option {
6768
// For GitHub: NewClientWithPlatform(github.NewPlatform(token), opts...)
6869
// For GitLab: NewClientWithPlatform(gitlab.NewPlatform(token), opts...)
6970
// For Gitea: NewClientWithPlatform(gitea.NewPlatform(token), opts...)
70-
func NewClient(platform Platform, opts ...Option) *Client {
71+
func NewClient(platform types.Platform, opts ...Option) *Client {
7172
return NewClientWithPlatform(platform, opts...)
7273
}
7374

7475
// NewClientWithPlatform creates a new Client with the given platform.
7576
// Use this to create clients for GitLab, Codeberg, or other platforms.
76-
func NewClientWithPlatform(platform Platform, opts ...Option) *Client {
77+
func NewClientWithPlatform(platform types.Platform, opts ...Option) *Client {
7778
c := &Client{
7879
platform: platform,
7980
logger: slog.Default(),
@@ -90,7 +91,7 @@ func NewClientWithPlatform(platform Platform, opts ...Option) *Client {
9091
return c
9192
}
9293

93-
func createDefaultCache(log *slog.Logger) *fido.TieredCache[string, PullRequestData] {
94+
func createDefaultCache(log *slog.Logger) *fido.TieredCache[string, types.PullRequestData] {
9495
dir, err := os.UserCacheDir()
9596
if err != nil {
9697
dir = os.TempDir()
@@ -100,7 +101,7 @@ func createDefaultCache(log *slog.Logger) *fido.TieredCache[string, PullRequestD
100101
log.Warn("failed to create cache directory, caching disabled", "error", err)
101102
return nil
102103
}
103-
store, err := localfs.New[string, PullRequestData]("prx-pr", dir)
104+
store, err := localfs.New[string, types.PullRequestData]("prx-pr", dir)
104105
if err != nil {
105106
log.Warn("failed to create cache store, caching disabled", "error", err)
106107
return nil
@@ -114,7 +115,7 @@ func createDefaultCache(log *slog.Logger) *fido.TieredCache[string, PullRequestD
114115
}
115116

116117
// PullRequest fetches a pull request with all its events and metadata.
117-
func (c *Client) PullRequest(ctx context.Context, owner, repo string, prNumber int) (*PullRequestData, error) {
118+
func (c *Client) PullRequest(ctx context.Context, owner, repo string, prNumber int) (*types.PullRequestData, error) {
118119
return c.PullRequestWithReferenceTime(ctx, owner, repo, prNumber, time.Now())
119120
}
120121

@@ -124,7 +125,7 @@ func (c *Client) PullRequestWithReferenceTime(
124125
owner, repo string,
125126
pr int,
126127
refTime time.Time,
127-
) (*PullRequestData, error) {
128+
) (*types.PullRequestData, error) {
128129
if c.prCache == nil {
129130
return c.platform.FetchPR(ctx, owner, repo, pr, refTime)
130131
}
@@ -150,10 +151,10 @@ func (c *Client) PullRequestWithReferenceTime(
150151
"platform", c.platform.Name(), "owner", owner, "repo", repo, "pr", pr)
151152
}
152153

153-
result, err := c.prCache.Fetch(ctx, key, func(ctx context.Context) (PullRequestData, error) {
154+
result, err := c.prCache.Fetch(ctx, key, func(ctx context.Context) (types.PullRequestData, error) {
154155
data, err := c.platform.FetchPR(ctx, owner, repo, pr, refTime)
155156
if err != nil {
156-
return PullRequestData{}, err
157+
return types.PullRequestData{}, err
157158
}
158159
data.CachedAt = time.Now()
159160
return *data, nil
@@ -182,7 +183,7 @@ func NewCacheStore(dir string) (PRStore, error) {
182183
if err := os.MkdirAll(dir, 0o700); err != nil {
183184
return nil, fmt.Errorf("creating cache directory: %w", err)
184185
}
185-
store, err := localfs.New[string, PullRequestData]("prx-pr", dir)
186+
store, err := localfs.New[string, types.PullRequestData]("prx-pr", dir)
186187
if err != nil {
187188
return nil, fmt.Errorf("creating PR cache store: %w", err)
188189
}
@@ -205,3 +206,15 @@ func collaboratorsCacheKey(owner, repo string) string {
205206
func rulesetsCacheKey(owner, repo string) string {
206207
return fmt.Sprintf("%s/%s", owner, repo)
207208
}
209+
210+
// isHexString returns true if the string contains only hex characters.
211+
func isHexString(s string) bool {
212+
for i := range s {
213+
c := s[i]
214+
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') {
215+
continue
216+
}
217+
return false
218+
}
219+
return true
220+
}

0 commit comments

Comments
 (0)