-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
104 lines (88 loc) · 2.74 KB
/
example_test.go
File metadata and controls
104 lines (88 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package prx_test
import (
"context"
"fmt"
"log"
"github.com/codeGROOVE-dev/prx/pkg/prx/fetch"
)
func Example() {
// The simplest way: just pass a URL
// Authentication is automatically resolved from environment or CLI tools
ctx := context.Background()
data, err := fetch.Fetch(ctx, "https://github.com/owner/repo/pull/123")
if err != nil {
log.Fatal(err)
}
// Show PR metadata
fmt.Printf("PR #%d: %s\n", data.PullRequest.Number, data.PullRequest.Title)
fmt.Printf("Author: %s (write access: %d)\n", data.PullRequest.Author, data.PullRequest.AuthorWriteAccess)
fmt.Printf("Status: %s, Mergeable: %v\n", data.PullRequest.State, data.PullRequest.MergeableState)
// Process events
for i := range data.Events {
event := &data.Events[i]
fmt.Printf("%s: %s by %s\n",
event.Timestamp.Format("2006-01-02 15:04:05"),
event.Kind,
event.Actor,
)
}
}
func ExampleFetch() {
ctx := context.Background()
// Works with GitHub
data, err := fetch.Fetch(ctx, "https://github.com/owner/repo/pull/123")
if err != nil {
log.Fatal(err)
}
fmt.Printf("GitHub PR #%d\n", data.PullRequest.Number)
// Works with GitLab
data, err = fetch.Fetch(ctx, "https://gitlab.com/owner/repo/-/merge_requests/456")
if err != nil {
log.Fatal(err)
}
fmt.Printf("GitLab MR #%d\n", data.PullRequest.Number)
// Works with Codeberg
data, err = fetch.Fetch(ctx, "https://codeberg.org/owner/repo/pulls/789")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Codeberg PR #%d\n", data.PullRequest.Number)
// Works with any Gitea instance
data, err = fetch.Fetch(ctx, "https://gitea.example.com/owner/repo/pulls/100")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Gitea PR #%d\n", data.PullRequest.Number)
// URL fragments and query parameters are automatically stripped
data, err = fetch.Fetch(ctx, "https://github.com/owner/repo/pull/123?tab=checks#issuecomment-456")
if err != nil {
log.Fatal(err)
}
fmt.Printf("PR #%d (cleaned URL)\n", data.PullRequest.Number)
}
func ExampleClient_PullRequest() {
// For advanced use cases where you need to fetch multiple PRs from the same platform,
// you can still use the client-based API
ctx := context.Background()
// Fetch using the simple API
data, err := fetch.Fetch(ctx, "https://github.com/golang/go/pull/123")
if err != nil {
log.Fatal(err)
}
// Show PR size
fmt.Printf("PR #%d: +%d -%d in %d files\n",
data.PullRequest.Number,
data.PullRequest.Additions,
data.PullRequest.Deletions,
data.PullRequest.ChangedFiles)
// Count events by type
eventCounts := make(map[string]int)
for i := range data.Events {
eventCounts[data.Events[i].Kind]++
}
// Print summary
fmt.Printf("Total events: %d\n", len(data.Events))
for eventKind, count := range eventCounts {
fmt.Printf("%s: %d\n", eventKind, count)
}
}