Skip to content

Commit

Permalink
Merge pull request #229 from fluxcd/fix-userlogin-usage
Browse files Browse the repository at this point in the history
Return correct repo info after creation
  • Loading branch information
makkes authored Jun 21, 2023
2 parents 2200758 + 59557fa commit 7161fd0
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 25 deletions.
2 changes: 1 addition & 1 deletion gitea/client_repositories_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *OrgRepositoriesClient) List(ctx context.Context, ref gitprovider.Organi
// apiObj is already validated at ListOrgRepos
repos = append(repos, newOrgRepository(c.clientContext, apiObj, gitprovider.OrgRepositoryRef{
OrganizationRef: ref,
RepositoryName: *&apiObj.Name,
RepositoryName: apiObj.Name,
}))
}
return repos, nil
Expand Down
7 changes: 7 additions & 0 deletions gitea/client_repositories_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gitea
import (
"context"
"errors"
"fmt"

"code.gitea.io/sdk/gitea"
"github.com/fluxcd/go-git-providers/gitprovider"
Expand Down Expand Up @@ -111,6 +112,12 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
if err != nil {
return nil, err
}

if apiObj.Owner == nil {
return nil, fmt.Errorf("returned API object doesn't have an owner")
}
ref.UserLogin = apiObj.Owner.UserName

return newUserRepository(c.clientContext, apiObj, ref), nil
}

Expand Down
3 changes: 0 additions & 3 deletions gitea/client_repository_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ import (
"github.com/fluxcd/go-git-providers/gitprovider"
)

var giteaNewFileMode = "100644"
var giteaBlobTypeFile = "blob"

// CommitClient implements the gitprovider.CommitClient interface.
var _ gitprovider.CommitClient = &CommitClient{}

