Repository navigation
Expand file tree
/
Copy pathgithub.go
More file actions
235 lines (220 loc) · 6.3 KB
/
Copy pathgithub.go
File metadata and controls
235 lines (220 loc) · 6.3 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
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type Asset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
Size int64 `json:"size"`
}
type Release struct {
Tag string `json:"tag_name"`
Name string `json:"name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
Published time.Time `json:"published_at"`
Assets []Asset `json:"assets"`
}
// Plan is what Mixtape intends to download for a port.
type Plan struct {
Version string
Date time.Time
Files []Asset
Snapshot bool // no releases: using a source archive of the default branch
Problem string // non-empty: can't install on-device, with the reason
}
func (p *Plan) Size() int64 {
var n int64
for _, a := range p.Files {
n += a.Size
}
return n
}
func (env *Env) api() string {
if env.APIBase != "" {
return env.APIBase
}
return "https://api.github.com"
}
type RateLimitError struct{ Reset time.Time }
func (e *RateLimitError) Error() string {
return "GitHub rate limit reached — try again after " + e.Reset.Local().Format("15:04")
}
func (env *Env) ghGet(url string, v any) error {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Accept", "application/vnd.github+json")
if env.Config.GitHubToken != "" {
req.Header.Set("Authorization", "Bearer "+env.Config.GitHubToken)
}
cl := &http.Client{Timeout: 20 * time.Second, Transport: env.HTTP.Transport}
resp, err := cl.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 403 || resp.StatusCode == 429 {
if resp.Header.Get("X-RateLimit-Remaining") == "0" {
sec, _ := strconv.ParseInt(resp.Header.Get("X-RateLimit-Reset"), 10, 64)
return &RateLimitError{Reset: time.Unix(sec, 0)}
}
}
if resp.StatusCode == 404 {
return fmt.Errorf("repository not found (moved or deleted?)")
}
if resp.StatusCode != 200 {
return fmt.Errorf("GitHub answered %s", resp.Status)
}
return json.NewDecoder(io.LimitReader(resp.Body, 8<<20)).Decode(v)
}
// Releases fetches (and caches for an hour) a repo's release list. If GitHub
// can't be reached, a cached list is used instead (good enough for installs).
func (env *Env) Releases(repo string, force bool) ([]Release, error) {
return env.releases(repo, force, false)
}
// releases with strict=true never answers from an old cache after a failed
// lookup; the self-update check uses it so it can't wrongly say "up to date".
func (env *Env) releases(repo string, force, strict bool) ([]Release, error) {
h := sha1.Sum([]byte(strings.ToLower(repo)))
cache := filepath.Join(env.DataDir, "cache", "rel-"+hex.EncodeToString(h[:8])+".json")
if !force {
if st, err := os.Stat(cache); err == nil && time.Since(st.ModTime()) < time.Hour {
var rs []Release
if b, err := os.ReadFile(cache); err == nil && json.Unmarshal(b, &rs) == nil {
return rs, nil
}
}
}
var rs []Release
err := env.ghGet(env.api()+"/repos/"+repo+"/releases?per_page=8", &rs)
if err != nil {
log.Printf("release lookup for %s failed: %v", repo, err)
// Fall back to a stale cache rather than nothing (unless strict).
if !strict {
if b, e2 := os.ReadFile(cache); e2 == nil && json.Unmarshal(b, &rs) == nil {
return rs, nil
}
}
return nil, err
}
if b, err := json.Marshal(rs); err == nil {
_ = writeFileAtomic(cache, b)
}
return rs, nil
}
var badAssetWords = []string{"portmaster", "muos", "muxapp", "allium", "spruce", "knulli", "rocknix", "trimui",
"anbernic", "rg35", "rg40", "x86_64", "x86-64", "amd64", "aarch64", "arm64", "windows", "win64", "macos",
"darwin", "source", "src", "debug", "symbols", "sha256", ".sig", ".asc", "arkos", "batocera", "garlic", "minui", "nextui"}
func assetScore(name string, want string) int {
n := strings.ToLower(name)
if !(strings.HasSuffix(n, ".zip") || strings.HasSuffix(n, ".tar.gz") || strings.HasSuffix(n, ".tgz")) {
return -1000
}
s := 0
if want != "" {
if strings.Contains(n, strings.ToLower(want)) {
s += 200
} else {
s -= 300
}
}
for _, w := range badAssetWords {
if strings.Contains(n, w) {
s -= 100
}
}
if strings.Contains(n, "onion") {
s += 50
}
for _, w := range []string{"miyoo", "mmp", "mini"} {
if strings.Contains(n, w) {
s += 20
break
}
}
if strings.Contains(n, "app") {
s += 5
}
return s
}
// PickAssets chooses which release files to install.
func PickAssets(rel Release, r *Recipe) ([]Asset, string) {
want := ""
all := false
if r != nil {
want, all = r.Asset, r.All
}
best, bestScore := -1, -60
var picks []Asset
has7z := false
for i, a := range rel.Assets {
if strings.HasSuffix(strings.ToLower(a.Name), ".7z") {
has7z = true
}
sc := assetScore(a.Name, want)
if all && sc > 0 {
picks = append(picks, a)
}
if sc > bestScore {
best, bestScore = i, sc
}
}
if all && len(picks) > 0 {
return picks, ""
}
if best >= 0 {
return []Asset{rel.Assets[best]}, ""
}
if has7z {
return nil, "This release is only published as a .7z archive, which Mixtape can't unpack yet. Extract it to the SD card root on a PC."
}
if len(rel.Assets) == 0 {
return nil, ""
}
return nil, "None of this release's files look like an OnionOS build."
}
// PlanFor works out what to install for a port.
func (env *Env) PlanFor(p *Port, force bool) (*Plan, error) {
if reason := p.ManualReason(); reason != "" {
return &Plan{Problem: reason}, nil
}
if p.Recipe != nil && p.Recipe.URL != "" {
base := p.Recipe.URL[strings.LastIndex(p.Recipe.URL, "/")+1:]
return &Plan{Version: "latest", Files: []Asset{{Name: base, URL: p.Recipe.URL}}}, nil
}
rels, err := env.Releases(p.Repo, force)
if err != nil {
return nil, err
}
for _, rel := range rels {
if rel.Draft {
continue
}
files, problem := PickAssets(rel, p.Recipe)
if len(files) == 0 && problem == "" {
continue // release with no binaries; try an older one
}
v := rel.Tag
if v == "" {
v = rel.Name
}
return &Plan{Version: v, Date: rel.Published, Files: files, Problem: problem}, nil
}
// No usable releases: many small Onion apps are just an App/ folder in the repo.
return &Plan{
Version: "main",
Snapshot: true,
Files: []Asset{{Name: "source.zip", URL: "https://github.com/" + p.Repo + "/archive/HEAD.zip"}},
}, nil
}