Skip to content

Commit

Permalink
fix(latest_version): double check new releases
Browse files Browse the repository at this point in the history
github responses *may* omit a version tag if you're querying *very* frequently
  • Loading branch information
JosephKav committed Jan 17, 2024
1 parent f4dd89a commit 9e319dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
22 changes: 18 additions & 4 deletions service/latest_version/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"net/http"
"strings"
"time"

"github.com/Masterminds/semver/v3"
github_types "github.com/release-argus/Argus/service/latest_version/api_type"
Expand All @@ -31,7 +32,9 @@ import (
// Query queries the Service source, updating Service.LatestVersion
// and returning true if it has changed (is a new release),
// otherwise returns false.
func (l *Lookup) query(logFrom *util.LogFrom) (bool, error) {
//
// checkNumber - 0 for first check, 1 for second check (if the first check found a new version)
func (l *Lookup) query(logFrom *util.LogFrom, checkNumber int) (bool, error) {
rawBody, err := l.httpRequest(logFrom)
if err != nil {
return false, err
Expand All @@ -48,8 +51,16 @@ func (l *Lookup) query(logFrom *util.LogFrom) (bool, error) {
// If this version is different (new?).
latestVersion := l.Status.LatestVersion()
if version != latestVersion {
// Verify that the version has changed. (GitHub may have just omitted the tag for some reason)
if checkNumber == 0 {
msg := fmt.Sprintf("Possibly found a new version (From %q to %q). Checking again", latestVersion, version)
jLog.Verbose(msg, *logFrom, latestVersion != "")
time.Sleep(time.Second)
return l.query(logFrom, 1)
}

if wantSemanticVersioning {
// Check it's a valid semnatic version
// Check it's a valid semantic version
newVersion, err := semver.NewVersion(version)
if err != nil {
err = fmt.Errorf("failed converting %q to a semantic version. If all versions are in this style, consider adding url_commands to get the version into the style of 'MAJOR.MINOR.PATCH' (https://semver.org/), or disabling semantic versioning (globally with defaults.service.semantic_versioning or just for this service with the semantic_versioning var)",
Expand Down Expand Up @@ -103,6 +114,8 @@ func (l *Lookup) query(logFrom *util.LogFrom) (bool, error) {
return true, nil
}

msg := fmt.Sprintf("Staying on %q as that's the latest version in the second check", version)
jLog.Verbose(msg, *logFrom, checkNumber == 1)
// Announce `LastQueried`
l.Status.AnnounceQuery()
// No version change.
Expand All @@ -114,7 +127,7 @@ func (l *Lookup) query(logFrom *util.LogFrom) (bool, error) {
//
// metrics - if true, set Prometheus metrics based on the query
func (l *Lookup) Query(metrics bool, logFrom *util.LogFrom) (newVersion bool, err error) {
newVersion, err = l.query(logFrom)
newVersion, err = l.query(logFrom, 0)

if metrics {
l.queryMetrics(err)
Expand Down Expand Up @@ -228,7 +241,8 @@ func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBody []byte, err error)
}
// Has tags/releases
} else {
jLog.Verbose("Potentially found new releases (new ETag)", *logFrom, true)
msg := fmt.Sprintf("Potentially found new releases (new ETag %s)", newETag)
jLog.Verbose(msg, *logFrom, true)
}

// 304 - Resource has not changed
Expand Down
4 changes: 2 additions & 2 deletions service/latest_version/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ func TestLookup_QueryGitHubETag(t *testing.T) {
"three requests only uses 1 api limit": {
attempts: 3,
eTagChanged: 1,
eTagUnchangedUseCache: 2,
eTagUnchangedUseCache: 3, // 2 attempts + 1 recheck
errRegex: `^$`},
"if initial request fails filter, cached results will be used": {
attempts: 3,
eTagChanged: 1,
eTagUnchangedUseCache: 2,
eTagUnchangedUseCache: 3, // 2 attempts + 1 recheck
initialRequireRegexVersion: `^FOO$`,
errRegex: `regex not matched on version`},
"invalid url_commands will catch no versions": {
Expand Down
2 changes: 1 addition & 1 deletion service/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestSlice_Track(t *testing.T) {
slice.Track(&tc.ordering, &sync.RWMutex{})

// THEN the function exits straight away
time.Sleep(time.Second)
time.Sleep(2 * time.Second)
for i := range *slice {
if !util.Contains(tc.ordering, i) {
if (*slice)[i].Status.LatestVersion() != "" {
Expand Down

0 comments on commit 9e319dd

Please sign in to comment.