Skip to content

Commit d5f82cf

Browse files
committed
Fix bug
1 parent 249e9cc commit d5f82cf

File tree

7 files changed

+27
-18
lines changed

7 files changed

+27
-18
lines changed

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ func Routes() *web.Router {
11721172
m.Get("", reqAnyRepoReader(), repo.ListCollaborators)
11731173
m.Group("/{collaborator}", func() {
11741174
m.Combo("").Get(reqAnyRepoReader(), repo.IsCollaborator).
1175-
Put(reqAdmin(), bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
1175+
Put(reqAdmin(), bind(api.AddCollaboratorOption{}), repo.AddOrUpdateCollaborator).
11761176
Delete(reqAdmin(), repo.DeleteCollaborator)
11771177
m.Get("/permission", repo.GetRepoPermissions)
11781178
})

routers/api/v1/repo/collaborators.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ func IsCollaborator(ctx *context.APIContext) {
122122
}
123123
}
124124

125-
// AddCollaborator add a collaborator to a repository
126-
func AddCollaborator(ctx *context.APIContext) {
125+
// AddOrUpdateCollaborator add or update a collaborator to a repository
126+
func AddOrUpdateCollaborator(ctx *context.APIContext) {
127127
// swagger:operation PUT /repos/{owner}/{repo}/collaborators/{collaborator} repository repoAddCollaborator
128128
// ---
129-
// summary: Add a collaborator to a repository
129+
// summary: Add or Update a collaborator to a repository
130130
// produces:
131131
// - application/json
132132
// parameters:
@@ -181,11 +181,11 @@ func AddCollaborator(ctx *context.APIContext) {
181181
p = perm.ParseAccessMode(*form.Permission)
182182
}
183183

184-
if err := repo_service.AddCollaborator(ctx, ctx.Repo.Repository, collaborator, p); err != nil {
184+
if err := repo_service.AddOrUpdateCollaborator(ctx, ctx.Repo.Repository, collaborator, p); err != nil {
185185
if errors.Is(err, user_model.ErrBlockedUser) {
186-
ctx.Error(http.StatusForbidden, "AddCollaborator", err)
186+
ctx.Error(http.StatusForbidden, "AddOrUpdateCollaborator", err)
187187
} else {
188-
ctx.Error(http.StatusInternalServerError, "AddCollaborator", err)
188+
ctx.Error(http.StatusInternalServerError, "AddOrUpdateCollaborator", err)
189189
}
190190
return
191191
}

routers/web/repo/setting/collaboration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ func CollaborationPost(ctx *context.Context) {
9999
}
100100
}
101101

102-
if err = repo_service.AddCollaborator(ctx, ctx.Repo.Repository, u, perm.AccessModeWrite); err != nil {
102+
if err = repo_service.AddOrUpdateCollaborator(ctx, ctx.Repo.Repository, u, perm.AccessModeWrite); err != nil {
103103
if errors.Is(err, user_model.ErrBlockedUser) {
104104
ctx.Flash.Error(ctx.Tr("repo.settings.add_collaborator.blocked_user"))
105105
ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
106106
} else {
107-
ctx.ServerError("AddCollaborator", err)
107+
ctx.ServerError("AddOrUpdateCollaborator", err)
108108
}
109109
return
110110
}

services/repository/collaboration.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"xorm.io/builder"
1818
)
1919

20-
func AddCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_model.User, mode perm.AccessMode) error {
20+
func AddOrUpdateCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_model.User, mode perm.AccessMode) error {
2121
if err := repo.LoadOwner(ctx); err != nil {
2222
return err
2323
}
@@ -27,17 +27,26 @@ func AddCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_m
2727
}
2828

2929
return db.WithTx(ctx, func(ctx context.Context) error {
30-
has, err := db.Exist[repo_model.Collaboration](ctx, builder.Eq{
30+
collaboration, has, err := db.Get[repo_model.Collaboration](ctx, builder.Eq{
3131
"repo_id": repo.ID,
3232
"user_id": u.ID,
3333
})
3434
if err != nil {
3535
return err
3636
} else if has {
37-
return nil
38-
}
39-
40-
if err = db.Insert(ctx, &repo_model.Collaboration{
37+
if collaboration.Mode == mode {
38+
return nil
39+
}
40+
if _, err = db.GetEngine(ctx).
41+
Where("repo_id=?", repo.ID).
42+
And("user_id=?", u.ID).
43+
Cols("mode").
44+
Update(&repo_model.Collaboration{
45+
Mode: mode,
46+
}); err != nil {
47+
return err
48+
}
49+
} else if err = db.Insert(ctx, &repo_model.Collaboration{
4150
RepoID: repo.ID,
4251
UserID: u.ID,
4352
Mode: mode,

services/repository/collaboration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRepository_AddCollaborator(t *testing.T) {
2222
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
2323
assert.NoError(t, repo.LoadOwner(db.DefaultContext))
2424
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
25-
assert.NoError(t, AddCollaborator(db.DefaultContext, repo, user, perm.AccessModeWrite))
25+
assert.NoError(t, AddOrUpdateCollaborator(db.DefaultContext, repo, user, perm.AccessModeWrite))
2626
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID}, &user_model.User{ID: userID})
2727
}
2828
testSuccess(1, 4)

services/repository/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, re
453453
return fmt.Errorf("IsUserRepoAdmin: %w", err)
454454
} else if !isAdmin {
455455
// Make creator repo admin if it wasn't assigned automatically
456-
if err = AddCollaborator(ctx, repo, doer, perm.AccessModeAdmin); err != nil {
456+
if err = AddOrUpdateCollaborator(ctx, repo, doer, perm.AccessModeAdmin); err != nil {
457457
return fmt.Errorf("AddCollaborator: %w", err)
458458
}
459459
}

services/repository/transfer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func StartRepositoryTransfer(ctx context.Context, doer, newOwner *user_model.Use
418418
return err
419419
}
420420
if !hasAccess {
421-
if err := AddCollaborator(ctx, repo, newOwner, perm.AccessModeRead); err != nil {
421+
if err := AddOrUpdateCollaborator(ctx, repo, newOwner, perm.AccessModeRead); err != nil {
422422
return err
423423
}
424424
}

0 commit comments

Comments
 (0)