From 0e9a520d5894a1e3663dea7570186459b8807bc2 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Mon, 19 Jul 2021 14:56:39 +0000 Subject: [PATCH] fix: detect updates for containerd Refs #48. Signed-off-by: Alexey Palazhchenko --- internal/pkg/update/github.go | 32 +++++++++++++++++------------- internal/pkg/update/github_test.go | 14 ++++++++++++- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/internal/pkg/update/github.go b/internal/pkg/update/github.go index 22d3f1e..803a4e2 100644 --- a/internal/pkg/update/github.go +++ b/internal/pkg/update/github.go @@ -89,6 +89,8 @@ func (g *gitHub) Latest(ctx context.Context, source string) (*LatestInfo, error) } // findLatestRelease returns information about latest released version. +// +//nolint:gocyclo func (g *gitHub) findLatestRelease(releases []*github.RepositoryRelease, sourceURL *url.URL, considerPrereleases bool) (*LatestInfo, error) { parts := strings.Split(sourceURL.Path, "/") owner, repo := parts[1], parts[2] @@ -116,26 +118,28 @@ func (g *gitHub) findLatestRelease(releases []*github.RepositoryRelease, sourceU source := sourceURL.String() - // treat releases without extra assets as tags - if len(newest.Assets) == 0 { - // update is available if the release doesn't have the same tarball URL - res.LatestURL = g.getTagGZ(owner, repo, newest.GetTagName()) - res.HasUpdate = res.LatestURL != source - - return res, nil - } - // update is available if the newest release doesn't have source in their assets download URLs for _, asset := range newest.Assets { if asset.GetBrowserDownloadURL() == source { - res.HasUpdate = false res.LatestURL = source return res, nil } } - // we don't know correct asset URL + // check default .tag.gz URL + latestTarGz := g.getTagTarGZ(owner, repo, newest.GetTagName()) + if latestTarGz == source { + res.LatestURL = source + + return res, nil + } + + // we don't know correct asset if there are any + if len(newest.Assets) == 0 { + res.LatestURL = latestTarGz + } + res.HasUpdate = true return res, nil @@ -179,7 +183,7 @@ func (g *gitHub) findLatestTag(ctx context.Context, tags []*github.RepositoryTag res := &LatestInfo{ BaseURL: fmt.Sprintf("https://github.com/%s/%s/releases/", owner, repo), - LatestURL: g.getTagGZ(owner, repo, newest.GetName()), + LatestURL: g.getTagTarGZ(owner, repo, newest.GetName()), } // update is available if the newest tag doesn't have the same tarball URL @@ -251,9 +255,9 @@ func (g *gitHub) getCommitTime(ctx context.Context, owner, repo, sha string) (ti return t, nil } -// getTagTarball returns .tar.gz URL. +// getTagTarGZ returns .tar.gz URL. // API's GetTarballURL is not good enough. -func (g *gitHub) getTagGZ(owner, repo, name string) string { +func (g *gitHub) getTagTarGZ(owner, repo, name string) string { return fmt.Sprintf("https://github.com/%s/%s/archive/refs/tags/%s.tar.gz", owner, repo, name) } diff --git a/internal/pkg/update/github_test.go b/internal/pkg/update/github_test.go index 067a01a..d7f7d3a 100644 --- a/internal/pkg/update/github_test.go +++ b/internal/pkg/update/github_test.go @@ -59,7 +59,7 @@ func TestLatestGithub(t *testing.T) { LatestURL: "https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-cpp-3.17.3.tar.gz", }, - // https://github.com/opencontainers/runc/releases has releases with extra assets (and no version in the file name). + // https://github.com/opencontainers/runc/releases has releases with extra assets and no version in the file name. "https://github.com/opencontainers/runc/releases/download/v1.0.0/runc.tar.xz": { HasUpdate: true, BaseURL: "https://github.com/opencontainers/runc/releases/", @@ -70,6 +70,18 @@ func TestLatestGithub(t *testing.T) { BaseURL: "https://github.com/opencontainers/runc/releases/", LatestURL: "https://github.com/opencontainers/runc/releases/download/v1.0.1/runc.tar.xz", }, + + // https://github.com/containerd/containerd/releases has releases with extra assets that we don't use. + "https://github.com/containerd/containerd/archive/refs/tags/v1.5.2.tar.gz": { + HasUpdate: true, + BaseURL: "https://github.com/containerd/containerd/releases/", + LatestURL: "", + }, + "https://github.com/containerd/containerd/archive/refs/tags/v1.5.3.tar.gz": { + HasUpdate: false, + BaseURL: "https://github.com/containerd/containerd/releases/", + LatestURL: "https://github.com/containerd/containerd/archive/refs/tags/v1.5.3.tar.gz", + }, } { source, expected := source, expected