Skip to content

Commit 59557fa

Browse files
author
Max Jonas Werner
committed
Handle UserLogin properly when creating repositories
- GitHub/Gitea/GitLab providers: The `UserRepositoriesClient.Create` method's `UserRepositoryRef` parameter contains a `UserLogin` field which is not taken into account for this call. Instead, the repository is actually created in the namespace of the user that is tied to the token used by the respective `gitprovider.Client` implementation. If a user now passed in a `UserLogin` that is different from the one derived from the token, the return value of `Create` would still contain the wrong `UserLogin` passed in to `Create` and methods like `GetCloneURL` would return the wrong URL. With this commit, the returned `UserRepository` now points to the correct owner of the repository. - Stash/Bitbucket Server: The Stash provider actually took the field into account and would fail to create the repository even when the token was correct. To align this provider's behaviour with the others, it now also creates the repository under the token user's account and ignores the provided `UserRepositoryRef.UserLogin` field. Signed-off-by: Max Jonas Werner <[email protected]>
1 parent 2200758 commit 59557fa

15 files changed

+88
-25
lines changed

gitea/client_repositories_org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (c *OrgRepositoriesClient) List(ctx context.Context, ref gitprovider.Organi
7777
// apiObj is already validated at ListOrgRepos
7878
repos = append(repos, newOrgRepository(c.clientContext, apiObj, gitprovider.OrgRepositoryRef{
7979
OrganizationRef: ref,
80-
RepositoryName: *&apiObj.Name,
80+
RepositoryName: apiObj.Name,
8181
}))
8282
}
8383
return repos, nil

gitea/client_repositories_user.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package gitea
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223

2324
"code.gitea.io/sdk/gitea"
2425
"github.com/fluxcd/go-git-providers/gitprovider"
@@ -111,6 +112,12 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
111112
if err != nil {
112113
return nil, err
113114
}
115+
116+
if apiObj.Owner == nil {
117+
return nil, fmt.Errorf("returned API object doesn't have an owner")
118+
}
119+
ref.UserLogin = apiObj.Owner.UserName
120+
114121
return newUserRepository(c.clientContext, apiObj, ref), nil
115122
}
116123

gitea/client_repository_commit.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import (
2424
"github.com/fluxcd/go-git-providers/gitprovider"
2525
)
2626

27-
var giteaNewFileMode = "100644"
28-
var giteaBlobTypeFile = "blob"
29-
3027
// CommitClient implements the gitprovider.CommitClient interface.
3128
var _ gitprovider.CommitClient = &CommitClient{}
3229

