-
Notifications
You must be signed in to change notification settings - Fork 43
/
find.go
222 lines (186 loc) · 4.87 KB
/
find.go
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// A Finder returns a list of URLs making up a project's assets.
type Finder interface {
Find() ([]string, error)
}
// A GithubRelease matches the Assets portion of Github's release API json.
type GithubRelease struct {
Assets []struct {
DownloadURL string `json:"browser_download_url"`
} `json:"assets"`
Prerelease bool `json:"prerelease"`
Tag string `json:"tag_name"`
CreatedAt time.Time `json:"created_at"`
}
type GithubError struct {
Code int
Status string
Body []byte
Url string
}
type errResponse struct {
Message string `json:"message"`
Doc string `json:"documentation_url"`
}
func (ge *GithubError) Error() string {
var msg errResponse
json.Unmarshal(ge.Body, &msg)
if ge.Code == http.StatusForbidden {
return fmt.Sprintf("%s: %s: %s", ge.Status, msg.Message, msg.Doc)
}
return fmt.Sprintf("%s (URL: %s)", ge.Status, ge.Url)
}
// A GithubAssetFinder finds assets for the given Repo at the given tag. Tags
// must be given as 'tag/<tag>'. Use 'latest' to get the latest release.
type GithubAssetFinder struct {
Repo string
Tag string
Prerelease bool
MinTime time.Time // release must be after MinTime to be found
}
var ErrNoUpgrade = errors.New("requested release is not more recent than current version")
func (f *GithubAssetFinder) Find() ([]string, error) {
if f.Prerelease && f.Tag == "latest" {
tag, err := f.getLatestTag()
if err != nil {
return nil, err
}
f.Tag = fmt.Sprintf("tags/%s", tag)
}
// query github's API for this repo/tag pair.
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/%s", f.Repo, f.Tag)
resp, err := Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if strings.HasPrefix(f.Tag, "tags/") && resp.StatusCode == http.StatusNotFound {
return f.FindMatch()
}
return nil, &GithubError{
Status: resp.Status,
Code: resp.StatusCode,
Body: body,
Url: url,
}
}
// read and unmarshal the resulting json
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var release GithubRelease
err = json.Unmarshal(body, &release)
if err != nil {
return nil, err
}
if release.CreatedAt.Before(f.MinTime) {
return nil, ErrNoUpgrade
}
// accumulate all assets from the json into a slice
assets := make([]string, 0, len(release.Assets))
for _, a := range release.Assets {
assets = append(assets, a.DownloadURL)
}
return assets, nil
}
func (f *GithubAssetFinder) FindMatch() ([]string, error) {
tag := f.Tag[len("tags/"):]
for page := 1; ; page++ {
url := fmt.Sprintf("https://api.github.com/repos/%s/releases?page=%d", f.Repo, page)
resp, err := Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, &GithubError{
Status: resp.Status,
Code: resp.StatusCode,
Body: body,
Url: url,
}
}
// read and unmarshal the resulting json
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var releases []GithubRelease
err = json.Unmarshal(body, &releases)
if err != nil {
return nil, err
}
for _, r := range releases {
if !f.Prerelease && r.Prerelease {
continue
}
if strings.Contains(r.Tag, tag) && !r.CreatedAt.Before(f.MinTime) {
// we have a winner
assets := make([]string, 0, len(r.Assets))
for _, a := range r.Assets {
assets = append(assets, a.DownloadURL)
}
return assets, nil
}
}
if len(releases) < 30 {
break
}
}
return nil, fmt.Errorf("no matching tag for '%s'", tag)
}
// finds the latest pre-release and returns the tag
func (f *GithubAssetFinder) getLatestTag() (string, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/releases", f.Repo)
resp, err := Get(url)
if err != nil {
return "", fmt.Errorf("pre-release finder: %w", err)
}
var releases []GithubRelease
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("pre-release finder: %w", err)
}
err = json.Unmarshal(body, &releases)
if err != nil {
return "", fmt.Errorf("pre-release finder: %w", err)
}
if len(releases) <= 0 {
return "", fmt.Errorf("no releases found")
}
return releases[0].Tag, nil
}
// A DirectAssetFinder returns the embedded URL directly as the only asset.
type DirectAssetFinder struct {
URL string
}
func (f *DirectAssetFinder) Find() ([]string, error) {
return []string{f.URL}, nil
}
type GithubSourceFinder struct {
Tool string
Repo string
Tag string
}
func (f *GithubSourceFinder) Find() ([]string, error) {
return []string{fmt.Sprintf("https://github.com/%s/tarball/%s/%s.tar.gz", f.Repo, f.Tag, f.Tool)}, nil
}