-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
version.go
182 lines (150 loc) · 4.64 KB
/
version.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
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package version
import (
"context"
"encoding/json"
"fmt"
"net/http"
"runtime"
"time"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
goversion "github.com/hashicorp/go-version"
)
// Release is a GitHub release
type Release struct {
ID int64 `json:"id,omitempty"`
NodeID string `json:"node_id,omitempty"`
URL string `json:"url,omitempty"`
HtmlURL string `json:"html_url,omitempty"`
TagName string `json:"tag_name,omitempty"`
TargetCommitish string `json:"target_commitish,omitempty"`
Name *string `json:"name,omitempty"`
Body *string `json:"body,omitempty"`
Draft bool `json:"draft,omitempty"`
Prerelease bool `json:"prerelease,omitempty"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Author Author `json:"author"`
Assets []Asset `json:"assets"`
}
type Author struct {
Login string `json:"login"`
Id int64 `json:"id"`
NodeId string `json:"node_id"`
AvatarUrl string `json:"avatar_url"`
GravatarId string `json:"gravatar_id"`
Url string `json:"url"`
HtmlUrl string `json:"html_url"`
Type string `json:"type"`
}
type Asset struct {
Url string `json:"url"`
Id int64 `json:"id"`
NodeId string `json:"node_id"`
Name string `json:"name"`
Label string `json:"label"`
Uploader Author `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int64 `json:"size"`
DownloadCount int64 `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadUrl string `json:"browser_download_url"`
}
func (r *Release) IsPreOrDraft() bool {
if r.Draft || r.Prerelease {
return true
}
return false
}
type Checker struct {
// user/repo-name or org/repo-name
Owner string
Repo string
CurrentVersion string
httpClient *http.Client
}
func NewChecker(owner, repo, currentVersion string) *Checker {
return &Checker{
Owner: owner,
Repo: repo,
CurrentVersion: currentVersion,
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
func (c *Checker) get(ctx context.Context) (*Release, error) {
url := fmt.Sprintf("https://api.autobrr.com/repos/%s/%s/releases/latest", c.Owner, c.Repo)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("User-Agent", c.buildUserAgent())
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error getting releases for %v: %s", c.Repo, resp.Status)
}
var release Release
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&release); err != nil {
return nil, err
}
return &release, nil
}
func (c *Checker) CheckNewVersion(ctx context.Context, version string) (bool, *Release, error) {
if isDevelop(version) {
return false, nil, nil
}
release, err := c.get(ctx)
if err != nil {
return false, nil, err
}
newAvailable, _, err := c.checkNewVersion(version, release)
if err != nil {
return false, nil, err
}
if !newAvailable {
return false, nil, nil
}
return true, release, nil
}
func (c *Checker) checkNewVersion(version string, release *Release) (bool, string, error) {
currentVersion, err := goversion.NewVersion(version)
if err != nil {
return false, "", errors.Wrap(err, "error parsing current version")
}
releaseVersion, err := goversion.NewVersion(release.TagName)
if err != nil {
return false, "", errors.Wrap(err, "error parsing release version")
}
if len(currentVersion.Prerelease()) == 0 && len(releaseVersion.Prerelease()) > 0 {
return false, "", nil
}
if releaseVersion.GreaterThan(currentVersion) {
// new update available
return true, releaseVersion.String(), nil
}
return false, "", nil
}
func (c *Checker) buildUserAgent() string {
return fmt.Sprintf("autobrr/%s (%s %s)", c.CurrentVersion, runtime.GOOS, runtime.GOARCH)
}
func isDevelop(version string) bool {
tags := []string{"dev", "develop", "master", "latest", ""}
for _, tag := range tags {
if version == tag {
return true
}
}
return false
}