Skip to content
17 changes: 14 additions & 3 deletions api/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ import (

// CreateSecret represents the API handler to
// create a secret in the configured backend.
//
//nolint:funlen // suppress long function error
func CreateSecret(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
Expand Down Expand Up @@ -136,10 +138,10 @@ func CreateSecret(c *gin.Context) {
}

if strings.EqualFold(t, constants.SecretRepo) {
// retrieve repo name from SCM
// retrieve org and repo name from SCM
//
// same story as org secret. SCM has accurate casing.
scmRepo, err := scm.FromContext(c).GetRepoName(u, o, n)
scmOrg, scmRepo, err := scm.FromContext(c).GetOrgAndRepoName(u, o, n)
if err != nil {
retErr := fmt.Errorf("unable to retrieve repository %s/%s", o, n)

Expand All @@ -148,7 +150,16 @@ func CreateSecret(c *gin.Context) {
return
}

// check if casing is accurate
// check if casing is accurate for org entry
if scmOrg != o {
retErr := fmt.Errorf("unable to retrieve org %s. Did you mean %s?", o, scmOrg)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// check if casing is accurate for repo entry
if scmRepo != n {
retErr := fmt.Errorf("unable to retrieve repository %s. Did you mean %s?", n, scmRepo)

Expand Down
8 changes: 4 additions & 4 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ func (c *client) GetRepo(u *library.User, r *library.Repo) (*library.Repo, error
return toLibraryRepo(*repo), nil
}

// GetRepoName returns the name of the repository in the SCM.
func (c *client) GetRepoName(u *library.User, o string, r string) (string, error) {
// GetOrgAndRepoName returns the name of the org and the repository in the SCM.
func (c *client) GetOrgAndRepoName(u *library.User, o string, r string) (string, string, error) {
c.Logger.WithFields(logrus.Fields{
"org": o,
"repo": r,
Expand All @@ -324,10 +324,10 @@ func (c *client) GetRepoName(u *library.User, o string, r string) (string, error
// send an API call to get the repo info
repo, _, err := client.Repositories.Get(ctx, o, r)
if err != nil {
return "", err
return "", "", err
}

return repo.GetName(), nil
return repo.GetOwner().GetLogin(), repo.GetName(), nil
}

// ListUserRepos returns a list of all repos the user has access to.
Expand Down
19 changes: 12 additions & 7 deletions scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ func TestGithub_GetRepo_Fail(t *testing.T) {
}
}

func TestGithub_GetRepoName(t *testing.T) {
func TestGithub_GetOrgAndRepoName(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

Expand All @@ -1035,12 +1035,13 @@ func TestGithub_GetRepoName(t *testing.T) {
u.SetName("foo")
u.SetToken("bar")

want := "Hello-World"
wantOrg := "octocat"
wantRepo := "Hello-World"

client, _ := NewTest(s.URL)

// run test
got, err := client.GetRepoName(u, "octocat", "Hello-World")
gotOrg, gotRepo, err := client.GetOrgAndRepoName(u, "octocat", "Hello-World")

if resp.Code != http.StatusOK {
t.Errorf("GetRepoName returned %v, want %v", resp.Code, http.StatusOK)
Expand All @@ -1050,12 +1051,16 @@ func TestGithub_GetRepoName(t *testing.T) {
t.Errorf("GetRepoName returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("GetRepoName is %v, want %v", got, want)
if !reflect.DeepEqual(gotOrg, wantOrg) {
t.Errorf("GetRepoName org is %v, want %v", gotOrg, wantOrg)
}

if !reflect.DeepEqual(gotRepo, wantRepo) {
t.Errorf("GetRepoName repo is %v, want %v", gotRepo, wantRepo)
}
}

func TestGithub_GetRepoName_Fail(t *testing.T) {
func TestGithub_GetOrgAndRepoName_Fail(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

Expand All @@ -1079,7 +1084,7 @@ func TestGithub_GetRepoName_Fail(t *testing.T) {
client, _ := NewTest(s.URL)

// run test
_, err := client.GetRepoName(u, "octocat", "Hello-World")
_, _, err := client.GetOrgAndRepoName(u, "octocat", "Hello-World")

if err == nil {
t.Error("GetRepoName should return error")
Expand Down
6 changes: 3 additions & 3 deletions scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ type Service interface {
// GetRepo defines a function that retrieves
// details for a repo.
GetRepo(*library.User, *library.Repo) (*library.Repo, error)
// GetRepoName defines a function that retrieves
// the name of the repo in the SCM.
GetRepoName(*library.User, string, string) (string, error)
// GetOrgAndRepoName defines a function that retrieves
// the name of the org and repo in the SCM.
GetOrgAndRepoName(*library.User, string, string) (string, string, error)
// GetOrg defines a function that retrieves
// the name for an org in the SCM.
GetOrgName(*library.User, string) (string, error)
Expand Down