Skip to content

Commit

Permalink
Allow updating to draft and pre-release
Browse files Browse the repository at this point in the history
New fields are added to update to draft and pre-release versions.
Fixes rhysd#14
  • Loading branch information
brutella committed Feb 19, 2021
1 parent 04a545f commit d0ded4f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
20 changes: 14 additions & 6 deletions selfupdate/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{'_', '-'} {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion selfupdate/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions selfupdate/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions selfupdate/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit d0ded4f

Please sign in to comment.