|
| 1 | +package packages |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +// repoPattern extracts owner/repo from a GitHub URL. |
| 16 | +var repoPattern = regexp.MustCompile(`github\.com[/:]([^/]+)/([^/.]+)`) |
| 17 | + |
| 18 | +// GitHubRelease represents a release from the GitHub Releases API. |
| 19 | +type GitHubRelease struct { |
| 20 | + TagName string `json:"tag_name"` |
| 21 | + Name string `json:"name"` |
| 22 | + Body string `json:"body"` |
| 23 | + PublishedAt time.Time `json:"published_at"` |
| 24 | + Assets []GitHubAsset `json:"assets"` |
| 25 | +} |
| 26 | + |
| 27 | +// GitHubAsset represents a downloadable file in a GitHub release. |
| 28 | +type GitHubAsset struct { |
| 29 | + Name string `json:"name"` |
| 30 | + BrowserDownloadURL string `json:"browser_download_url"` |
| 31 | + Size int64 `json:"size"` |
| 32 | + ContentType string `json:"content_type"` |
| 33 | +} |
| 34 | + |
| 35 | +// GitHubClient fetches release information from GitHub repositories. |
| 36 | +type GitHubClient struct { |
| 37 | + httpClient *http.Client |
| 38 | + token string // Optional GitHub token for higher rate limits. |
| 39 | +} |
| 40 | + |
| 41 | +// NewGitHubClient creates a GitHub API client. It reads GITHUB_TOKEN |
| 42 | +// from the environment for authenticated requests (5000 req/hour vs 60). |
| 43 | +func NewGitHubClient() *GitHubClient { |
| 44 | + return &GitHubClient{ |
| 45 | + httpClient: &http.Client{Timeout: 30 * time.Second}, |
| 46 | + token: os.Getenv("GITHUB_TOKEN"), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// parseRepo extracts owner and repo from a GitHub URL. |
| 51 | +// Supports https://github.com/owner/repo and git@github.com:owner/repo. |
| 52 | +func parseRepo(repoURL string) (owner, repo string, err error) { |
| 53 | + matches := repoPattern.FindStringSubmatch(repoURL) |
| 54 | + if len(matches) < 3 { |
| 55 | + return "", "", fmt.Errorf("cannot parse GitHub repo from URL: %s", repoURL) |
| 56 | + } |
| 57 | + return matches[1], strings.TrimSuffix(matches[2], ".git"), nil |
| 58 | +} |
| 59 | + |
| 60 | +// ListReleases fetches all releases for a GitHub repository. |
| 61 | +// Returns them sorted by published_at descending (newest first). |
| 62 | +func (c *GitHubClient) ListReleases(ctx context.Context, repoURL string) ([]GitHubRelease, error) { |
| 63 | + owner, repo, err := parseRepo(repoURL) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + apiURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases?per_page=50", owner, repo) |
| 69 | + |
| 70 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil) |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("creating request: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + req.Header.Set("Accept", "application/vnd.github+json") |
| 76 | + if c.token != "" { |
| 77 | + req.Header.Set("Authorization", "Bearer "+c.token) |
| 78 | + } |
| 79 | + |
| 80 | + resp, err := c.httpClient.Do(req) |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("fetching releases: %w", err) |
| 83 | + } |
| 84 | + defer resp.Body.Close() |
| 85 | + |
| 86 | + if resp.StatusCode != http.StatusOK { |
| 87 | + body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) |
| 88 | + return nil, fmt.Errorf("GitHub API returned %d: %s", resp.StatusCode, string(body)) |
| 89 | + } |
| 90 | + |
| 91 | + var releases []GitHubRelease |
| 92 | + if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil { |
| 93 | + return nil, fmt.Errorf("decoding releases: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + return releases, nil |
| 97 | +} |
| 98 | + |
| 99 | +// DownloadAsset downloads a release asset (ZIP file) to disk. |
| 100 | +// Returns the number of bytes written. |
| 101 | +func (c *GitHubClient) DownloadAsset(ctx context.Context, downloadURL, destPath string) (int64, error) { |
| 102 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil) |
| 103 | + if err != nil { |
| 104 | + return 0, fmt.Errorf("creating download request: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + if c.token != "" { |
| 108 | + req.Header.Set("Authorization", "Bearer "+c.token) |
| 109 | + } |
| 110 | + |
| 111 | + resp, err := c.httpClient.Do(req) |
| 112 | + if err != nil { |
| 113 | + return 0, fmt.Errorf("downloading asset: %w", err) |
| 114 | + } |
| 115 | + defer resp.Body.Close() |
| 116 | + |
| 117 | + if resp.StatusCode != http.StatusOK { |
| 118 | + return 0, fmt.Errorf("download returned HTTP %d", resp.StatusCode) |
| 119 | + } |
| 120 | + |
| 121 | + out, err := os.Create(destPath) |
| 122 | + if err != nil { |
| 123 | + return 0, fmt.Errorf("creating file %s: %w", destPath, err) |
| 124 | + } |
| 125 | + defer out.Close() |
| 126 | + |
| 127 | + n, err := io.Copy(out, resp.Body) |
| 128 | + if err != nil { |
| 129 | + _ = os.Remove(destPath) |
| 130 | + return 0, fmt.Errorf("writing file: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + return n, nil |
| 134 | +} |
0 commit comments