-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_client.go
More file actions
360 lines (304 loc) · 9.78 KB
/
github_client.go
File metadata and controls
360 lines (304 loc) · 9.78 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"context"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/google/go-github/v60/github"
)
// ChangelogInfo contains changelog data fetched from GitHub
type ChangelogInfo struct {
Found bool
Source string // "file" or "releases"
URL string // Link to the changelog
Content string // Full changelog content
BreakingChanges []string // Extracted breaking changes
}
// Regex patterns for extracting breaking changes (compiled once at package level)
var (
breakingPattern1 = regexp.MustCompile(`(?im)^[#*\s]*breaking[\s:]+(.+)$`)
breakingPattern2 = regexp.MustCompile(`(?im)^[#*\s]*⚠️(.+)$`)
breakingPattern3 = regexp.MustCompile(`(?im)^[#*\s]*removed[\s:]+(.+)$`)
breakingPattern4 = regexp.MustCompile(`(?im)\[breaking\]\s*(.+)$`)
)
// GitHubClient wraps the GitHub API client
type GitHubClient struct {
client *github.Client
ctx context.Context
}
// RepoStatus contains repository metadata
type RepoStatus struct {
Archived bool
Description string
}
// NewGitHubClient creates a new GitHub client
// If GITHUB_TOKEN is set, it will be used for authentication (higher rate limits)
func NewGitHubClient() *GitHubClient {
client := github.NewClient(nil)
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
client = client.WithAuthToken(token)
}
return &GitHubClient{
client: client,
ctx: context.Background(),
}
}
// CheckRepoStatus checks if a repository is archived or has other important metadata
func (gc *GitHubClient) CheckRepoStatus(basePath string) (*RepoStatus, error) {
// Resolve vanity imports to actual GitHub repositories
resolvedPath := resolveVanityImport(basePath)
// Extract owner and repo from GitHub path
owner, repo, ok := extractGitHubOwnerRepo(resolvedPath)
if !ok {
return nil, fmt.Errorf("not a GitHub repository")
}
// Fetch repository information
ctx, cancel := context.WithTimeout(gc.ctx, 10*time.Second)
defer cancel()
repository, _, err := gc.client.Repositories.Get(ctx, owner, repo)
if err != nil {
return nil, err
}
return &RepoStatus{
Archived: repository.GetArchived(),
Description: repository.GetDescription(),
}, nil
}
// FetchChangelog attempts to fetch changelog information for a package
func (gc *GitHubClient) FetchChangelog(basePath string) (*ChangelogInfo, error) {
// Resolve vanity imports to actual GitHub repositories
resolvedPath := resolveVanityImport(basePath)
// Extract owner and repo from GitHub path
owner, repo, ok := extractGitHubOwnerRepo(resolvedPath)
if !ok {
return &ChangelogInfo{Found: false}, nil
}
// Try fetching CHANGELOG.md from repository
info, err := gc.fetchChangelogFile(owner, repo)
if err == nil && info.Found {
return info, nil
}
// Fallback: try GitHub Releases
info, err = gc.fetchReleases(owner, repo)
if err == nil && info.Found {
return info, nil
}
return &ChangelogInfo{Found: false}, nil
}
// fetchChangelogFile tries to fetch CHANGELOG.md from the repository
func (gc *GitHubClient) fetchChangelogFile(owner, repo string) (*ChangelogInfo, error) {
// Try different common changelog filenames and branches
filenames := []string{"CHANGELOG.md", "CHANGELOG", "CHANGES.md", "HISTORY.md"}
branches := []string{"main", "master"}
for _, branch := range branches {
for _, filename := range filenames {
ctx, cancel := context.WithTimeout(gc.ctx, 10*time.Second)
fileContent, _, _, err := gc.client.Repositories.GetContents(ctx, owner, repo, filename, &github.RepositoryContentGetOptions{
Ref: branch,
})
cancel()
if err == nil && fileContent != nil {
content, err := fileContent.GetContent()
if err == nil && content != "" {
return &ChangelogInfo{
Found: true,
Source: "file",
URL: fmt.Sprintf("https://github.com/%s/%s/blob/%s/%s", owner, repo, branch, filename),
Content: content,
BreakingChanges: extractBreakingChanges(content),
}, nil
}
}
}
}
return &ChangelogInfo{Found: false}, nil
}
// fetchReleases fetches release notes from GitHub Releases
func (gc *GitHubClient) fetchReleases(owner, repo string) (*ChangelogInfo, error) {
ctx, cancel := context.WithTimeout(gc.ctx, 10*time.Second)
releases, _, err := gc.client.Repositories.ListReleases(ctx, owner, repo, &github.ListOptions{
PerPage: 10, // Get last 10 releases
})
cancel()
if err != nil {
return &ChangelogInfo{Found: false}, err
}
if len(releases) == 0 {
return &ChangelogInfo{Found: false}, nil
}
// Combine release notes
var sb strings.Builder
for _, release := range releases {
sb.WriteString(fmt.Sprintf("## %s\n\n", release.GetTagName()))
sb.WriteString(release.GetBody())
sb.WriteString("\n\n")
}
content := sb.String()
// Link to the latest release specifically instead of generic releases page
releaseURL := fmt.Sprintf("https://github.com/%s/%s/releases/tag/%s", owner, repo, releases[0].GetTagName())
return &ChangelogInfo{
Found: true,
Source: "releases",
URL: releaseURL,
Content: content,
BreakingChanges: extractBreakingChanges(content),
}, nil
}
// resolveVanityImport resolves common vanity import paths to their GitHub equivalents
func resolveVanityImport(path string) string {
// Common vanity import patterns
vanityMappings := map[string]string{
"k8s.io/": "github.com/kubernetes/",
"sigs.k8s.io/": "github.com/kubernetes-sigs/",
"go.uber.org/": "github.com/uber-go/",
"golang.org/x/": "github.com/golang/",
"google.golang.org/": "github.com/",
"gocloud.dev/": "github.com/google/go-cloud/",
}
for prefix, replacement := range vanityMappings {
suffix, ok := strings.CutPrefix(path, prefix)
if !ok {
continue
}
// Handle special case for google.golang.org where repo name might differ
if prefix == "google.golang.org/" {
// google.golang.org/grpc -> github.com/grpc/grpc-go
// google.golang.org/protobuf -> github.com/protocolbuffers/protobuf-go
parts := strings.Split(suffix, "/")
if len(parts) > 0 {
switch parts[0] {
case "grpc":
return "github.com/grpc/grpc-go"
case "protobuf":
return "github.com/protocolbuffers/protobuf-go"
}
}
}
return replacement + suffix
}
return path
}
// extractGitHubOwnerRepo extracts owner and repo from a GitHub module path
// Returns (owner, repo, ok)
func extractGitHubOwnerRepo(path string) (string, string, bool) {
if !strings.HasPrefix(path, "github.com/") {
return "", "", false
}
parts := strings.Split(path, "/")
if len(parts) < 3 {
return "", "", false
}
return parts[1], parts[2], true
}
// extractBreakingChanges finds breaking changes in changelog content
func extractBreakingChanges(content string) []string {
var changes []string
// Generic section headers to skip (these aren't actual breaking changes)
skipPatterns := []string{
"breaking changes",
"changes",
"breaking",
"removed",
"important",
"notes",
"⚠️",
}
// Use package-level compiled patterns
patterns := []*regexp.Regexp{
breakingPattern1,
breakingPattern2,
breakingPattern3,
breakingPattern4,
}
for line := range strings.SplitSeq(content, "\n") {
for _, pattern := range patterns {
if matches := pattern.FindStringSubmatch(line); len(matches) > 1 {
change := strings.TrimSpace(matches[1])
// Skip if empty or too short (likely a header)
if len(change) < 10 {
continue
}
// Skip if it ends with ":**" (markdown link labels)
if strings.HasSuffix(change, ":**") {
continue
}
// Skip if it starts with emoji (likely a heading)
if strings.HasPrefix(change, "⚠️") || strings.HasPrefix(change, "⚠") {
continue
}
// Skip if it's just "Changes" followed by punctuation
if rest, ok := strings.CutPrefix(change, "Changes:"); ok && len(rest) < 5 {
continue
}
// Skip generic section headers
changeLower := strings.ToLower(strings.TrimSpace(strings.Trim(change, "⚠️ ")))
isGenericHeader := false
for _, skip := range skipPatterns {
if changeLower == skip ||
strings.HasPrefix(changeLower, skip+":") ||
strings.HasSuffix(changeLower, skip) {
isGenericHeader = true
break
}
}
if isGenericHeader {
continue
}
// Skip if it looks like a markdown header (starts with #)
if strings.HasPrefix(change, "#") {
continue
}
// Skip if it's ONLY punctuation and emojis
trimmedChange := strings.Trim(change, " ⚠️⚠*#:-")
if len(trimmedChange) < 5 {
continue
}
changes = append(changes, change)
}
}
}
// Limit to top 5 most relevant breaking changes
if len(changes) > 5 {
changes = changes[:5]
}
return changes
}
// ExtractVersionSection extracts a specific version section from changelog
func ExtractVersionSection(content, version string) string {
// Try to find the version header
versionPattern := regexp.MustCompile(fmt.Sprintf(`(?mi)^#+\s*\[?%s\]?`, regexp.QuoteMeta(version)))
lines := strings.Split(content, "\n")
var sectionLines []string
inSection := false
headerLevel := 0
for _, line := range lines {
if versionPattern.MatchString(line) {
inSection = true
// Count header level (number of # characters)
headerLevel = strings.Count(strings.SplitN(line, " ", 2)[0], "#")
sectionLines = append(sectionLines, line)
continue
}
if inSection {
// Check if we've hit the next section header at the same or higher level
if strings.HasPrefix(strings.TrimSpace(line), "#") {
currentLevel := strings.Count(strings.SplitN(line, " ", 2)[0], "#")
if currentLevel <= headerLevel {
break
}
}
sectionLines = append(sectionLines, line)
}
}
if len(sectionLines) > 0 {
result := strings.Join(sectionLines, "\n")
// Truncate if too long
if len(result) > 1000 {
result = result[:1000] + "\n\n_(truncated)_"
}
return result
}
return ""
}