Skip to content

Commit ccf1aed

Browse files
extracting GitHub related code to a separate class
surrounded by an interface. It will help with mocking GitHub, and also providing support for Gitlab
1 parent 89277ba commit ccf1aed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+649
-509
lines changed

.travis.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ install:
3232
- go get -t -d -v ./...
3333

3434
script:
35-
- go build ./selfupdate/
36-
- go build ./cmd/detect-latest-release/
37-
- go build ./cmd/go-get-release/
35+
- go build ./...
3836
- |
3937
if [[ "${GITHUB_TOKEN}" != "" ]]; then
40-
go test -v -race -coverprofile=coverage.txt ./selfupdate
38+
go test -v -race -coverprofile=coverage.txt . ./update
4139
else
42-
go test -v -race -short ./selfupdate
40+
go test -v -race -short . ./update
4341
fi
4442
4543
after_success:

Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ GOTOOL=$(GOCMD) tool
1111
GOGET=$(GOCMD) get
1212
GOPATH?=`$(GOCMD) env GOPATH`
1313

14-
TESTS=./...
14+
TESTS=. ./update
1515
COVERAGE_FILE=coverage.out
1616

1717
BUILD_DATE=`date`
@@ -22,9 +22,7 @@ BUILD_COMMIT=`git rev-parse HEAD`
2222
all: test build
2323

2424
build:
25-
$(GOBUILD) -v ./selfupdate
26-
$(GOBUILD) -v ./cmd/go-get-release
27-
$(GOBUILD) -v ./cmd/detect-latest-release
25+
$(GOBUILD) -v ./...
2826

2927
test:
3028
$(GOTEST) -race -v $(TESTS)
@@ -34,6 +32,7 @@ coverage:
3432
$(GOTOOL) cover -html=$(COVERAGE_FILE)
3533

3634
clean:
35+
rm detect-latest-release go-get-release coverage.out
3736
$(GOCLEAN)
3837

3938
toc:
File renamed without changes.
File renamed without changes.
File renamed without changes.

cmd/detect-latest-release/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"regexp"
99
"strings"
1010

11-
"github.com/creativeprojects/go-selfupdate/selfupdate"
11+
"github.com/creativeprojects/go-selfupdate"
1212
)
1313

1414
func usage() {

cmd/go-get-release/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"runtime"
1313
"strings"
1414

15-
"github.com/creativeprojects/go-selfupdate/selfupdate"
15+
"github.com/creativeprojects/go-selfupdate"
1616
)
1717