Expand Down
4 changes: 2 additions & 2 deletions gitea/integration_repositories_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ var _ = Describe("Gitea Provider", func() {
Expect(err).ToNot(HaveOccurred())

// Generate a repository name which doesn't exist already
testOrgRepoName = fmt.Sprintf("test-repo-%03d", rand.Intn(1000))
testOrgRepoName = fmt.Sprintf("test-org-repo-%03d", rand.Intn(1000))
for findOrgRepo(repos, testOrgRepoName) != nil {
testOrgRepoName = fmt.Sprintf("test-repo-%03d", rand.Intn(1000))
testOrgRepoName = fmt.Sprintf("test-org-repo-%03d", rand.Intn(1000))
}

// We know that a repo with this name doesn't exist in the organization, let's verify we get an
Expand Down
23 changes: 18 additions & 5 deletions gitea/integration_repositories_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var _ = Describe("Gitea Provider", func() {
}

fmt.Print("Creating repository ", testRepoName, "...")
repoRef := newUserRepoRef(giteaUser, testRepoName)
repoRef := newUserRepoRef(testRepoName)

// Check that the repository doesn't exist
_, err = c.UserRepositories().Get(ctx, repoRef)
Expand Down Expand Up @@ -100,8 +100,21 @@ var _ = Describe("Gitea Provider", func() {
Expect(getSpec.Equals(postSpec)).To(BeTrue())
})

It("should return correct repo info when creating a repository with wrong UserLogin", func() {
repoName := fmt.Sprintf("test-user-repo-creation-%03d", rand.Intn(1000))
repoRef := newUserRepoRef(repoName)
repoRef.UserLogin = "yadda-yadda-yada"

repo, err := c.UserRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})

Expect(err).To(BeNil())
Expect(
repo.Repository().GetCloneURL(gitprovider.TransportTypeHTTPS)).
To(Equal(fmt.Sprintf("%s/%s/%s.git", giteaBaseUrl, giteaUser, repoName)))
})

It("should error at creation time if the repo already does exist", func() {
repoRef := newUserRepoRef(giteaUser, testRepoName)
repoRef := newUserRepoRef(testRepoName)
_, err := c.UserRepositories().Get(ctx, repoRef)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -110,7 +123,7 @@ var _ = Describe("Gitea Provider", func() {
})

It("should update if the repository already exists when reconciling", func() {
repoRef := newUserRepoRef(giteaUser, testRepoName)
repoRef := newUserRepoRef(testRepoName)
// No-op reconcile
resp, actionTaken, err := c.UserRepositories().Reconcile(ctx, repoRef, gitprovider.RepositoryInfo{
Description: gitprovider.StringVar(defaultDescription),
Expand Down Expand Up @@ -189,7 +202,7 @@ var _ = Describe("Gitea Provider", func() {

It("should be possible to create a pr for a user repository", func() {
testRepoName = fmt.Sprintf("test-user-repo2-%03d", rand.Intn(1000))
repoRef := newUserRepoRef(giteaUser, testRepoName)
repoRef := newUserRepoRef(testRepoName)
description := "test description"
// Create a new repo
userRepo, err := c.UserRepositories().Create(ctx, repoRef,
Expand Down Expand Up @@ -281,7 +294,7 @@ var _ = Describe("Gitea Provider", func() {

It("should be possible to download files from path and branch specified", func() {
testRepoName = fmt.Sprintf("test-repo-tree-%03d", rand.Intn(1000))
userRepoRef := newUserRepoRef(giteaUser, testRepoName)
userRepoRef := newUserRepoRef(testRepoName)
repo, err := c.UserRepositories().Create(ctx, userRepoRef, gitprovider.RepositoryInfo{
DefaultBranch: gitprovider.StringVar(defaultBranch),
Description: gitprovider.StringVar(defaultDescription),
Expand Down
4 changes: 2 additions & 2 deletions gitea/integration_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ func newUserRef(userLogin string) gitprovider.UserRef {
}
}

func newUserRepoRef(userLogin, repoName string) gitprovider.UserRepositoryRef {
func newUserRepoRef(repoName string) gitprovider.UserRepositoryRef {
return gitprovider.UserRepositoryRef{
UserRef: newUserRef(userLogin),
UserRef: newUserRef(giteaUser),
RepositoryName: repoName,
}
}
Expand Down
6 changes: 3 additions & 3 deletions gitea/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (r *userRepository) Update(ctx context.Context) error {
Archived: &r.r.Archived,
DefaultMergeStyle: &r.r.DefaultMergeStyle,
}
if r.r.Mirror == true {
if r.r.Mirror {
opts.MirrorInterval = &r.r.MirrorInterval
}
apiObj, err := updateRepo(r.c, r.ref.GetIdentity(), r.ref.GetRepository(), &opts)
Expand Down Expand Up @@ -261,7 +261,7 @@ func validateRepositoryAPI(apiObj *gitea.Repository) error {
validator.Required("Name")
}
// Make sure visibility is valid if set
if apiObj.Private != true {
if !apiObj.Private {
v := gitprovider.RepositoryVisibility("public")
validator.Append(gitprovider.ValidateRepositoryVisibility(v), v, "Visibility")
} else {
Expand All @@ -276,7 +276,7 @@ func repositoryFromAPI(apiObj *gitea.Repository) gitprovider.RepositoryInfo {
Description: &apiObj.Description,
DefaultBranch: &apiObj.DefaultBranch,
}
if apiObj.Private != true {
if !apiObj.Private {
repo.Visibility = gitprovider.RepositoryVisibilityVar(gitprovider.RepositoryVisibility("public"))
} else {
repo.Visibility = gitprovider.RepositoryVisibilityVar(gitprovider.RepositoryVisibility("private"))
Expand Down
7 changes: 0 additions & 7 deletions gitea/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ import (
"github.com/fluxcd/go-git-providers/validation"
)

const (
alreadyExistsMagicString = "name already exists on this account"
)

// TODO: Guard better against nil pointer dereference panics in this package, also
// validate data coming from the server

// validateUserRepositoryRef makes sure the UserRepositoryRef is valid for Gitea's usage.
func validateUserRepositoryRef(ref gitprovider.UserRepositoryRef, expectedDomain string) error {
// Make sure the RepositoryRef fields are valid
Expand Down
8 changes: 8 additions & 0 deletions github/client_repositories_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package github
import (
"context"
"errors"
"fmt"

"github.com/fluxcd/go-git-providers/gitprovider"
)
Expand Down Expand Up @@ -91,6 +92,13 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
if err != nil {
return nil, err
}

owner := apiObj.GetOwner()
if owner == nil {
return nil, fmt.Errorf("returned API object doesn't have an owner")
}
ref.UserLogin = *owner.Login

return newUserRepository(c.clientContext, apiObj, ref), nil
}

Expand Down
12 changes: 12 additions & 0 deletions github/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,18 @@ var _ = Describe("GitHub Provider", func() {
Expect(getSpec.Equals(postSpec)).To(BeTrue())
})

It("should return correct repo info when creating a repository with wrong UserLogin", func() {
repoName := fmt.Sprintf("test-user-repo-creation-%03d", rand.Intn(1000))
repoRef := newUserRepoRef("yadda-yadda-yada", repoName)

repo, err := c.UserRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})

Expect(err).To(BeNil())
Expect(
repo.Repository().GetCloneURL(gitprovider.TransportTypeHTTPS)).
To(Equal(fmt.Sprintf("https://%s/%s/%s.git", githubDomain, testUser, repoName)))
})

It("should error at creation time if the repo already does exist", func() {
repoRef := newOrgRepoRef(testOrgName, testOrgRepoName)
_, err := c.OrgRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})
Expand Down
8 changes: 8 additions & 0 deletions gitlab/client_repositories_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gitlab
import (
"context"
"errors"
"fmt"

"github.com/fluxcd/go-git-providers/gitprovider"
)
Expand Down Expand Up @@ -82,6 +83,7 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
req gitprovider.RepositoryInfo,
opts ...gitprovider.RepositoryCreateOption,
) (gitprovider.UserRepository, error) {

// Make sure the RepositoryRef is valid
if err := validateUserRepositoryRef(ref, c.domain); err != nil {
return nil, err
Expand All @@ -91,6 +93,12 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
if err != nil {
return nil, err
}

if apiObj.Owner == nil {
return nil, fmt.Errorf("returned API object doesn't have an owner")
}
ref.UserLogin = apiObj.Owner.Username

return newUserProject(c.clientContext, apiObj, ref), nil
}

Expand Down
12 changes: 12 additions & 0 deletions gitlab/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,18 @@ var _ = Describe("GitLab Provider", func() {
Expect(errors.Is(err, gitprovider.ErrAlreadyExists)).To(BeTrue())
})

It("should return correct repo info when creating a repository with wrong UserLogin", func() {
repoName := fmt.Sprintf("test-user-repo-creation-%03d", rand.Intn(1000))
repoRef := newUserRepoRef(testBaseUrl, "yadda-yadda-yada", repoName)

repo, err := c.UserRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})

Expect(err).To(BeNil())
Expect(
repo.Repository().GetCloneURL(gitprovider.TransportTypeHTTPS)).
To(Equal(fmt.Sprintf("%s/%s/%s.git", testBaseUrl, testUserName, repoName)))
})

It("should update if the user repo already exists when reconciling", func() {
repoRef := newUserRepoRef(testBaseUrl, testUserName, testRepoName)
// No-op reconcile
Expand Down
3 changes: 2 additions & 1 deletion stash/client_repositories_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
return nil, err
}

apiObj, err := createRepository(ctx, c.client, addTilde(ref.UserLogin), ref, req, opts...)
apiObj, err := createRepository(ctx, c.client, addTilde(c.client.username), ref, req, opts...)
if err != nil {
if errors.Is(err, ErrAlreadyExists) {
return nil, gitprovider.ErrAlreadyExists
Expand All @@ -137,6 +137,7 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
}

ref.SetSlug(apiObj.Slug)
ref.UserLogin = apiObj.UserName

return newUserRepository(c.clientContext, apiObj, ref), nil
}
Expand Down
12 changes: 12 additions & 0 deletions stash/integration_repositories_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ var _ = Describe("Stash Provider", func() {
Expect(getSpec.Equals(postSpec)).To(BeTrue())
})

It("should return correct repo info when creating a repository with wrong UserLogin", func() {
repoName := fmt.Sprintf("test-user-repo-creation-%03d", rand.Intn(1000))
repoRef := newUserRepoRef("yadda-yadda-yadda", repoName)

repo, err := client.UserRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})

Expect(err).To(BeNil(), "repository with different UserLogin failed")
Expect(
repo.Repository().GetCloneURL(gitprovider.TransportTypeHTTPS)).
To(Equal(fmt.Sprintf("https://%s/%s/%s.git", stashDomain, stashUser, repoName)))
})

It("should error at creation time if the user repo already does exist", func() {
repoRef := newUserRepoRef(stashUser, testRepoName)
_, err := client.UserRepositories().Get(ctx, repoRef)
Expand Down
2 changes: 1 addition & 1 deletion stash/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (s *RepositoriesService) Create(ctx context.Context, projectKey string, rep
return nil, fmt.Errorf("create respository failed: %w", err)
}

if resp != nil && resp.StatusCode == http.StatusBadRequest {
if resp != nil && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("create repository failed: %s", resp.Status)
}

Expand Down

0 comments on commit 7161fd0

Please sign in to comment.