From e36d67e753a72c529ff7e5519927a95c809d17fd Mon Sep 17 00:00:00 2001 From: Josh Padnick Date: Thu, 28 Apr 2016 17:03:38 -0700 Subject: [PATCH] Fix issue where fetch did not download commits or branches from private repos. --- file.go | 21 ++++++++++----------- file_test.go | 2 ++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/file.go b/file.go index 613a8a8..ae79c20 100644 --- a/file.go +++ b/file.go @@ -37,19 +37,16 @@ func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string) (strin if err != nil { return zipFilePath, wrapError(err) } - - // Load the resp.Body into a buffer so we can convert it to a string or []bytes as necessary - respBodyBuffer := new(bytes.Buffer) - respBodyBuffer.ReadFrom(resp.Body) - if resp.StatusCode != 200 { - return zipFilePath, newError(FAILED_TO_DOWNLOAD_FILE, fmt.Sprintf("Failed to download file at the url %s. Received HTTP Response %d. Body: %s", req.URL.String(), resp.StatusCode, respBodyBuffer.String())) + return zipFilePath, newError(FAILED_TO_DOWNLOAD_FILE, fmt.Sprintf("Failed to download file at the url %s. Received HTTP Response %d.", req.URL.String(), resp.StatusCode)) } if resp.Header.Get("Content-Type") != "application/zip" { return zipFilePath, newError(FAILED_TO_DOWNLOAD_FILE, fmt.Sprintf("Failed to download file at the url %s. Expected HTTP Response's \"Content-Type\" header to be \"application/zip\", but was \"%s\"", req.URL.String(), resp.Header.Get("Content-Type"))) } // Copy the contents of the downloaded file to our empty file + respBodyBuffer := new(bytes.Buffer) + respBodyBuffer.ReadFrom(resp.Body) err = ioutil.WriteFile(filepath.Join(tempDir, "repo.zip"), respBodyBuffer.Bytes(), 0644) if err != nil { return zipFilePath, wrapError(err) @@ -120,18 +117,20 @@ func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) erro func MakeGitHubZipFileRequest(gitHubCommit GitHubCommit, gitHubToken string) (*http.Request, error) { var request *http.Request - var url string - + // This represents either a commit, branch, or git tag + var gitRef string if gitHubCommit.CommitSha != "" { - url = fmt.Sprintf("https://github.com/%s/%s/archive/%s.zip", gitHubCommit.Repo.Owner, gitHubCommit.Repo.Name, gitHubCommit.CommitSha) + gitRef = gitHubCommit.CommitSha } else if gitHubCommit.BranchName != "" { - url = fmt.Sprintf("https://github.com/%s/%s/archive/%s.zip", gitHubCommit.Repo.Owner, gitHubCommit.Repo.Name, gitHubCommit.BranchName) + gitRef = gitHubCommit.BranchName } else if gitHubCommit.GitTag != "" { - url = fmt.Sprintf("https://api.github.com/repos/%s/%s/zipball/%s", gitHubCommit.Repo.Owner, gitHubCommit.Repo.Name, gitHubCommit.GitTag) + gitRef = gitHubCommit.GitTag } else { return request, fmt.Errorf("Neither a GitCommitSha nor a GitTag nor a BranchName were specified so impossible to identify a specific commit to download.") } + url := fmt.Sprintf("https://api.github.com/repos/%s/%s/zipball/%s", gitHubCommit.Repo.Owner, gitHubCommit.Repo.Name, gitRef) + request, err := http.NewRequest("GET", url, nil) if err != nil { return request, wrapError(err) diff --git a/file_test.go b/file_test.go index 3ef7e75..8faf9d3 100644 --- a/file_test.go +++ b/file_test.go @@ -62,6 +62,7 @@ func TestDownloadGitBranchZipFile(t *testing.T) { githubToken string }{ {"gruntwork-io", "fetch-test-public", "sample-branch", ""}, + {"gruntwork-io", "fetch-test-private", "sample-branch", os.Getenv("GITHUB_OAUTH_TOKEN")}, } for _, tc := range cases { @@ -126,6 +127,7 @@ func TestDownloadGitCommitFile(t *testing.T) { {"gruntwork-io", "fetch-test-public", "d2de34edb4c6564e0674b3f390b3b1fb0468183a", ""}, {"gruntwork-io", "fetch-test-public", "57752e7f1df0acbd3c1e61545d5c4d0e87699d84", ""}, {"gruntwork-io", "fetch-test-public", "f32a08313e30f116a1f5617b8b68c11f1c1dbb61", ""}, + {"gruntwork-io", "fetch-test-private", "676cfb92b54d33538c756c7a9479bfc3f6b44de2", os.Getenv("GITHUB_OAUTH_TOKEN")}, } for _, tc := range cases {