Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform direct asset name comparison #102

Merged
merged 5 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGetListOfReleasesFromGitHubRepo(t *testing.T) {
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", 3, "", testInst},
{"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", "v0.0.4", 4, "", testInst},

// Private repo equivalent
{"https://github.com/gruntwork-io/fetch-test-private", "v0.0.2", "v0.0.2", 1, os.Getenv("GITHUB_OAUTH_TOKEN"), testInst},
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ func findAssetsInRelease(assetRegex string, release GitHubReleaseApiResponse) ([
if matched {
assetRef := asset
matches = append(matches, &assetRef)
} else if asset.Name == assetRegex {
// Sometimes the actual asset name contains regex symbols that could mess up matching.
// Perform a direct comparison as a last resort.
assetRef := asset
matches = append(matches, &assetRef)
}
}

Expand Down
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ func TestDownloadReleaseAssets(t *testing.T) {
}
}

func TestDownloadReleaseAssetsWithRegexCharacters(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}

const githubRepoUrl = "https://github.com/gruntwork-io/fetch-test-public"
const releaseAsset = "hello+world.txt"
const assetVersion = "v0.0.4"

githubRepo, err := ParseUrlIntoGitHubRepo(githubRepoUrl, "", testInst)
if err != nil {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}

assetPaths, fetchErr := downloadReleaseAssets(logger, releaseAsset, tmpDir, githubRepo, assetVersion, false)
if fetchErr != nil {
t.Fatalf("Failed to download release asset: %s", fetchErr)
}

if len(assetPaths) != 1 {
t.Fatalf("Expected to download 1 assets, not %d", len(assetPaths))
}

assetPath := assetPaths[0]

if _, err := os.Stat(assetPath); os.IsNotExist(err) {
t.Fatalf("Downloaded file should exist at %s", assetPath)
} else {
fmt.Printf("Verified the downloaded asset exists at %s\n", assetPath)
}
}

func TestInvalidReleaseAssetsRegex(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
Expand Down