Skip to content

Commit

Permalink
Merge pull request #77 from pete0emerson/pete/75/tags_without_semver
Browse files Browse the repository at this point in the history
Skip tags that are not Semantically Versioned
  • Loading branch information
brikis98 authored Oct 28, 2020
2 parents 9554201 + 505acf4 commit 8a6d52f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/dustin/go-humanize"
"github.com/hashicorp/go-version"
)

type GitHubRepo struct {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -126,7 +127,10 @@ func FetchTags(githubRepoUrl string, githubToken string, instance GitHubInstance
}

for _, tag := range tags {
tagsString = append(tagsString, tag.Name)
// 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)
}
}

return tagsString, nil
Expand Down
9 changes: 7 additions & 2 deletions github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])
}
Expand Down

0 comments on commit 8a6d52f

Please sign in to comment.