-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
github.go
112 lines (92 loc) · 2.55 KB
/
github.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
package updater
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/blang/semver/v4"
"golang.org/x/net/context/ctxhttp"
)
var (
// APITimeout is how long we wait for the GitHub API
APITimeout = 30 * time.Second
// BaseURL is exported for tests
BaseURL = "https://api.github.com/repos/%s/%s/releases/latest"
gitHubOrg = "gopasspw"
gitHubRepo = "gopass"
)
// Asset is a GitHub release asset
type Asset struct {
ID int `json:"id"`
Name string `json:"name"`
URL string `json:"browser_download_url"`
}
// Release is a GitHub release
type Release struct {
ID int `json:"id"`
Name string `json:"name"`
TagName string `json:"tag_name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
PublishedAt time.Time `json:"published_at"`
Assets []Asset `json:"assets"`
Version semver.Version `json:"-"`
}
func downloadAsset(ctx context.Context, assets []Asset, suffix string) (string, []byte, error) {
var url string
var filename string
for _, a := range assets {
if !strings.HasSuffix(a.Name, suffix) {
continue
}
url = a.URL
filename = a.Name
break
}
if url == "" {
return "", nil, fmt.Errorf("asset with suffix %q not found", suffix)
}
buf, err := tryDownload(ctx, url)
if err != nil {
return "", nil, err
}
return filename, buf, nil
}
// FetchLatestRelease fetches meta-data about the latest Gopass release
// from GitHub.
func FetchLatestRelease(ctx context.Context) (Release, error) {
owner := gitHubOrg
repo := gitHubRepo
ctx, cancel := context.WithTimeout(ctx, APITimeout)
defer cancel()
url := fmt.Sprintf(BaseURL, owner, repo)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return Release{}, nil
}
// pin to API version 3 to avoid breaking our structs
req.Header.Set("Accept", "application/vnd.github.v3+json")
resp, err := ctxhttp.Do(ctx, http.DefaultClient, req)
if err != nil {
return Release{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return Release{}, fmt.Errorf("request faild with %v (%v)", resp.StatusCode, resp.Status)
}
var rs Release
if err := json.NewDecoder(resp.Body).Decode(&rs); err != nil {
return rs, err
}
if !strings.HasPrefix(rs.TagName, "v") {
return rs, fmt.Errorf("tag name %q is invalid, must start with 'v'", rs.TagName)
}
v, err := semver.Parse(rs.TagName[1:])
if err != nil {
return rs, fmt.Errorf("failed to parse version %q: %q", rs.TagName[1:], err)
}
rs.Version = v
return rs, nil
}