1818
func usage() {

selfupdate/config.go renamed to config.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
package selfupdate
22

3-
import "context"
4-
53
// Config represents the configuration of self-update.
64
type Config struct {
7-
// APIToken represents GitHub API token. If it's not empty, it will be used for authentication of GitHub API
8-
APIToken string
9-
// EnterpriseBaseURL is a base URL of GitHub API. If you want to use this library with GitHub Enterprise,
10-
// please set "https://{your-organization-address}/api/v3/" to this field.
11-
EnterpriseBaseURL string
12-
// EnterpriseUploadURL is a URL to upload stuffs to GitHub Enterprise instance. This is often the same as an API base URL.
13-
// So if this field is not set and EnterpriseBaseURL is set, EnterpriseBaseURL is also set to this field.
14-
EnterpriseUploadURL string
5+
// Source where to load the releases from (example: GitHubSource)
6+
Source Source
157
// Validator represents types which enable additional validation of downloaded release.
168
Validator Validator
179
// Filters are regexp used to filter on specific assets for releases with multiple assets.
1810
// An asset is selected if it matches any of those, in addition to the regular tag, os, arch, extensions.
1911
// Please make sure that your filter(s) uniquely match an asset.
2012
Filters []string
21-
// Context used by the http client (default to context.Background)
22-
Context context.Context
2313
// OS is set to the value of runtime.GOOS by default, but you can force another value here
2414
OS string
2515
// Arch is set to the value of runtime.GOARCH by default, but you can force another value here
File renamed without changes.
File renamed without changes.

selfupdate/detect.go renamed to detect.go

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/Masterminds/semver/v3"
9-
"github.com/google/go-github/v30/github"
109
)
1110

1211
var reVersion = regexp.MustCompile(`\d+\.\d+\.\d+`)
@@ -31,14 +30,8 @@ func (up *Updater) DetectVersion(slug string, version string) (release *Release,
3130
return nil, false, fmt.Errorf("invalid slug format. It should be 'owner/name': %s", slug)
3231
}
3332

34-
rels, res, err := up.api.Repositories.ListReleases(up.apiCtx, repo[0], repo[1], nil)
33+
rels, err := up.source.ListReleases(repo[0], repo[1])
3534
if err != nil {
36-
log.Printf("API returned an error response: %s", err)
37-
if res != nil && res.StatusCode == 404 {
38-
// 404 means repository not found or release not found. It's not an error here.
39-
err = nil
40-
log.Print("API returned 404. Repository or release not found")
41-
}
4235
return nil, false, err
4336
}
4437

@@ -50,19 +43,19 @@ func (up *Updater) DetectVersion(slug string, version string) (release *Release,
5043
url := asset.GetBrowserDownloadURL()
5144
log.Printf("Successfully fetched the latest release. tag: %s, name: %s, URL: %s, Asset: %s", rel.GetTagName(), rel.GetName(), rel.GetURL(), url)
5245

53-
publishedAt := rel.GetPublishedAt().Time
46+
publishedAt := rel.GetPublishedAt()
5447
release = &Release{
5548
version: ver,
49+
repoOwner: repo[0],
50+
repoName: repo[1],
5651
AssetURL: url,
5752
AssetByteSize: asset.GetSize(),
5853
AssetID: asset.GetID(),
5954
ValidationAssetID: -1,
60-
URL: rel.GetHTMLURL(),
61-
ReleaseNotes: rel.GetBody(),
55+
URL: rel.GetURL(),
56+
ReleaseNotes: rel.GetReleaseNotes(),
6257
Name: rel.GetName(),
6358
PublishedAt: &publishedAt,
64-
RepoOwner: repo[0],
65-
RepoName: repo[1],
6659
OS: up.os,
6760
Arch: up.arch,
6861
Arm: up.arm,
@@ -80,9 +73,11 @@ func (up *Updater) DetectVersion(slug string, version string) (release *Release,
8073
return release, true, nil
8174
}
8275

83-
func findAssetFromRelease(rel *github.RepositoryRelease,
84-
suffixes []string, targetVersion string, filters []*regexp.Regexp) (*github.ReleaseAsset, *semver.Version, bool) {
85-
76+
func findAssetFromRelease(rel SourceRelease, suffixes []string, targetVersion string, filters []*regexp.Regexp) (SourceAsset, *semver.Version, bool) {
77+
if rel == nil {
78+
log.Print("Empty release instance!")
79+
return nil, nil, false
80+
}
8681
if targetVersion != "" && targetVersion != rel.GetTagName() {
8782
log.Printf("Skip %s not matching to specified version %s", rel.GetTagName(), targetVersion)
8883
return nil, nil, false
@@ -104,7 +99,7 @@ func findAssetFromRelease(rel *github.RepositoryRelease,
10499
return nil, nil, false
105100
}
106101
if indices[0] > 0 {
107-
log.Printf("Strip prefix '%s' from '%s'", verText[:indices[0]], verText)
102+
// log.Printf("Strip prefix '%s' from '%s'", verText[:indices[0]], verText)
108103
verText = verText[indices[0]:]
109104
}
110105

@@ -116,7 +111,7 @@ func findAssetFromRelease(rel *github.RepositoryRelease,
116111
return nil, nil, false
117112
}
118113

119-
for _, asset := range rel.Assets {
114+
for _, asset := range rel.GetAssets() {
120115
name := asset.GetName()
121116
if len(filters) > 0 {
122117
// if some filters are defined, match them: if any one matches, the asset is selected
@@ -146,20 +141,16 @@ func findAssetFromRelease(rel *github.RepositoryRelease,
146141
return nil, nil, false
147142
}
148143

149-
func findValidationAsset(rel *github.RepositoryRelease, validationName string) (*github.ReleaseAsset, bool) {
150-
for _, asset := range rel.Assets {
144+
func findValidationAsset(rel SourceRelease, validationName string) (SourceAsset, bool) {
145+
for _, asset := range rel.GetAssets() {
151146
if asset.GetName() == validationName {
152147
return asset, true
153148
}
154149
}
155150
return nil, false
156151
}
157152

158-
func (up *Updater) findReleaseAndAsset(rels []*github.RepositoryRelease, targetVersion string,
159-
) (*github.RepositoryRelease,
160-
*github.ReleaseAsset,
161-
*semver.Version,
162-
bool) {
153+
func (up *Updater) findReleaseAndAsset(rels []SourceRelease, targetVersion string) (SourceRelease, SourceAsset, *semver.Version, bool) {
163154
// we put the detected arch at the end of the list: that's fine for ARM so far,
164155
// as the additional arch are more accurate than the generic one
165156
for _, arch := range append(generateAdditionalArch(up.arch, up.arm), up.arch) {
@@ -175,10 +166,10 @@ func (up *Updater) findReleaseAndAsset(rels []*github.RepositoryRelease, targetV
175166
func findReleaseAndAssetForArch(
176167
os string,
177168
arch string,
178-
rels []*github.RepositoryRelease,
169+
rels []SourceRelease,
179170
targetVersion string,
180171
filters []*regexp.Regexp,
181-
) (*github.RepositoryRelease, *github.ReleaseAsset, *semver.Version, bool) {
172+
) (SourceRelease, SourceAsset, *semver.Version, bool) {
182173
// Generate candidates
183174
suffixes := make([]string, 0, 2*7*2)
184175
for _, sep := range []rune{'_', '-'} {
@@ -193,8 +184,8 @@ func findReleaseAndAssetForArch(
193184
}
194185

195186
var ver *semver.Version
196-
var asset *github.ReleaseAsset
197-
var release *github.RepositoryRelease
187+
var asset SourceAsset
188+
var release SourceRelease
198189

199190
// Find the latest version from the list of releases.
200191
// Returned list from GitHub API is in the order of the date when created.

0 commit comments

Comments
 (0)