From 4bec67f9ab28423e6b6f72b2fc9d397e5681c671 Mon Sep 17 00:00:00 2001 From: Pete Emerson Date: Thu, 22 Oct 2020 09:05:49 -0700 Subject: [PATCH 1/4] Exclude non-SemVer tags from list of fetched tags --- github.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/github.go b/github.go index e0682bf..2ee3e41 100644 --- a/github.go +++ b/github.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/dustin/go-humanize" + "github.com/hashicorp/go-version" ) type GitHubRepo struct { @@ -96,7 +97,7 @@ func ParseUrlIntoGithubInstance(repoUrl string, apiv string) (GitHubInstance, *F return instance, nil } -// Fetch all tags from the given GitHub repo +// Fetch all SemVer tags from the given GitHub repo func FetchTags(githubRepoUrl string, githubToken string, instance GitHubInstance) ([]string, *FetchError) { var tagsString []string @@ -106,6 +107,7 @@ func FetchTags(githubRepoUrl string, githubToken string, instance GitHubInstance } url := createGitHubRepoUrlForPath(repo, "tags") + resp, err := callGitHubApi(repo, url, map[string]string{}) if err != nil { return tagsString, err @@ -126,7 +128,9 @@ func FetchTags(githubRepoUrl string, githubToken string, instance GitHubInstance } for _, tag := range tags { - tagsString = append(tagsString, tag.Name) + if _, err := version.NewVersion(tag.Name); err == nil { + tagsString = append(tagsString, tag.Name) + } } return tagsString, nil From 801c1f57ba4bcba51d34f9aef4c9f689b1152269 Mon Sep 17 00:00:00 2001 From: Pete Emerson Date: Thu, 22 Oct 2020 09:13:29 -0700 Subject: [PATCH 2/4] Remove extra added blank line --- github.go | 1 - 1 file changed, 1 deletion(-) diff --git a/github.go b/github.go index 2ee3e41..536bf54 100644 --- a/github.go +++ b/github.go @@ -107,7 +107,6 @@ func FetchTags(githubRepoUrl string, githubToken string, instance GitHubInstance } url := createGitHubRepoUrlForPath(repo, "tags") - resp, err := callGitHubApi(repo, url, map[string]string{}) if err != nil { return tagsString, err From 441309b8e2038374f3a470afbb15583a69662431 Mon Sep 17 00:00:00 2001 From: Pete Emerson Date: Mon, 26 Oct 2020 13:35:24 -0700 Subject: [PATCH 3/4] Skip non-SemVer tags --- github.go | 1 + 1 file changed, 1 insertion(+) diff --git a/github.go b/github.go index 536bf54..0669639 100644 --- a/github.go +++ b/github.go @@ -127,6 +127,7 @@ func FetchTags(githubRepoUrl string, githubToken string, instance GitHubInstance } for _, tag := range tags { + // Skip tags that are not semantically versioned so that they don't cause errors. (issue #75) if _, err := version.NewVersion(tag.Name); err == nil { tagsString = append(tagsString, tag.Name) } From e7fa52ac1e442e6be23b1ccdece62ea003232768 Mon Sep 17 00:00:00 2001 From: Pete Emerson Date: Mon, 26 Oct 2020 13:52:37 -0700 Subject: [PATCH 4/4] Check that the number of releases received matches expectations --- github_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/github_test.go b/github_test.go index 9b5f36f..5c68e33 100644 --- a/github_test.go +++ b/github_test.go @@ -18,14 +18,15 @@ func TestGetListOfReleasesFromGitHubRepo(t *testing.T) { repoUrl string firstReleaseTag string lastReleaseTag string + expectedNumTags int gitHubOAuthToken string testInst GitHubInstance }{ // Test on a public repo whose sole purpose is to be a test fixture for this tool - {"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", "v0.0.3", "", testInst}, + {"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", "v0.0.3", 3, "", testInst}, // Private repo equivalent - {"https://github.com/gruntwork-io/fetch-test-private", "v0.0.2", "v0.0.2", os.Getenv("GITHUB_OAUTH_TOKEN"), testInst}, + {"https://github.com/gruntwork-io/fetch-test-private", "v0.0.2", "v0.0.2", 1, os.Getenv("GITHUB_OAUTH_TOKEN"), testInst}, } for _, tc := range cases { @@ -42,6 +43,10 @@ func TestGetListOfReleasesFromGitHubRepo(t *testing.T) { t.Fatalf("expected non-empty list of releases for repo %s, but no releases were found", tc.repoUrl) } + if len(releases) != tc.expectedNumTags { + t.Fatalf("expected %d releases, but got %d", tc.expectedNumTags, len(releases)) + } + if releases[len(releases)-1] != tc.firstReleaseTag { t.Fatalf("error parsing github releases for repo %s. expected first release = %s, actual = %s", tc.repoUrl, tc.firstReleaseTag, releases[len(releases)-1]) }