summaryrefslogtreecommitdiffstats
path: root/services/convert
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-02-15 21:37:34 +0800
committerGitHub <noreply@github.com>2023-02-15 21:37:34 +0800
commitbd820aa9c52da4568b460a0b8604287f8ed8df26 (patch)
tree15e59e1d4f705b1c5adbd418ed711dfd602a1b25 /services/convert
parent03638f9725de3cda5207384098b38462f56d442e (diff)
downloadgitea-bd820aa9c52da4568b460a0b8604287f8ed8df26.tar.gz
gitea-bd820aa9c52da4568b460a0b8604287f8ed8df26.zip
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set a context cache to do that. i.e. Some pages may load a user from a database with the same id in different areas on the same page. But the code is hidden in two different deep logic. How should we share the user? As a result of this PR, now if both entry functions accept `context.Context` as the first parameter and we just need to refactor `GetUserByID` to reuse the user from the context cache. Then it will not be loaded twice on an HTTP request. But of course, sometimes we would like to reload an object from the database, that's why `RemoveContextData` is also exposed. The core context cache is here. It defines a new context ```go type cacheContext struct { ctx context.Context data map[any]map[any]any lock sync.RWMutex } var cacheContextKey = struct{}{} func WithCacheContext(ctx context.Context) context.Context { return context.WithValue(ctx, cacheContextKey, &cacheContext{ ctx: ctx, data: make(map[any]map[any]any), }) } ``` Then you can use the below 4 methods to read/write/del the data within the same context. ```go func GetContextData(ctx context.Context, tp, key any) any func SetContextData(ctx context.Context, tp, key, value any) func RemoveContextData(ctx context.Context, tp, key any) func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error) ``` Then let's take a look at how `system.GetString` implement it. ```go func GetSetting(ctx context.Context, key string) (string, error) { return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) { return cache.GetString(genSettingCacheKey(key), func() (string, error) { res, err := GetSettingNoCache(ctx, key) if err != nil { return "", err } return res.SettingValue, nil }) }) } ``` First, it will check if context data include the setting object with the key. If not, it will query from the global cache which may be memory or a Redis cache. If not, it will get the object from the database. In the end, if the object gets from the global cache or database, it will be set into the context cache. An object stored in the context cache will only be destroyed after the context disappeared.
Diffstat (limited to 'services/convert')
-rw-r--r--services/convert/convert.go26
-rw-r--r--services/convert/git_commit.go25
-rw-r--r--services/convert/issue.go6
-rw-r--r--services/convert/issue_comment.go14
-rw-r--r--services/convert/package.go4
-rw-r--r--services/convert/pull.go2
-rw-r--r--services/convert/pull_review.go8
-rw-r--r--services/convert/release.go6
-rw-r--r--services/convert/repository.go14
-rw-r--r--services/convert/status.go2
-rw-r--r--services/convert/user.go22
-rw-r--r--services/convert/user_test.go9
12 files changed, 72 insertions, 66 deletions
diff --git a/services/convert/convert.go b/services/convert/convert.go
index 17f7e3d650..16daad849b 100644
--- a/services/convert/convert.go
+++ b/services/convert/convert.go
@@ -39,7 +39,7 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
}
// ToBranch convert a git.Commit and git.Branch to an api.Branch
-func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
+func ToBranch(ctx context.Context, repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
if bp == nil {
var hasPerm bool
var canPush bool
@@ -59,7 +59,7 @@ func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git
return &api.Branch{
Name: b.Name,
- Commit: ToPayloadCommit(repo, c),
+ Commit: ToPayloadCommit(ctx, repo, c),
Protected: false,
RequiredApprovals: 0,
EnableStatusCheck: false,
@@ -71,7 +71,7 @@ func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git
branch := &api.Branch{
Name: b.Name,
- Commit: ToPayloadCommit(repo, c),
+ Commit: ToPayloadCommit(ctx, repo, c),
Protected: true,
RequiredApprovals: bp.RequiredApprovals,
EnableStatusCheck: bp.EnableStatusCheck,
@@ -169,8 +169,8 @@ func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag {
}
// ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
-func ToVerification(c *git.Commit) *api.PayloadCommitVerification {
- verif := asymkey_model.ParseCommitWithSignature(c)
+func ToVerification(ctx context.Context, c *git.Commit) *api.PayloadCommitVerification {
+ verif := asymkey_model.ParseCommitWithSignature(ctx, c)
commitVerification := &api.PayloadCommitVerification{
Verified: verif.Verified,
Reason: verif.Reason,
@@ -271,10 +271,10 @@ func ToDeployKey(apiLink string, key *asymkey_model.DeployKey) *api.DeployKey {
}
// ToOrganization convert user_model.User to api.Organization
-func ToOrganization(org *organization.Organization) *api.Organization {
+func ToOrganization(ctx context.Context, org *organization.Organization) *api.Organization {
return &api.Organization{
ID: org.ID,
- AvatarURL: org.AsUser().AvatarLink(),
+ AvatarURL: org.AsUser().AvatarLink(ctx),
Name: org.Name,
UserName: org.Name,
FullName: org.FullName,
@@ -287,8 +287,8 @@ func ToOrganization(org *organization.Organization) *api.Organization {
}
// ToTeam convert models.Team to api.Team
-func ToTeam(team *organization.Team, loadOrg ...bool) (*api.Team, error) {
- teams, err := ToTeams([]*organization.Team{team}, len(loadOrg) != 0 && loadOrg[0])
+func ToTeam(ctx context.Context, team *organization.Team, loadOrg ...bool) (*api.Team, error) {
+ teams, err := ToTeams(ctx, []*organization.Team{team}, len(loadOrg) != 0 && loadOrg[0])
if err != nil || len(teams) == 0 {
return nil, err
}
@@ -296,7 +296,7 @@ func ToTeam(team *organization.Team, loadOrg ...bool) (*api.Team, error) {
}
// ToTeams convert models.Team list to api.Team list
-func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
+func ToTeams(ctx context.Context, teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
if len(teams) == 0 || teams[0] == nil {
return nil, nil
}
@@ -326,7 +326,7 @@ func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
if err != nil {
return nil, err
}
- apiOrg = ToOrganization(org)
+ apiOrg = ToOrganization(ctx, org)
cache[teams[i].OrgID] = apiOrg
}
apiTeams[i].Organization = apiOrg
@@ -336,7 +336,7 @@ func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
}
// ToAnnotatedTag convert git.Tag to api.AnnotatedTag
-func ToAnnotatedTag(repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
+func ToAnnotatedTag(ctx context.Context, repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
return &api.AnnotatedTag{
Tag: t.Name,
SHA: t.ID.String(),
@@ -344,7 +344,7 @@ func ToAnnotatedTag(repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api
Message: t.Message,
URL: util.URLJoin(repo.APIURL(), "git/tags", t.ID.String()),
Tagger: ToCommitUser(t.Tagger),
- Verification: ToVerification(c),
+ Verification: ToVerification(ctx, c),
}
}
diff --git a/services/convert/git_commit.go b/services/convert/git_commit.go
index 59842e4020..20fb8c2565 100644
--- a/services/convert/git_commit.go
+++ b/services/convert/git_commit.go
@@ -4,6 +4,7 @@
package convert
import (
+ "context"
"net/url"
"time"
@@ -37,16 +38,16 @@ func ToCommitMeta(repo *repo_model.Repository, tag *git.Tag) *api.CommitMeta {
}
// ToPayloadCommit convert a git.Commit to api.PayloadCommit
-func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCommit {
+func ToPayloadCommit(ctx context.Context, repo *repo_model.Repository, c *git.Commit) *api.PayloadCommit {
authorUsername := ""
- if author, err := user_model.GetUserByEmail(c.Author.Email); err == nil {
+ if author, err := user_model.GetUserByEmail(ctx, c.Author.Email); err == nil {
authorUsername = author.Name
} else if !user_model.IsErrUserNotExist(err) {
log.Error("GetUserByEmail: %v", err)
}
committerUsername := ""
- if committer, err := user_model.GetUserByEmail(c.Committer.Email); err == nil {
+ if committer, err := user_model.GetUserByEmail(ctx, c.Committer.Email); err == nil {
committerUsername = committer.Name
} else if !user_model.IsErrUserNotExist(err) {
log.Error("GetUserByEmail: %v", err)
@@ -67,12 +68,12 @@ func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCom
UserName: committerUsername,
},
Timestamp: c.Author.When,
- Verification: ToVerification(c),
+ Verification: ToVerification(ctx, c),
}
}
// ToCommit convert a git.Commit to api.Commit
-func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*api.Commit, error) {
+func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*api.Commit, error) {
var apiAuthor, apiCommitter *api.User
// Retrieve author and committer information
@@ -87,13 +88,13 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.
}
if ok {
- apiAuthor = ToUser(cacheAuthor, nil)
+ apiAuthor = ToUser(ctx, cacheAuthor, nil)
} else {
- author, err := user_model.GetUserByEmail(commit.Author.Email)
+ author, err := user_model.GetUserByEmail(ctx, commit.Author.Email)
if err != nil && !user_model.IsErrUserNotExist(err) {
return nil, err
} else if err == nil {
- apiAuthor = ToUser(author, nil)
+ apiAuthor = ToUser(ctx, author, nil)
if userCache != nil {
userCache[commit.Author.Email] = author
}
@@ -109,13 +110,13 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.
}
if ok {
- apiCommitter = ToUser(cacheCommitter, nil)
+ apiCommitter = ToUser(ctx, cacheCommitter, nil)
} else {
- committer, err := user_model.GetUserByEmail(commit.Committer.Email)
+ committer, err := user_model.GetUserByEmail(ctx, commit.Committer.Email)
if err != nil && !user_model.IsErrUserNotExist(err) {
return nil, err
} else if err == nil {
- apiCommitter = ToUser(committer, nil)
+ apiCommitter = ToUser(ctx, committer, nil)
if userCache != nil {
userCache[commit.Committer.Email] = committer
}
@@ -161,7 +162,7 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.
SHA: commit.ID.String(),
Created: commit.Committer.When,
},
- Verification: ToVerification(commit),
+ Verification: ToVerification(ctx, commit),
},
Author: apiAuthor,
Committer: apiCommitter,
diff --git a/services/convert/issue.go b/services/convert/issue.go
index f3af03ed94..02f25e6e0e 100644
--- a/services/convert/issue.go
+++ b/services/convert/issue.go
@@ -41,7 +41,7 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
URL: issue.APIURL(),
HTMLURL: issue.HTMLURL(),
Index: issue.Index,
- Poster: ToUser(issue.Poster, nil),
+ Poster: ToUser(ctx, issue.Poster, nil),
Title: issue.Title,
Body: issue.Content,
Attachments: ToAttachments(issue.Attachments),
@@ -77,9 +77,9 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
}
if len(issue.Assignees) > 0 {
for _, assignee := range issue.Assignees {
- apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, nil))
+ apiIssue.Assignees = append(apiIssue.Assignees, ToUser(ctx, assignee, nil))
}
- apiIssue.Assignee = ToUser(issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
+ apiIssue.Assignee = ToUser(ctx, issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
}
if issue.IsPull {
if err := issue.LoadPullRequest(ctx); err != nil {
diff --git a/services/convert/issue_comment.go b/services/convert/issue_comment.go
index 6044cbcf61..2810c6c9bc 100644
--- a/services/convert/issue_comment.go
+++ b/services/convert/issue_comment.go
@@ -14,10 +14,10 @@ import (
)
// ToComment converts a issues_model.Comment to the api.Comment format
-func ToComment(c *issues_model.Comment) *api.Comment {
+func ToComment(ctx context.Context, c *issues_model.Comment) *api.Comment {
return &api.Comment{
ID: c.ID,
- Poster: ToUser(c.Poster, nil),
+ Poster: ToUser(ctx, c.Poster, nil),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
@@ -69,7 +69,7 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
comment := &api.TimelineComment{
ID: c.ID,
Type: c.Type.String(),
- Poster: ToUser(c.Poster, nil),
+ Poster: ToUser(ctx, c.Poster, nil),
HTMLURL: c.HTMLURL(),
IssueURL: c.IssueURL(),
PRURL: c.PRURL(),
@@ -131,7 +131,7 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
log.Error("LoadPoster: %v", err)
return nil
}
- comment.RefComment = ToComment(com)
+ comment.RefComment = ToComment(ctx, com)
}
if c.Label != nil {
@@ -157,14 +157,14 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
}
if c.Assignee != nil {
- comment.Assignee = ToUser(c.Assignee, nil)
+ comment.Assignee = ToUser(ctx, c.Assignee, nil)
}
if c.AssigneeTeam != nil {
- comment.AssigneeTeam, _ = ToTeam(c.AssigneeTeam)
+ comment.AssigneeTeam, _ = ToTeam(ctx, c.AssigneeTeam)
}
if c.ResolveDoer != nil {
- comment.ResolveDoer = ToUser(c.ResolveDoer, nil)
+ comment.ResolveDoer = ToUser(ctx, c.ResolveDoer, nil)
}
if c.DependentIssue != nil {
diff --git a/services/convert/package.go b/services/convert/package.go
index 68ae6f4e62..7d170ccc25 100644
--- a/services/convert/package.go
+++ b/services/convert/package.go
@@ -28,9 +28,9 @@ func ToPackage(ctx context.Context, pd *packages.PackageDescriptor, doer *user_m
return &api.Package{
ID: pd.Version.ID,
- Owner: ToUser(pd.Owner, doer),
+ Owner: ToUser(ctx, pd.Owner, doer),
Repository: repo,
- Creator: ToUser(pd.Creator, doer),
+ Creator: ToUser(ctx, pd.Creator, doer),
Type: string(pd.Package.Type),
Name: pd.Package.Name,
Version: pd.Version.Version,
diff --git a/services/convert/pull.go b/services/convert/pull.go
index cdf72e7805..4989e82cd4 100644
--- a/services/convert/pull.go
+++ b/services/convert/pull.go
@@ -201,7 +201,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
if pr.HasMerged {
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
apiPullRequest.MergedCommitID = &pr.MergedCommitID
- apiPullRequest.MergedBy = ToUser(pr.Merger, nil)
+ apiPullRequest.MergedBy = ToUser(ctx, pr.Merger, nil)
}
return apiPullRequest
diff --git a/services/convert/pull_review.go b/services/convert/pull_review.go
index 66c5018ee2..5d5d5d883c 100644
--- a/services/convert/pull_review.go
+++ b/services/convert/pull_review.go
@@ -21,14 +21,14 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.
r.Reviewer = user_model.NewGhostUser()
}
- apiTeam, err := ToTeam(r.ReviewerTeam)
+ apiTeam, err := ToTeam(ctx, r.ReviewerTeam)
if err != nil {
return nil, err
}
result := &api.PullReview{
ID: r.ID,
- Reviewer: ToUser(r.Reviewer, doer),
+ Reviewer: ToUser(ctx, r.Reviewer, doer),
ReviewerTeam: apiTeam,
State: api.ReviewStateUnknown,
Body: r.Content,
@@ -93,8 +93,8 @@ func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, d
apiComment := &api.PullReviewComment{
ID: comment.ID,
Body: comment.Content,
- Poster: ToUser(comment.Poster, doer),
- Resolver: ToUser(comment.ResolveDoer, doer),
+ Poster: ToUser(ctx, comment.Poster, doer),
+ Resolver: ToUser(ctx, comment.ResolveDoer, doer),
ReviewID: review.ID,
Created: comment.CreatedUnix.AsTime(),
Updated: comment.UpdatedUnix.AsTime(),
diff --git a/services/convert/release.go b/services/convert/release.go
index 3afa53c03f..ca28aa0d6b 100644
--- a/services/convert/release.go
+++ b/services/convert/release.go
@@ -4,12 +4,14 @@
package convert
import (
+ "context"
+
repo_model "code.gitea.io/gitea/models/repo"
api "code.gitea.io/gitea/modules/structs"
)
// ToRelease convert a repo_model.Release to api.Release
-func ToRelease(r *repo_model.Release) *api.Release {
+func ToRelease(ctx context.Context, r *repo_model.Release) *api.Release {
return &api.Release{
ID: r.ID,
TagName: r.TagName,
@@ -24,7 +26,7 @@ func ToRelease(r *repo_model.Release) *api.Release {
IsPrerelease: r.IsPrerelease,
CreatedAt: r.CreatedUnix.AsTime(),
PublishedAt: r.CreatedUnix.AsTime(),
- Publisher: ToUser(r.Publisher, nil),
+ Publisher: ToUser(ctx, r.Publisher, nil),
Attachments: ToAttachments(r.Attachments),
}
}
diff --git a/services/convert/repository.go b/services/convert/repository.go
index 3ba604002e..5db68e8379 100644
--- a/services/convert/repository.go
+++ b/services/convert/repository.go
@@ -126,7 +126,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
if err := t.LoadAttributes(ctx); err != nil {
log.Warn("LoadAttributes of RepoTransfer: %v", err)
} else {
- transfer = ToRepoTransfer(t)
+ transfer = ToRepoTransfer(ctx, t)
}
}
}
@@ -140,7 +140,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
return &api.Repository{
ID: repo.ID,
- Owner: ToUserWithAccessMode(repo.Owner, mode),
+ Owner: ToUserWithAccessMode(ctx, repo.Owner, mode),
Name: repo.Name,
FullName: repo.FullName(),
Description: repo.Description,
@@ -185,7 +185,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
DefaultDeleteBranchAfterMerge: defaultDeleteBranchAfterMerge,
DefaultMergeStyle: string(defaultMergeStyle),
DefaultAllowMaintainerEdit: defaultAllowMaintainerEdit,
- AvatarURL: repo.AvatarLink(),
+ AvatarURL: repo.AvatarLink(ctx),
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
MirrorInterval: mirrorInterval,
MirrorUpdated: mirrorUpdated,
@@ -194,12 +194,12 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc
}
// ToRepoTransfer convert a models.RepoTransfer to a structs.RepeTransfer
-func ToRepoTransfer(t *models.RepoTransfer) *api.RepoTransfer {
- teams, _ := ToTeams(t.Teams, false)
+func ToRepoTransfer(ctx context.Context, t *models.RepoTransfer) *api.RepoTransfer {
+ teams, _ := ToTeams(ctx, t.Teams, false)
return &api.RepoTransfer{
- Doer: ToUser(t.Doer, nil),
- Recipient: ToUser(t.Recipient, nil),
+ Doer: ToUser(ctx, t.Doer, nil),
+ Recipient: ToUser(ctx, t.Recipient, nil),
Teams: teams,
}
}
diff --git a/services/convert/status.go b/services/convert/status.go
index 84ca1665d2..b8c11ab630 100644
--- a/services/convert/status.go
+++ b/services/convert/status.go
@@ -26,7 +26,7 @@ func ToCommitStatus(ctx context.Context, status *git_model.CommitStatus) *api.Co
if status.CreatorID != 0 {
creator, _ := user_model.GetUserByID(ctx, status.CreatorID)
- apiStatus.Creator = ToUser(creator, nil)
+ apiStatus.Creator = ToUser(ctx, creator, nil)
}
return apiStatus
diff --git a/services/convert/user.go b/services/convert/user.go
index 6b90539fd9..79fcba0176 100644
--- a/services/convert/user.go
+++ b/services/convert/user.go
@@ -4,6 +4,8 @@
package convert
import (
+ "context"
+
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
@@ -11,7 +13,7 @@ import (
// 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 *user_model.User) *api.User {
+func ToUser(ctx context.Context, user, doer *user_model.User) *api.User {
if user == nil {
return nil
}
@@ -21,36 +23,36 @@ func ToUser(user, doer *user_model.User) *api.User {
signed = true
authed = doer.ID == user.ID || doer.IsAdmin
}
- return toUser(user, signed, authed)
+ return toUser(ctx, user, signed, authed)
}
// ToUsers convert list of user_model.User to list of api.User
-func ToUsers(doer *user_model.User, users []*user_model.User) []*api.User {
+func ToUsers(ctx context.Context, 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)
+ result[i] = ToUser(ctx, users[i], doer)
}
return result
}
// ToUserWithAccessMode convert user_model.User to api.User
// AccessMode is not none show add some more information
-func ToUserWithAccessMode(user *user_model.User, accessMode perm.AccessMode) *api.User {
+func ToUserWithAccessMode(ctx context.Context, user *user_model.User, accessMode perm.AccessMode) *api.User {
if user == nil {
return nil
}
- return toUser(user, accessMode != perm.AccessModeNone, false)
+ return toUser(ctx, user, accessMode != perm.AccessModeNone, false)
}
// 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 *user_model.User, signed, authed bool) *api.User {
+func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *api.User {
result := &api.User{
ID: user.ID,
UserName: user.Name,
FullName: user.FullName,
Email: user.GetEmail(),
- AvatarURL: user.AvatarLink(),
+ AvatarURL: user.AvatarLink(ctx),
Created: user.CreatedUnix.AsTime(),
Restricted: user.IsRestricted,
Location: user.Location,
@@ -97,9 +99,9 @@ func User2UserSettings(user *user_model.User) api.UserSettings {
}
// ToUserAndPermission return User and its collaboration permission for a repository
-func ToUserAndPermission(user, doer *user_model.User, accessMode perm.AccessMode) api.RepoCollaboratorPermission {
+func ToUserAndPermission(ctx context.Context, user, doer *user_model.User, accessMode perm.AccessMode) api.RepoCollaboratorPermission {
return api.RepoCollaboratorPermission{
- User: ToUser(user, doer),
+ User: ToUser(ctx, user, doer),
Permission: accessMode.String(),
RoleName: accessMode.String(),
}
diff --git a/services/convert/user_test.go b/services/convert/user_test.go
index c3ab4187b7..4b1effc7aa 100644
--- a/services/convert/user_test.go
+++ b/services/convert/user_test.go
@@ -6,6 +6,7 @@ package convert
import (
"testing"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
@@ -18,22 +19,22 @@ func TestUser_ToUser(t *testing.T) {
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1, IsAdmin: true})
- apiUser := toUser(user1, true, true)
+ apiUser := toUser(db.DefaultContext, user1, true, true)
assert.True(t, apiUser.IsAdmin)
assert.Contains(t, apiUser.AvatarURL, "://")
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2, IsAdmin: false})
- apiUser = toUser(user2, true, true)
+ apiUser = toUser(db.DefaultContext, user2, true, true)
assert.False(t, apiUser.IsAdmin)
- apiUser = toUser(user1, false, false)
+ apiUser = toUser(db.DefaultContext, user1, false, false)
assert.False(t, apiUser.IsAdmin)
assert.EqualValues(t, api.VisibleTypePublic.String(), apiUser.Visibility)
user31 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate})
- apiUser = toUser(user31, true, true)
+ apiUser = toUser(db.DefaultContext, user31, true, true)
assert.False(t, apiUser.IsAdmin)
assert.EqualValues(t, api.VisibleTypePrivate.String(), apiUser.Visibility)
}