diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-09-09 05:09:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-08 21:09:23 +0000 |
commit | e3ed67859a434f2fef1db175e9c82251a7f9f6c9 (patch) | |
tree | e4c48b0c17d6d5a9507c3fc1479d7da2faba59dd /services | |
parent | b8ad558c93052ee3e2f43f1e24e6f5c134da36c4 (diff) | |
download | gitea-e3ed67859a434f2fef1db175e9c82251a7f9f6c9.tar.gz gitea-e3ed67859a434f2fef1db175e9c82251a7f9f6c9.zip |
Move some functions to service layer (#26969)
Diffstat (limited to 'services')
-rw-r--r-- | services/externalaccount/user.go | 25 | ||||
-rw-r--r-- | services/migrations/gitea_uploader.go | 10 | ||||
-rw-r--r-- | services/migrations/update.go | 4 | ||||
-rw-r--r-- | services/repository/collaboration.go | 47 | ||||
-rw-r--r-- | services/repository/collaboration_test.go | 28 |
5 files changed, 105 insertions, 9 deletions
diff --git a/services/externalaccount/user.go b/services/externalaccount/user.go index 87d2e02b48..3da5af3486 100644 --- a/services/externalaccount/user.go +++ b/services/externalaccount/user.go @@ -6,8 +6,9 @@ package externalaccount import ( "strings" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/auth" + issues_model "code.gitea.io/gitea/models/issues" + repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/structs" @@ -62,7 +63,7 @@ func LinkAccountToUser(user *user_model.User, gothUser goth.User) error { } if tp.Name() != "" { - return models.UpdateMigrationsByType(tp, externalID, user.ID) + return UpdateMigrationsByType(tp, externalID, user.ID) } return nil @@ -77,3 +78,23 @@ func UpdateExternalUser(user *user_model.User, gothUser goth.User) error { return user_model.UpdateExternalUserByExternalID(externalLoginUser) } + +// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID +func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, userID int64) error { + if err := issues_model.UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil { + return err + } + + if err := issues_model.UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil { + return err + } + + if err := repo_model.UpdateReleasesMigrationsByType(tp, externalUserID, userID); err != nil { + return err + } + + if err := issues_model.UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil { + return err + } + return issues_model.UpdateReviewsMigrationsByType(tp, externalUserID, userID) +} diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 3f055707ae..4c21efae44 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -205,7 +205,7 @@ func (g *GiteaLocalUploader) CreateMilestones(milestones ...*base.Milestone) err mss = append(mss, &ms) } - err := models.InsertMilestones(mss...) + err := issues_model.InsertMilestones(mss...) if err != nil { return err } @@ -350,7 +350,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { rels = append(rels, &rel) } - return models.InsertReleases(rels...) + return repo_model.InsertReleases(rels...) } // SyncTags syncs releases with tags in the database @@ -430,7 +430,7 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error { } if len(iss) > 0 { - if err := models.InsertIssues(iss...); err != nil { + if err := issues_model.InsertIssues(iss...); err != nil { return err } @@ -510,7 +510,7 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error { if len(cms) == 0 { return nil } - return models.InsertIssueComments(cms) + return issues_model.InsertIssueComments(cms) } // CreatePullRequests creates pull requests @@ -529,7 +529,7 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error gprs = append(gprs, gpr) } - if err := models.InsertPullRequests(ctx, gprs...); err != nil { + if err := issues_model.InsertPullRequests(ctx, gprs...); err != nil { return err } for _, pr := range gprs { diff --git a/services/migrations/update.go b/services/migrations/update.go index 48b61885e8..2adca01dfe 100644 --- a/services/migrations/update.go +++ b/services/migrations/update.go @@ -6,11 +6,11 @@ package migrations import ( "context" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/services/externalaccount" ) // UpdateMigrationPosterID updates all migrated repositories' issues and comments posterID @@ -62,7 +62,7 @@ func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServ default: } externalUserID := user.ExternalID - if err := models.UpdateMigrationsByType(tp, externalUserID, user.UserID); err != nil { + if err := externalaccount.UpdateMigrationsByType(tp, externalUserID, user.UserID); err != nil { log.Error("UpdateMigrationsByType type %s external user id %v to local user id %v failed: %v", tp.Name(), user.ExternalID, user.UserID, err) } } diff --git a/services/repository/collaboration.go b/services/repository/collaboration.go new file mode 100644 index 0000000000..28824d83f5 --- /dev/null +++ b/services/repository/collaboration.go @@ -0,0 +1,47 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Copyright 2020 The Gitea Authors. +// SPDX-License-Identifier: MIT + +package repository + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + access_model "code.gitea.io/gitea/models/perm/access" + repo_model "code.gitea.io/gitea/models/repo" +) + +// DeleteCollaboration removes collaboration relation between the user and repository. +func DeleteCollaboration(repo *repo_model.Repository, uid int64) (err error) { + collaboration := &repo_model.Collaboration{ + RepoID: repo.ID, + UserID: uid, + } + + ctx, committer, err := db.TxContext(db.DefaultContext) + if err != nil { + return err + } + defer committer.Close() + + if has, err := db.GetEngine(ctx).Delete(collaboration); err != nil || has == 0 { + return err + } else if err = access_model.RecalculateAccesses(ctx, repo); err != nil { + return err + } + + if err = repo_model.WatchRepo(ctx, uid, repo.ID, false); err != nil { + return err + } + + if err = models.ReconsiderWatches(ctx, repo, uid); err != nil { + return err + } + + // Unassign a user from any issue (s)he has been assigned to in the repository + if err := models.ReconsiderRepoIssuesAssignee(ctx, repo, uid); err != nil { + return err + } + + return committer.Commit() +} diff --git a/services/repository/collaboration_test.go b/services/repository/collaboration_test.go new file mode 100644 index 0000000000..08159af7bc --- /dev/null +++ b/services/repository/collaboration_test.go @@ -0,0 +1,28 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repository + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestRepository_DeleteCollaboration(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}) + assert.NoError(t, repo.LoadOwner(db.DefaultContext)) + assert.NoError(t, DeleteCollaboration(repo, 4)) + unittest.AssertNotExistsBean(t, &repo_model.Collaboration{RepoID: repo.ID, UserID: 4}) + + assert.NoError(t, DeleteCollaboration(repo, 4)) + unittest.AssertNotExistsBean(t, &repo_model.Collaboration{RepoID: repo.ID, UserID: 4}) + + unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repo.ID}) +} |