Skip to content

Commit

Permalink
Merge pull request #5 from gruntwork-io/enable-fetch-to-pull-from-branch
Browse files Browse the repository at this point in the history
Fetch was not downloading commits or branches from private repos.
  • Loading branch information
josh-padnick committed Apr 29, 2016
2 parents 2f705ad + e36d67e commit f5ed36b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 10 additions & 11 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f5ed36b

Please sign in to comment.