-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
209 lines (178 loc) · 5.18 KB
/
github.go
File metadata and controls
209 lines (178 loc) · 5.18 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
var (
githubAPIBase = "https://api.github.com"
rawBase = "https://raw.githubusercontent.com"
)
const (
defaultBranch = "master"
httpTimeoutSec = 10
)
type TreeResponse struct {
Sha string `json:"sha"`
Tree []TreeObject `json:"tree"`
}
type TreeObject struct {
Path string `json:"path"`
Mode string `json:"mode"`
Type string `json:"type"`
Sha string `json:"sha"`
URL string `json:"url"`
Size int `json:"size"`
}
type Branch struct {
Name string `json:"name"`
Default bool `json:"-"`
}
type repoInfo struct {
DefaultBranch string `json:"default_branch"`
}
// parseRepoURL extrahiert owner und repo aus z.B.
// https://github.com/owner/repo
// https://github.com/owner/repo/
// https://github.com/owner/repo/tree/main
func parseRepoURL(repoURL string) (owner, repo string, err error) {
u, err := url.Parse(strings.TrimSpace(repoURL))
if err != nil {
return "", "", err
}
if u.Host != "github.com" {
return "", "", errors.New("URL must be a github.com URL")
}
parts := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(parts) < 2 {
return "", "", errors.New("URL must have the form https://github.com/owner/repo")
}
return parts[0], parts[1], nil
}
// buildRawURL erzeugt einen Raw-Link für eine Datei.
func buildRawURL(owner, repo, branch, filePath string) string {
return fmt.Sprintf("%s/%s/%s/%s/%s", rawBase, owner, repo, branch, filePath)
}
// fetchTree nutzt die GitHub Git Trees API mit ?recursive=1.
func fetchTree(owner, repo, branch string) ([]TreeObject, error) {
client := &http.Client{Timeout: httpTimeoutSec * time.Second}
apiURL := fmt.Sprintf("%s/repos/%s/%s/git/trees/%s?recursive=1", githubAPIBase, owner, repo, branch)
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
if err != nil {
return nil, err
}
// Optional: User-Agent setzen, manche Setups verlangen das
req.Header.Set("User-Agent", "github-repo-raw-urls-ui")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("Repository or branch not found (HTTP 404)")
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("GitHub API error: %s", resp.Status)
}
var tr TreeResponse
if err := json.NewDecoder(resp.Body).Decode(&tr); err != nil {
return nil, err
}
return tr.Tree, nil
}
func ListRawURLsWithOwnerRepo(owner, repo, branch string) ([]string, error) {
if branch == "" {
branch = defaultBranch
}
tree, err := fetchTree(owner, repo, branch)
if err != nil {
return nil, err
}
var urls []string
for _, obj := range tree {
if obj.Type == "blob" && !strings.HasSuffix(obj.Path, "/") {
urls = append(urls, buildRawURL(owner, repo, branch, obj.Path))
}
}
return urls, nil
}
func ListRawURLs(repoURL, branch string) ([]string, error) {
owner, repo, err := parseRepoURL(repoURL)
if err != nil {
return nil, err
}
return ListRawURLsWithOwnerRepo(owner, repo, branch)
}
func getRepoInfo(owner, repo string) (*repoInfo, error) {
client := &http.Client{Timeout: httpTimeoutSec * time.Second}
apiURL := fmt.Sprintf("%s/repos/%s/%s", githubAPIBase, owner, repo)
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "github-repo-raw-urls-ui")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("Repository not found (HTTP 404)")
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("GitHub API error (repo info): %s – %s", resp.Status, string(body))
}
var info repoInfo
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return nil, err
}
return &info, nil
}
// getBranches holt alle Branches und markiert den Default-Branch.
func getBranches(owner, repo string) ([]Branch, string, error) {
client := &http.Client{Timeout: httpTimeoutSec * time.Second}
// Erst Default-Branch holen
info, err := getRepoInfo(owner, repo)
if err != nil {
return nil, "", err
}
defaultBranch := info.DefaultBranch
apiURL := fmt.Sprintf("%s/repos/%s/%s/branches?per_page=100", githubAPIBase, owner, repo)
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
if err != nil {
return nil, "", err
}
req.Header.Set("User-Agent", "github-repo-raw-urls-ui")
resp, err := client.Do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, "", fmt.Errorf("Branches not found (HTTP 404)")
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return nil, "", fmt.Errorf("GitHub API error (branches): %s – %s", resp.Status, string(body))
}
var tmp []struct {
Name string `json:"name"`
}
if err := json.NewDecoder(resp.Body).Decode(&tmp); err != nil {
return nil, "", err
}
branches := make([]Branch, 0, len(tmp))
for _, b := range tmp {
branches = append(branches, Branch{
Name: b.Name,
Default: b.Name == defaultBranch,
})
}
return branches, defaultBranch, nil
}