diff --git a/api/secret.go b/api/secret.go index 46a9d5ea3..c23bd7010 100644 --- a/api/secret.go +++ b/api/secret.go @@ -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) @@ -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) @@ -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) diff --git a/scm/github/repo.go b/scm/github/repo.go index 12a7bb855..cbc858e39 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -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, @@ -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. diff --git a/scm/github/repo_test.go b/scm/github/repo_test.go index 769870f34..2d1d63ad9 100644 --- a/scm/github/repo_test.go +++ b/scm/github/repo_test.go @@ -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) @@ -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) @@ -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) @@ -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") diff --git a/scm/service.go b/scm/service.go index a0d55d564..b6e2fa781 100644 --- a/scm/service.go +++ b/scm/service.go @@ -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)