diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-24 17:49:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-24 17:49:20 +0800 |
commit | a666829a37be6f9fd98f9e7dd1767c420f7f3b32 (patch) | |
tree | 9ab1434b759a8a2cb275a83149903a823851e309 /modules/convert | |
parent | 4e7ca946da2a2642a62f114825129bf5d7ed9196 (diff) | |
download | gitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.tar.gz gitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.zip |
Move user related model into models/user (#17781)
* Move user related model into models/user
* Fix lint for windows
* Fix windows lint
* Fix windows lint
* Move some tests in models
* Merge
Diffstat (limited to 'modules/convert')
-rw-r--r-- | modules/convert/convert.go | 16 | ||||
-rw-r--r-- | modules/convert/git_commit.go | 27 | ||||
-rw-r--r-- | modules/convert/issue.go | 5 | ||||
-rw-r--r-- | modules/convert/pull.go | 3 | ||||
-rw-r--r-- | modules/convert/pull_review.go | 15 | ||||
-rw-r--r-- | modules/convert/status.go | 3 | ||||
-rw-r--r-- | modules/convert/user.go | 19 | ||||
-rw-r--r-- | modules/convert/user_test.go | 8 |
8 files changed, 53 insertions, 43 deletions
diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 4229e6cc7d..f08a62588e 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -33,7 +33,7 @@ func ToEmail(email *user_model.EmailAddress) *api.Email { } // ToBranch convert a git.Commit and git.Branch to an api.Branch -func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *models.User, isRepoAdmin bool) (*api.Branch, error) { +func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) { if bp == nil { var hasPerm bool var err error @@ -83,15 +83,15 @@ func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models. // ToBranchProtection convert a ProtectedBranch to api.BranchProtection func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection { - pushWhitelistUsernames, err := models.GetUserNamesByIDs(bp.WhitelistUserIDs) + pushWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.WhitelistUserIDs) if err != nil { log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err) } - mergeWhitelistUsernames, err := models.GetUserNamesByIDs(bp.MergeWhitelistUserIDs) + mergeWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.MergeWhitelistUserIDs) if err != nil { log.Error("GetUserNamesByIDs (MergeWhitelistUserIDs): %v", err) } - approvalsWhitelistUsernames, err := models.GetUserNamesByIDs(bp.ApprovalsWhitelistUserIDs) + approvalsWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.ApprovalsWhitelistUserIDs) if err != nil { log.Error("GetUserNamesByIDs (ApprovalsWhitelistUserIDs): %v", err) } @@ -276,7 +276,7 @@ func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey { } } -// ToOrganization convert models.User to api.Organization +// ToOrganization convert user_model.User to api.Organization func ToOrganization(org *models.Organization) *api.Organization { return &api.Organization{ ID: org.ID, @@ -355,12 +355,16 @@ func ToOAuth2Application(app *login.OAuth2Application) *api.OAuth2Application { // ToLFSLock convert a LFSLock to api.LFSLock func ToLFSLock(l *models.LFSLock) *api.LFSLock { + u, err := user_model.GetUserByID(l.OwnerID) + if err != nil { + return nil + } return &api.LFSLock{ ID: strconv.FormatInt(l.ID, 10), Path: l.Path, LockedAt: l.Created.Round(time.Second), Owner: &api.LFSLockOwner{ - Name: l.Owner.DisplayName(), + Name: u.DisplayName(), }, } } diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index 9905b51fe4..3148d880ff 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -9,6 +9,7 @@ import ( "time" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" @@ -38,16 +39,16 @@ func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta { // ToPayloadCommit convert a git.Commit to api.PayloadCommit func ToPayloadCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit { authorUsername := "" - if author, err := models.GetUserByEmail(c.Author.Email); err == nil { + if author, err := user_model.GetUserByEmail(c.Author.Email); err == nil { authorUsername = author.Name - } else if !models.IsErrUserNotExist(err) { + } else if !user_model.IsErrUserNotExist(err) { log.Error("GetUserByEmail: %v", err) } committerUsername := "" - if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil { + if committer, err := user_model.GetUserByEmail(c.Committer.Email); err == nil { committerUsername = committer.Name - } else if !models.IsErrUserNotExist(err) { + } else if !user_model.IsErrUserNotExist(err) { log.Error("GetUserByEmail: %v", err) } @@ -71,16 +72,16 @@ func ToPayloadCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit } // ToCommit convert a git.Commit to api.Commit -func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]*models.User) (*api.Commit, error) { +func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) { var apiAuthor, apiCommitter *api.User // Retrieve author and committer information - var cacheAuthor *models.User + var cacheAuthor *user_model.User var ok bool if userCache == nil { - cacheAuthor = (*models.User)(nil) + cacheAuthor = (*user_model.User)(nil) ok = false } else { cacheAuthor, ok = userCache[commit.Author.Email] @@ -89,8 +90,8 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] if ok { apiAuthor = ToUser(cacheAuthor, nil) } else { - author, err := models.GetUserByEmail(commit.Author.Email) - if err != nil && !models.IsErrUserNotExist(err) { + author, err := user_model.GetUserByEmail(commit.Author.Email) + if err != nil && !user_model.IsErrUserNotExist(err) { return nil, err } else if err == nil { apiAuthor = ToUser(author, nil) @@ -100,9 +101,9 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] } } - var cacheCommitter *models.User + var cacheCommitter *user_model.User if userCache == nil { - cacheCommitter = (*models.User)(nil) + cacheCommitter = (*user_model.User)(nil) ok = false } else { cacheCommitter, ok = userCache[commit.Committer.Email] @@ -111,8 +112,8 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] if ok { apiCommitter = ToUser(cacheCommitter, nil) } else { - committer, err := models.GetUserByEmail(commit.Committer.Email) - if err != nil && !models.IsErrUserNotExist(err) { + committer, err := user_model.GetUserByEmail(commit.Committer.Email) + if err != nil && !user_model.IsErrUserNotExist(err) { return nil, err } else if err == nil { apiCommitter = ToUser(committer, nil) diff --git a/modules/convert/issue.go b/modules/convert/issue.go index 7363cfb8fb..c67ff5427f 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -10,6 +10,7 @@ import ( "strings" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -175,7 +176,7 @@ func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList { } // ToLabel converts Label to API format -func ToLabel(label *models.Label, repo *models.Repository, org *models.User) *api.Label { +func ToLabel(label *models.Label, repo *models.Repository, org *user_model.User) *api.Label { result := &api.Label{ ID: label.ID, Name: label.Name, @@ -202,7 +203,7 @@ func ToLabel(label *models.Label, repo *models.Repository, org *models.User) *ap } // ToLabelList converts list of Label to API format -func ToLabelList(labels []*models.Label, repo *models.Repository, org *models.User) []*api.Label { +func ToLabelList(labels []*models.Label, repo *models.Repository, org *user_model.User) []*api.Label { result := make([]*api.Label, len(labels)) for i := range labels { result[i] = ToLabel(labels[i], repo, org) diff --git a/modules/convert/pull.go b/modules/convert/pull.go index 0f2b5af1b4..f892107f4b 100644 --- a/modules/convert/pull.go +++ b/modules/convert/pull.go @@ -8,6 +8,7 @@ import ( "fmt" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" @@ -16,7 +17,7 @@ import ( // ToAPIPullRequest assumes following fields have been assigned with valid values: // Required - Issue // Optional - Merger -func ToAPIPullRequest(pr *models.PullRequest, doer *models.User) *api.PullRequest { +func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRequest { var ( baseBranch *git.Branch headBranch *git.Branch diff --git a/modules/convert/pull_review.go b/modules/convert/pull_review.go index b461b27b5a..b99d7bead6 100644 --- a/modules/convert/pull_review.go +++ b/modules/convert/pull_review.go @@ -8,16 +8,17 @@ import ( "strings" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" ) // ToPullReview convert a review to api format -func ToPullReview(r *models.Review, doer *models.User) (*api.PullReview, error) { +func ToPullReview(r *models.Review, doer *user_model.User) (*api.PullReview, error) { if err := r.LoadAttributes(); err != nil { - if !models.IsErrUserNotExist(err) { + if !user_model.IsErrUserNotExist(err) { return nil, err } - r.Reviewer = models.NewGhostUser() + r.Reviewer = user_model.NewGhostUser() } result := &api.PullReview{ @@ -53,7 +54,7 @@ func ToPullReview(r *models.Review, doer *models.User) (*api.PullReview, error) } // ToPullReviewList convert a list of review to it's api format -func ToPullReviewList(rl []*models.Review, doer *models.User) ([]*api.PullReview, error) { +func ToPullReviewList(rl []*models.Review, doer *user_model.User) ([]*api.PullReview, error) { result := make([]*api.PullReview, 0, len(rl)) for i := range rl { // show pending reviews only for the user who created them @@ -70,12 +71,12 @@ func ToPullReviewList(rl []*models.Review, doer *models.User) ([]*api.PullReview } // ToPullReviewCommentList convert the CodeComments of an review to it's api format -func ToPullReviewCommentList(review *models.Review, doer *models.User) ([]*api.PullReviewComment, error) { +func ToPullReviewCommentList(review *models.Review, doer *user_model.User) ([]*api.PullReviewComment, error) { if err := review.LoadAttributes(); err != nil { - if !models.IsErrUserNotExist(err) { + if !user_model.IsErrUserNotExist(err) { return nil, err } - review.Reviewer = models.NewGhostUser() + review.Reviewer = user_model.NewGhostUser() } apiComments := make([]*api.PullReviewComment, 0, len(review.CodeComments)) diff --git a/modules/convert/status.go b/modules/convert/status.go index bb2f964fe4..eb77c14dab 100644 --- a/modules/convert/status.go +++ b/modules/convert/status.go @@ -6,6 +6,7 @@ package convert import ( "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" ) @@ -23,7 +24,7 @@ func ToCommitStatus(status *models.CommitStatus) *api.CommitStatus { } if status.CreatorID != 0 { - creator, _ := models.GetUserByID(status.CreatorID) + creator, _ := user_model.GetUserByID(status.CreatorID) apiStatus.Creator = ToUser(creator, nil) } diff --git a/modules/convert/user.go b/modules/convert/user.go index 3f17ae4b4d..ebe2f06460 100644 --- a/modules/convert/user.go +++ b/modules/convert/user.go @@ -6,12 +6,13 @@ package convert import ( "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" ) -// ToUser convert models.User to api.User +// ToUser convert user_model.User to api.User // if doer is set, private information is added if the doer has the permission to see it -func ToUser(user, doer *models.User) *api.User { +func ToUser(user, doer *user_model.User) *api.User { if user == nil { return nil } @@ -24,8 +25,8 @@ func ToUser(user, doer *models.User) *api.User { return toUser(user, signed, authed) } -// ToUsers convert list of models.User to list of api.User -func ToUsers(doer *models.User, users []*models.User) []*api.User { +// ToUsers convert list of user_model.User to list of api.User +func ToUsers(doer *user_model.User, users []*user_model.User) []*api.User { result := make([]*api.User, len(users)) for i := range users { result[i] = ToUser(users[i], doer) @@ -33,18 +34,18 @@ func ToUsers(doer *models.User, users []*models.User) []*api.User { return result } -// ToUserWithAccessMode convert models.User to api.User +// ToUserWithAccessMode convert user_model.User to api.User // AccessMode is not none show add some more information -func ToUserWithAccessMode(user *models.User, accessMode models.AccessMode) *api.User { +func ToUserWithAccessMode(user *user_model.User, accessMode models.AccessMode) *api.User { if user == nil { return nil } return toUser(user, accessMode != models.AccessModeNone, false) } -// toUser convert models.User to api.User +// toUser convert user_model.User to api.User // signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself -func toUser(user *models.User, signed, authed bool) *api.User { +func toUser(user *user_model.User, signed, authed bool) *api.User { result := &api.User{ ID: user.ID, UserName: user.Name, @@ -81,7 +82,7 @@ func toUser(user *models.User, signed, authed bool) *api.User { } // User2UserSettings return UserSettings based on a user -func User2UserSettings(user *models.User) api.UserSettings { +func User2UserSettings(user *user_model.User) api.UserSettings { return api.UserSettings{ FullName: user.FullName, Website: user.Website, diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go index ce174e3076..2ed962950f 100644 --- a/modules/convert/user_test.go +++ b/modules/convert/user_test.go @@ -7,8 +7,8 @@ package convert import ( "testing" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -17,13 +17,13 @@ import ( func TestUser_ToUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) + user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1, IsAdmin: true}).(*user_model.User) apiUser := toUser(user1, true, true) assert.True(t, apiUser.IsAdmin) assert.Contains(t, apiUser.AvatarURL, "://") - user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2, IsAdmin: false}).(*user_model.User) apiUser = toUser(user2, true, true) assert.False(t, apiUser.IsAdmin) @@ -32,7 +32,7 @@ func TestUser_ToUser(t *testing.T) { assert.False(t, apiUser.IsAdmin) assert.EqualValues(t, api.VisibleTypePublic.String(), apiUser.Visibility) - user31 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate}).(*models.User) + user31 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate}).(*user_model.User) apiUser = toUser(user31, true, true) assert.False(t, apiUser.IsAdmin) |