Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Retrieve default branch from github API.
Browse files Browse the repository at this point in the history
Signed-off-by: Howard Hellyer <[email protected]>
  • Loading branch information
hhellyer committed Jul 7, 2020
1 parent 38b9896 commit 13c1c80
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
7 changes: 4 additions & 3 deletions cmd/cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"os/exec"
"testing"
Expand Down Expand Up @@ -202,7 +203,7 @@ func testCreateProjectFromTemplate(t *testing.T) {
out, err := cmd.CombinedOutput()
assert.NotNil(t, err)
assert.Contains(t, string(out), "invalid_git_credentials")
assert.Contains(t, string(out), "401 Unauthorized")
assert.Contains(t, string(out), http.StatusText(http.StatusUnauthorized))
})
}

Expand Down Expand Up @@ -418,7 +419,7 @@ func testSuccessfulAddAndRemoveTemplateRepos(t *testing.T) {
createOut, createErr := createCmd.CombinedOutput()
assert.NotNil(t, createErr)
assert.Contains(t, string(createOut), "invalid_git_credentials")
assert.Contains(t, string(createOut), "401 Unauthorized")
assert.Contains(t, string(createOut), http.StatusText(http.StatusUnauthorized))

removeCmd := exec.Command(cwctl, "templates", "repos", "remove",
"--url="+test.GHEDevfileURL,
Expand Down Expand Up @@ -454,7 +455,7 @@ func testSuccessfulAddAndRemoveTemplateRepos(t *testing.T) {
createOut, createErr := createCmd.CombinedOutput()
assert.NotNil(t, createErr)
assert.Contains(t, string(createOut), "invalid_git_credentials")
assert.Contains(t, string(createOut), "401 Unauthorized")
assert.Contains(t, string(createOut), http.StatusText(http.StatusUnauthorized))

removeCmd := exec.Command(cwctl, "templates", "repos", "remove",
"--url="+test.GHEDevfileURL,
Expand Down
2 changes: 1 addition & 1 deletion pkg/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func DownloadTemplate(destination, url string, gitCredentials *utils.GitCredenti
if err != nil {
errOp := errOpCreateProject
// if 401 error, use invalid credentials error code
if strings.Contains(err.Error(), "401 Unauthorized") {
if err.Error() == http.StatusText(http.StatusUnauthorized) {
errOp = errOpInvalidCredentials
}
return nil, &ProjectError{errOp, err, err.Error()}
Expand Down
4 changes: 2 additions & 2 deletions pkg/project/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestDownloadTemplate(t *testing.T) {

assert.Nil(t, out)
assert.Equal(t, errOpInvalidCredentials, err.Op)
assert.Equal(t, "unexpected status code: 401 Unauthorized", err.Desc)
assert.Equal(t, http.StatusText(http.StatusUnauthorized), err.Desc)
})
t.Run("fail case: download GHE template using bad personalAccessToken)", func(t *testing.T) {
os.RemoveAll(testDir)
Expand All @@ -116,7 +116,7 @@ func TestDownloadTemplate(t *testing.T) {

assert.Nil(t, out)
assert.Equal(t, errOpInvalidCredentials, err.Op)
assert.Equal(t, "unexpected status code: 401 Unauthorized", err.Desc)
assert.Equal(t, http.StatusText(http.StatusUnauthorized), err.Desc)
})
}

Expand Down
23 changes: 21 additions & 2 deletions pkg/utils/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package utils

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -79,7 +80,6 @@ func DownloadFromTarGzURL(URL *url.URL, destination string, gitCredentials *GitC
func getURLToDownloadReleaseAsset(URL *url.URL, gitCredentials *GitCredentials) (*url.URL, error) {
URLPathSlice := strings.Split(URL.Path, "/")


if !strings.Contains(URL.Host, "github") || len(URLPathSlice) < 6 {
return nil, fmt.Errorf("URL must point to a GitHub repository release asset: %v", URL)
}
Expand Down Expand Up @@ -152,7 +152,14 @@ func DownloadFromRepoURL(URL *url.URL, destination string, gitCredentials *GitCr

owner := URLPathSlice[1]
repo := URLPathSlice[2]
zipURL, err := GetZipURL(owner, repo, "master", client)

// Get the default branch rather than assuming a name.
branch, err := getDefaultBranch(owner, repo, client)
if err != nil {
return err
}

zipURL, err := GetZipURL(owner, repo, branch, client)
if err != nil {
return err
}
Expand Down Expand Up @@ -197,6 +204,18 @@ func GetZipURL(owner, repo, branch string, client *github.Client) (*url.URL, err
return URL, nil
}

func getDefaultBranch(owner, repo string, client *github.Client) (string, error) {
ctx := context.Background()
repository, response, err := client.Repositories.Get(ctx, owner, repo)
if err != nil {
if response != nil && response.StatusCode == http.StatusUnauthorized {
return "", errors.New(http.StatusText(http.StatusUnauthorized))
}
return "", err
}
return *repository.DefaultBranch, nil
}

// DownloadAndExtractZip downloads a zip file from a URL
// and extracts it to a destination
func DownloadAndExtractZip(zipURL *url.URL, destination string) error {
Expand Down
3 changes: 2 additions & 1 deletion pkg/utils/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestDownloadFromURLThenExtract(t *testing.T) {
inDestination: filepath.Join(testDir, "failCase"),
inGitCredentials: &GitCredentials{Username: test.GHEUsername, Password: "bad password"},
wantedType: errors.New(""),
wantedErrMsg: "401 Unauthorized",
wantedErrMsg: http.StatusText(http.StatusUnauthorized),
wantedNumFiles: 0,
},
"fail case: input good GHE tar.gz URL and credentials but no matching repo found": {
Expand Down

0 comments on commit 13c1c80

Please sign in to comment.