gitea/integration_repositories_org_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ var _ = Describe("Gitea Provider", func() {
6161
Expect(err).ToNot(HaveOccurred())
6262

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

6969
// We know that a repo with this name doesn't exist in the organization, let's verify we get an

gitea/integration_repositories_user_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var _ = Describe("Gitea Provider", func() {
7070
}
7171

7272
fmt.Print("Creating repository ", testRepoName, "...")
73-
repoRef := newUserRepoRef(giteaUser, testRepoName)
73+
repoRef := newUserRepoRef(testRepoName)
7474

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

103+
It("should return correct repo info when creating a repository with wrong UserLogin", func() {
104+
repoName := fmt.Sprintf("test-user-repo-creation-%03d", rand.Intn(1000))
105+
repoRef := newUserRepoRef(repoName)
106+
repoRef.UserLogin = "yadda-yadda-yada"
107+
108+
repo, err := c.UserRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})
109+
110+
Expect(err).To(BeNil())
111+
Expect(
112+
repo.Repository().GetCloneURL(gitprovider.TransportTypeHTTPS)).
113+
To(Equal(fmt.Sprintf("%s/%s/%s.git", giteaBaseUrl, giteaUser, repoName)))
114+
})
115+
103116
It("should error at creation time if the repo already does exist", func() {
104-
repoRef := newUserRepoRef(giteaUser, testRepoName)
117+
repoRef := newUserRepoRef(testRepoName)
105118
_, err := c.UserRepositories().Get(ctx, repoRef)
106119
Expect(err).ToNot(HaveOccurred())
107120

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

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

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

282295
It("should be possible to download files from path and branch specified", func() {
283296
testRepoName = fmt.Sprintf("test-repo-tree-%03d", rand.Intn(1000))
284-
userRepoRef := newUserRepoRef(giteaUser, testRepoName)
297+
userRepoRef := newUserRepoRef(testRepoName)
285298
repo, err := c.UserRepositories().Create(ctx, userRepoRef, gitprovider.RepositoryInfo{
286299
DefaultBranch: gitprovider.StringVar(defaultBranch),
287300
Description: gitprovider.StringVar(defaultDescription),

gitea/integration_suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ func newUserRef(userLogin string) gitprovider.UserRef {
243243
}
244244
}
245245

246-
func newUserRepoRef(userLogin, repoName string) gitprovider.UserRepositoryRef {
246+
func newUserRepoRef(repoName string) gitprovider.UserRepositoryRef {
247247
return gitprovider.UserRepositoryRef{
248-
UserRef: newUserRef(userLogin),
248+
UserRef: newUserRef(giteaUser),
249249
RepositoryName: repoName,
250250
}
251251
}

gitea/resource_repository.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (r *userRepository) Update(ctx context.Context) error {
164164
Archived: &r.r.Archived,
165165
DefaultMergeStyle: &r.r.DefaultMergeStyle,
166166
}
167-
if r.r.Mirror == true {
167+
if r.r.Mirror {
168168
opts.MirrorInterval = &r.r.MirrorInterval
169169
}
170170
apiObj, err := updateRepo(r.c, r.ref.GetIdentity(), r.ref.GetRepository(), &opts)
@@ -261,7 +261,7 @@ func validateRepositoryAPI(apiObj *gitea.Repository) error {
261261
validator.Required("Name")
262262
}
263263
// Make sure visibility is valid if set
264-
if apiObj.Private != true {
264+
if !apiObj.Private {
265265
v := gitprovider.RepositoryVisibility("public")
266266
validator.Append(gitprovider.ValidateRepositoryVisibility(v), v, "Visibility")
267267
} else {
@@ -276,7 +276,7 @@ func repositoryFromAPI(apiObj *gitea.Repository) gitprovider.RepositoryInfo {
276276
Description: &apiObj.Description,
277277
DefaultBranch: &apiObj.DefaultBranch,
278278
}
279-
if apiObj.Private != true {
279+
if !apiObj.Private {
280280
repo.Visibility = gitprovider.RepositoryVisibilityVar(gitprovider.RepositoryVisibility("public"))
281281
} else {
282282
repo.Visibility = gitprovider.RepositoryVisibilityVar(gitprovider.RepositoryVisibility("private"))

gitea/util.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ import (
2626
"github.com/fluxcd/go-git-providers/validation"
2727
)
2828

29-
const (
30-
alreadyExistsMagicString = "name already exists on this account"
31-
)
32-
33-
// TODO: Guard better against nil pointer dereference panics in this package, also
34-
// validate data coming from the server
35-
3629
// validateUserRepositoryRef makes sure the UserRepositoryRef is valid for Gitea's usage.
3730
func validateUserRepositoryRef(ref gitprovider.UserRepositoryRef, expectedDomain string) error {
3831
// Make sure the RepositoryRef fields are valid

github/client_repositories_user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package github
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223

2324
"github.com/fluxcd/go-git-providers/gitprovider"
2425
)
@@ -91,6 +92,13 @@ func (c *UserRepositoriesClient) Create(ctx context.Context,
9192
if err != nil {
9293
return nil, err
9394
}
95+
96+
owner := apiObj.GetOwner()
97+
if owner == nil {
98+
return nil, fmt.Errorf("returned API object doesn't have an owner")
99+
}
100+
ref.UserLogin = *owner.Login
101+
94102
return newUserRepository(c.clientContext, apiObj, ref), nil
95103
}
96104

github/integration_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ var _ = Describe("GitHub Provider", func() {
345345
Expect(getSpec.Equals(postSpec)).To(BeTrue())
346346
})
347347

348+
It("should return correct repo info when creating a repository with wrong UserLogin", func() {
349+
repoName := fmt.Sprintf("test-user-repo-creation-%03d", rand.Intn(1000))
350+
repoRef := newUserRepoRef("yadda-yadda-yada", repoName)
351+
352+
repo, err := c.UserRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})
353+
354+
Expect(err).To(BeNil())
355+
Expect(
356+
repo.Repository().GetCloneURL(gitprovider.TransportTypeHTTPS)).
357+
To(Equal(fmt.Sprintf("https://%s/%s/%s.git", githubDomain, testUser, repoName)))
358+
})
359+
348360
It("should error at creation time if the repo already does exist", func() {
349361
repoRef := newOrgRepoRef(testOrgName, testOrgRepoName)
350362
_, err := c.OrgRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{})

0 commit comments

Comments
 (0)