From d0ded4f829e75c491a4879b43beb0933044d861f Mon Sep 17 00:00:00 2001 From: Matthias Hochgatterer Date: Fri, 19 Feb 2021 13:00:51 +0100 Subject: [PATCH] Allow updating to draft and pre-release New fields are added to update to draft and pre-release versions. Fixes rhysd/go-github-selfupdate#14 --- selfupdate/detect.go | 20 ++++++++++++++------ selfupdate/detect_test.go | 2 +- selfupdate/release.go | 4 ++++ selfupdate/updater.go | 10 ++++++++-- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/selfupdate/detect.go b/selfupdate/detect.go index 6ca1af7..77272de 100644 --- a/selfupdate/detect.go +++ b/selfupdate/detect.go @@ -12,19 +12,24 @@ import ( var reVersion = regexp.MustCompile(`\d+\.\d+\.\d+`) +type options struct { + draft bool + pre bool +} + func findAssetFromRelease(rel *github.RepositoryRelease, - suffixes []string, targetVersion string, filters []*regexp.Regexp) (*github.ReleaseAsset, semver.Version, bool) { + suffixes []string, targetVersion string, filters []*regexp.Regexp, opt options) (*github.ReleaseAsset, semver.Version, bool) { if targetVersion != "" && targetVersion != rel.GetTagName() { log.Println("Skip", rel.GetTagName(), "not matching to specified version", targetVersion) return nil, semver.Version{}, false } - if targetVersion == "" && rel.GetDraft() { + if targetVersion == "" && rel.GetDraft() && !opt.draft { log.Println("Skip draft version", rel.GetTagName()) return nil, semver.Version{}, false } - if targetVersion == "" && rel.GetPrerelease() { + if targetVersion == "" && rel.GetPrerelease() && !opt.pre { log.Println("Skip pre-release version", rel.GetTagName()) return nil, semver.Version{}, false } @@ -89,7 +94,7 @@ func findValidationAsset(rel *github.RepositoryRelease, validationName string) ( func findReleaseAndAsset(rels []*github.RepositoryRelease, targetVersion string, - filters []*regexp.Regexp) (*github.RepositoryRelease, *github.ReleaseAsset, semver.Version, bool) { + filters []*regexp.Regexp, opt options) (*github.RepositoryRelease, *github.ReleaseAsset, semver.Version, bool) { // Generate candidates suffixes := make([]string, 0, 2*7*2) for _, sep := range []rune{'_', '-'} { @@ -111,7 +116,7 @@ func findReleaseAndAsset(rels []*github.RepositoryRelease, // Returned list from GitHub API is in the order of the date when created. // ref: https://github.com/rhysd/go-github-selfupdate/issues/11 for _, rel := range rels { - if a, v, ok := findAssetFromRelease(rel, suffixes, targetVersion, filters); ok { + if a, v, ok := findAssetFromRelease(rel, suffixes, targetVersion, filters, opt); ok { // Note: any version with suffix is less than any version without suffix. // e.g. 0.0.1 > 0.0.1-beta if release == nil || v.GTE(ver) { @@ -159,7 +164,8 @@ func (up *Updater) DetectVersion(slug string, version string) (release *Release, return nil, false, err } - rel, asset, ver, found := findReleaseAndAsset(rels, version, up.filters) + opt := options{pre: up.pre, draft: up.draft} + rel, asset, ver, found := findReleaseAndAsset(rels, version, up.filters, opt) if !found { return nil, false, nil } @@ -170,6 +176,8 @@ func (up *Updater) DetectVersion(slug string, version string) (release *Release, publishedAt := rel.GetPublishedAt().Time release = &Release{ ver, + rel.GetPrerelease(), + rel.GetDraft(), url, asset.GetSize(), asset.GetID(), diff --git a/selfupdate/detect_test.go b/selfupdate/detect_test.go index d64fe65..87ca110 100644 --- a/selfupdate/detect_test.go +++ b/selfupdate/detect_test.go @@ -434,7 +434,7 @@ func TestFindReleaseAndAsset(t *testing.T) { expectedFound: false, }, } { - asset, ver, found := findAssetFromRelease(fixture.rels, []string{".gz"}, fixture.targetVersion, fixture.filters) + asset, ver, found := findAssetFromRelease(fixture.rels, []string{".gz"}, fixture.targetVersion, fixture.filters, options{}) if fixture.expectedFound { if !found { t.Errorf("expected to find an asset for this fixture: %q", fixture.name) diff --git a/selfupdate/release.go b/selfupdate/release.go index 014ac47..8ef7f6e 100644 --- a/selfupdate/release.go +++ b/selfupdate/release.go @@ -10,6 +10,10 @@ import ( type Release struct { // Version is the version of the release Version semver.Version + // PreRelease is the pre-release flag of the release + PreRelease bool + // Draft is the draft flag of the release + Draft bool // AssetURL is a URL to the uploaded file for the release AssetURL string // AssetSize represents the size of asset in bytes diff --git a/selfupdate/updater.go b/selfupdate/updater.go index 32cf5e0..bd67656 100644 --- a/selfupdate/updater.go +++ b/selfupdate/updater.go @@ -19,6 +19,8 @@ type Updater struct { apiCtx context.Context validator Validator filters []*regexp.Regexp + pre bool + draft bool } // Config represents the configuration of self-update. @@ -37,6 +39,10 @@ type Config struct { // An asset is selected if it matches any of those, in addition to the regular tag, os, arch, extensions. // Please make sure that your filter(s) uniquely match an asset. Filters []string + // PreRelease indicates if pre-releases are allowed. + PreRelease bool + // Draft indicates if drafts are allowed. + Draft bool } func newHTTPClient(ctx context.Context, token string) *http.Client { @@ -71,7 +77,7 @@ func NewUpdater(config Config) (*Updater, error) { if config.EnterpriseBaseURL == "" { client := github.NewClient(hc) - return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe}, nil + return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe, pre: config.PreRelease, draft: config.Draft}, nil } u := config.EnterpriseUploadURL @@ -82,7 +88,7 @@ func NewUpdater(config Config) (*Updater, error) { if err != nil { return nil, err } - return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe}, nil + return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe, pre: config.PreRelease, draft: config.Draft}, nil } // DefaultUpdater creates a new updater instance with default configuration.