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

Commit bda160e

Browse files
authored
Merge pull request #507 from hhellyer/0.14.0_default_branch_selection
[0.14.0] Retrieve default branch from github API.
2 parents 38b9896 + 13c1c80 commit bda160e

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

cmd/cli/main_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package main
1414
import (
1515
"encoding/json"
1616
"io/ioutil"
17+
"net/http"
1718
"os"
1819
"os/exec"
1920
"testing"
@@ -202,7 +203,7 @@ func testCreateProjectFromTemplate(t *testing.T) {
202203
out, err := cmd.CombinedOutput()
203204
assert.NotNil(t, err)
204205
assert.Contains(t, string(out), "invalid_git_credentials")
205-
assert.Contains(t, string(out), "401 Unauthorized")
206+
assert.Contains(t, string(out), http.StatusText(http.StatusUnauthorized))
206207
})
207208
}
208209

@@ -418,7 +419,7 @@ func testSuccessfulAddAndRemoveTemplateRepos(t *testing.T) {
418419
createOut, createErr := createCmd.CombinedOutput()
419420
assert.NotNil(t, createErr)
420421
assert.Contains(t, string(createOut), "invalid_git_credentials")
421-
assert.Contains(t, string(createOut), "401 Unauthorized")
422+
assert.Contains(t, string(createOut), http.StatusText(http.StatusUnauthorized))
422423

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

459460
removeCmd := exec.Command(cwctl, "templates", "repos", "remove",
460461
"--url="+test.GHEDevfileURL,

pkg/project/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func DownloadTemplate(destination, url string, gitCredentials *utils.GitCredenti
7474
if err != nil {
7575
errOp := errOpCreateProject
7676
// if 401 error, use invalid credentials error code
77-
if strings.Contains(err.Error(), "401 Unauthorized") {
77+
if err.Error() == http.StatusText(http.StatusUnauthorized) {
7878
errOp = errOpInvalidCredentials
7979
}
8080
return nil, &ProjectError{errOp, err, err.Error()}

pkg/project/create_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestDownloadTemplate(t *testing.T) {
9999

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

117117
assert.Nil(t, out)
118118
assert.Equal(t, errOpInvalidCredentials, err.Op)
119-
assert.Equal(t, "unexpected status code: 401 Unauthorized", err.Desc)
119+
assert.Equal(t, http.StatusText(http.StatusUnauthorized), err.Desc)
120120
})
121121
}
122122

pkg/utils/download.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package utils
1313

1414
import (
1515
"context"
16+
"errors"
1617
"fmt"
1718
"io"
1819
"net/http"
@@ -79,7 +80,6 @@ func DownloadFromTarGzURL(URL *url.URL, destination string, gitCredentials *GitC
7980
func getURLToDownloadReleaseAsset(URL *url.URL, gitCredentials *GitCredentials) (*url.URL, error) {
8081
URLPathSlice := strings.Split(URL.Path, "/")
8182

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

153153
owner := URLPathSlice[1]
154154
repo := URLPathSlice[2]
155-
zipURL, err := GetZipURL(owner, repo, "master", client)
155+
156+
// Get the default branch rather than assuming a name.
157+
branch, err := getDefaultBranch(owner, repo, client)
158+
if err != nil {
159+
return err
160+
}
161+
162+
zipURL, err := GetZipURL(owner, repo, branch, client)
156163
if err != nil {
157164
return err
158165
}
@@ -197,6 +204,18 @@ func GetZipURL(owner, repo, branch string, client *github.Client) (*url.URL, err
197204
return URL, nil
198205
}
199206

207+
func getDefaultBranch(owner, repo string, client *github.Client) (string, error) {
208+
ctx := context.Background()
209+
repository, response, err := client.Repositories.Get(ctx, owner, repo)
210+
if err != nil {
211+
if response != nil && response.StatusCode == http.StatusUnauthorized {
212+
return "", errors.New(http.StatusText(http.StatusUnauthorized))
213+
}
214+
return "", err
215+
}
216+
return *repository.DefaultBranch, nil
217+
}
218+
200219
// DownloadAndExtractZip downloads a zip file from a URL
201220
// and extracts it to a destination
202221
func DownloadAndExtractZip(zipURL *url.URL, destination string) error {

pkg/utils/download_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"errors"
1616
"fmt"
1717
"io/ioutil"
18+
"net/http"
1819
"net/url"
1920
"os"
2021
"path/filepath"
@@ -136,7 +137,7 @@ func TestDownloadFromURLThenExtract(t *testing.T) {
136137
inDestination: filepath.Join(testDir, "failCase"),
137138
inGitCredentials: &GitCredentials{Username: test.GHEUsername, Password: "bad password"},
138139
wantedType: errors.New(""),
139-
wantedErrMsg: "401 Unauthorized",
140+
wantedErrMsg: http.StatusText(http.StatusUnauthorized),
140141
wantedNumFiles: 0,
141142
},
142143
"fail case: input good GHE tar.gz URL and credentials but no matching repo found": {

0 commit comments

Comments
 (0)