diff options
author | Lunny Xiao | 2023-02-15 21:37:34 +0800 |
---|---|---|
committer | GitHub | 2023-02-15 21:37:34 +0800 |
commit | bd820aa9c52da4568b460a0b8604287f8ed8df26 (patch) | |
tree | 15e59e1d4f705b1c5adbd418ed711dfd602a1b25 /services | |
parent | 03638f9725de3cda5207384098b38462f56d442e (diff) |
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')
25 files changed, 159 insertions, 153 deletions
diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 0ed69097d..cdf9087fe 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -52,7 +52,7 @@ func (n *actionsNotifier) NotifyNewIssue(ctx context.Context, issue *issues_mode Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(issue.Poster, nil), + Sender: convert.ToUser(ctx, issue.Poster, nil), }).Notify(withMethod(ctx, "NotifyNewIssue")) } @@ -70,7 +70,7 @@ func (n *actionsNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use Index: issue.Index, PullRequest: convert.ToAPIPullRequest(db.DefaultContext, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), CommitID: commitID, } if isClosed { @@ -88,7 +88,7 @@ func (n *actionsNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), } if isClosed { apiIssue.Action = api.HookIssueClosed @@ -134,7 +134,7 @@ func (n *actionsNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, perm_model.AccessModeNone), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). Notify(ctx) return @@ -146,7 +146,7 @@ func (n *actionsNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). Notify(ctx) } @@ -165,9 +165,9 @@ func (n *actionsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us WithPayload(&api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(ctx, issue), - Comment: convert.ToComment(comment), + Comment: convert.ToComment(ctx, comment), Repository: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), IsPull: true, }). Notify(ctx) @@ -178,9 +178,9 @@ func (n *actionsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us WithPayload(&api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(ctx, issue), - Comment: convert.ToComment(comment), + Comment: convert.ToComment(ctx, comment), Repository: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), IsPull: false, }). Notify(ctx) @@ -210,7 +210,7 @@ func (n *actionsNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pull, nil), Repository: convert.ToRepo(ctx, pull.Issue.Repo, mode), - Sender: convert.ToUser(pull.Issue.Poster, nil), + Sender: convert.ToUser(ctx, pull.Issue.Poster, nil), }). WithPullRequest(pull). Notify(ctx) @@ -222,8 +222,8 @@ func (n *actionsNotifier) NotifyCreateRepository(ctx context.Context, doer, u *u newNotifyInput(repo, doer, webhook_module.HookEventRepository).WithPayload(&api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm_model.AccessModeOwner), - Organization: convert.ToUser(u, nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, u, nil), + Sender: convert.ToUser(ctx, doer, nil), }).Notify(ctx) } @@ -237,7 +237,7 @@ func (n *actionsNotifier) NotifyForkRepository(ctx context.Context, doer *user_m newNotifyInput(oldRepo, doer, webhook_module.HookEventFork).WithPayload(&api.ForkPayload{ Forkee: convert.ToRepo(ctx, oldRepo, oldMode), Repo: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }).Notify(ctx) u := repo.MustOwner(ctx) @@ -249,8 +249,8 @@ func (n *actionsNotifier) NotifyForkRepository(ctx context.Context, doer *user_m WithPayload(&api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm_model.AccessModeOwner), - Organization: convert.ToUser(u, nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, u, nil), + Sender: convert.ToUser(ctx, doer, nil), }).Notify(ctx) } } @@ -291,7 +291,7 @@ func (n *actionsNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), Repository: convert.ToRepo(ctx, review.Issue.Repo, mode), - Sender: convert.ToUser(review.Reviewer, nil), + Sender: convert.ToUser(ctx, review.Reviewer, nil), Review: &api.ReviewPayload{ Type: string(reviewHookType), Content: review.Content, @@ -329,7 +329,7 @@ func (*actionsNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), Action: api.HookIssueClosed, } @@ -343,7 +343,7 @@ func (*actionsNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { ctx = withMethod(ctx, "NotifyPushCommits") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -369,7 +369,7 @@ func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { ctx = withMethod(ctx, "NotifyCreateRef") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm_model.AccessModeNone) refName := git.RefEndName(refFullName) @@ -388,7 +388,7 @@ func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { ctx = withMethod(ctx, "NotifyDeleteRef") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm_model.AccessModeNone) refName := git.RefEndName(refFullName) @@ -407,7 +407,7 @@ func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode func (n *actionsNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { ctx = withMethod(ctx, "NotifySyncPushCommits") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(db.DefaultContext, repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -490,7 +490,7 @@ func (n *actionsNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, perm_model.AccessModeNone), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). WithPullRequest(pr). Notify(ctx) @@ -521,7 +521,7 @@ func (n *actionsNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex }, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). WithPullRequest(pr). Notify(ctx) diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 5b8f6bfdf..df67d2fa1 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -205,9 +205,9 @@ func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.R WithRef(ref). WithPayload(&api.ReleasePayload{ Action: action, - Release: convert.ToRelease(rel), + Release: convert.ToRelease(ctx, rel), Repository: convert.ToRepo(ctx, rel.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). Notify(ctx) } @@ -230,7 +230,7 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo WithPayload(&api.PackagePayload{ Action: action, Package: apiPackage, - Sender: convert.ToUser(sender, nil), + Sender: convert.ToUser(ctx, sender, nil), }). Notify(ctx) } diff --git a/services/asymkey/sign.go b/services/asymkey/sign.go index 01718ebe7..252277e1b 100644 --- a/services/asymkey/sign.go +++ b/services/asymkey/sign.go @@ -207,7 +207,7 @@ Loop: if commit.Signature == nil { return false, "", nil, &ErrWontSign{parentSigned} } - verification := asymkey_model.ParseCommitWithSignature(commit) + verification := asymkey_model.ParseCommitWithSignature(ctx, commit) if !verification.Verified { return false, "", nil, &ErrWontSign{parentSigned} } @@ -260,7 +260,7 @@ Loop: if commit.Signature == nil { return false, "", nil, &ErrWontSign{parentSigned} } - verification := asymkey_model.ParseCommitWithSignature(commit) + verification := asymkey_model.ParseCommitWithSignature(ctx, commit) if !verification.Verified { return false, "", nil, &ErrWontSign{parentSigned} } @@ -332,7 +332,7 @@ Loop: if err != nil { return false, "", nil, err } - verification := asymkey_model.ParseCommitWithSignature(commit) + verification := asymkey_model.ParseCommitWithSignature(ctx, commit) if !verification.Verified { return false, "", nil, &ErrWontSign{baseSigned} } @@ -348,7 +348,7 @@ Loop: if err != nil { return false, "", nil, err } - verification := asymkey_model.ParseCommitWithSignature(commit) + verification := asymkey_model.ParseCommitWithSignature(ctx, commit) if !verification.Verified { return false, "", nil, &ErrWontSign{headSigned} } @@ -364,7 +364,7 @@ Loop: if err != nil { return false, "", nil, err } - verification := asymkey_model.ParseCommitWithSignature(commit) + verification := asymkey_model.ParseCommitWithSignature(ctx, commit) if !verification.Verified { return false, "", nil, &ErrWontSign{commitsSigned} } @@ -378,7 +378,7 @@ Loop: return false, "", nil, err } for _, commit := range commitList { - verification := asymkey_model.ParseCommitWithSignature(commit) + verification := asymkey_model.ParseCommitWithSignature(ctx, commit) if !verification.Verified { return false, "", nil, &ErrWontSign{commitsSigned} } diff --git a/services/auth/reverseproxy.go b/services/auth/reverseproxy.go index 0206ccdf6..91acff90c 100644 --- a/services/auth/reverseproxy.go +++ b/services/auth/reverseproxy.go @@ -91,7 +91,7 @@ func (r *ReverseProxy) getUserFromAuthEmail(req *http.Request) *user_model.User } log.Trace("ReverseProxy Authorization: Found email: %s", email) - user, err := user_model.GetUserByEmail(email) + user, err := user_model.GetUserByEmail(req.Context(), email) if err != nil { // Do not allow auto-registration, we don't have a username here if !user_model.IsErrUserNotExist(err) { diff --git a/services/convert/convert.go b/services/convert/convert.go index 17f7e3d65..16daad849 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 59842e402..20fb8c256 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 f3af03ed9..02f25e6e0 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 6044cbcf6..2810c6c9b 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 68ae6f4e6..7d170ccc2 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 cdf72e780..4989e82cd 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 66c5018ee..5d5d5d883 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 3afa53c03..ca28aa0d6 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 3ba604002..5db68e837 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 84ca1665d..b8c11ab63 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 6b90539fd..79fcba017 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 c3ab4187b..4b1effc7a 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) } diff --git a/services/pull/check.go b/services/pull/check.go index 481491c73..ea5cccbde 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -256,7 +256,7 @@ func manuallyMerged(ctx context.Context, pr *issues_model.PullRequest) bool { pr.MergedCommitID = commit.ID.String() pr.MergedUnix = timeutil.TimeStamp(commit.Author.When.Unix()) pr.Status = issues_model.PullRequestStatusManuallyMerged - merger, _ := user_model.GetUserByEmail(commit.Author.Email) + merger, _ := user_model.GetUserByEmail(ctx, commit.Author.Email) // When the commit author is unknown set the BaseRepo owner as merger if merger == nil { diff --git a/services/release/release.go b/services/release/release.go index 13042cd3a..eec03b468 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -102,7 +102,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel } if rel.PublisherID <= 0 { - u, err := user_model.GetUserByEmailContext(ctx, commit.Author.Email) + u, err := user_model.GetUserByEmail(ctx, commit.Author.Email) if err == nil { rel.PublisherID = u.ID } diff --git a/services/repository/files/cherry_pick.go b/services/repository/files/cherry_pick.go index 6bc67e263..c1c5bfb61 100644 --- a/services/repository/files/cherry_pick.go +++ b/services/repository/files/cherry_pick.go @@ -115,7 +115,7 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod } fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil - verification := GetPayloadCommitVerification(commit) + verification := GetPayloadCommitVerification(ctx, commit) fileResponse := &structs.FileResponse{ Commit: fileCommitResponse, Verification: verification, diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go index 9d237f1e2..3e4627487 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -66,9 +66,9 @@ func CountDivergingCommits(ctx context.Context, repo *repo_model.Repository, bra } // GetPayloadCommitVerification returns the verification information of a commit -func GetPayloadCommitVerification(commit *git.Commit) *structs.PayloadCommitVerification { +func GetPayloadCommitVerification(ctx context.Context, commit *git.Commit) *structs.PayloadCommitVerification { verification := &structs.PayloadCommitVerification{} - commitVerification := asymkey_model.ParseCommitWithSignature(commit) + commitVerification := asymkey_model.ParseCommitWithSignature(ctx, commit) if commit.Signature != nil { verification.Signature = commit.Signature.Signature verification.Payload = commit.Signature.Payload diff --git a/services/repository/files/file.go b/services/repository/files/file.go index ddd64a539..2bac4372d 100644 --- a/services/repository/files/file.go +++ b/services/repository/files/file.go @@ -21,7 +21,7 @@ import ( func GetFileResponseFromCommit(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) { fileContents, _ := GetContents(ctx, repo, treeName, branch, false) // ok if fails, then will be nil fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil - verification := GetPayloadCommitVerification(commit) + verification := GetPayloadCommitVerification(ctx, commit) fileResponse := &api.FileResponse{ Content: fileContents, Commit: fileCommitResponse, diff --git a/services/repository/files/patch.go b/services/repository/files/patch.go index f65199cfc..19d089b9e 100644 --- a/services/repository/files/patch.go +++ b/services/repository/files/patch.go @@ -183,7 +183,7 @@ func ApplyDiffPatch(ctx context.Context, repo *repo_model.Repository, doer *user } fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil - verification := GetPayloadCommitVerification(commit) + verification := GetPayloadCommitVerification(ctx, commit) fileResponse := &structs.FileResponse{ Commit: fileCommitResponse, Verification: verification, diff --git a/services/repository/push.go b/services/repository/push.go index ef6460cef..8aa8be6aa 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -355,7 +355,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo var ok bool author, ok = emailToUser[sig.Email] if !ok { - author, err = user_model.GetUserByEmailContext(ctx, sig.Email) + author, err = user_model.GetUserByEmail(ctx, sig.Email) if err != nil && !user_model.IsErrUserNotExist(err) { return fmt.Errorf("GetUserByEmail: %w", err) } diff --git a/services/user/user_test.go b/services/user/user_test.go index 5e052a9df..a25804fce 100644 --- a/services/user/user_test.go +++ b/services/user/user_test.go @@ -115,7 +115,7 @@ func TestCreateUser_Issue5882(t *testing.T) { assert.NoError(t, user_model.CreateUser(v.user)) - u, err := user_model.GetUserByEmail(v.user.Email) + u, err := user_model.GetUserByEmail(db.DefaultContext, v.user.Email) assert.NoError(t, err) assert.Equal(t, !u.AllowCreateOrganization, v.disableOrgCreation) diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index 16d2b9581..ba6d968db 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -63,7 +63,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } else { err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ @@ -71,7 +71,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } if err != nil { @@ -87,7 +87,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m if err := PrepareWebhooks(ctx, EventSource{Repository: oldRepo}, webhook_module.HookEventFork, &api.ForkPayload{ Forkee: convert.ToRepo(ctx, oldRepo, oldMode), Repo: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err) } @@ -99,8 +99,8 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), - Organization: convert.ToUser(u, nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, u, nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -112,8 +112,8 @@ func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *u if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), - Organization: convert.ToUser(u, nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, u, nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -123,8 +123,8 @@ func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoDeleted, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), - Organization: convert.ToUser(repo.MustOwner(ctx), nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, repo.MustOwner(ctx), nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -135,8 +135,8 @@ func (m *webhookNotifier) NotifyMigrateRepository(ctx context.Context, doer, u * if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), - Organization: convert.ToUser(u, nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, u, nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -155,7 +155,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), } if removed { apiPullRequest.Action = api.HookIssueUnassigned @@ -173,7 +173,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), } if removed { apiIssue.Action = api.HookIssueUnassigned @@ -207,7 +207,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user }, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } else { err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ @@ -220,7 +220,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user }, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } @@ -242,7 +242,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), CommitID: commitID, } if isClosed { @@ -256,7 +256,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), CommitID: commitID, } if isClosed { @@ -287,7 +287,7 @@ func (m *webhookNotifier) NotifyNewIssue(ctx context.Context, issue *issues_mode Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(issue.Poster, nil), + Sender: convert.ToUser(ctx, issue.Poster, nil), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -313,7 +313,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pull, nil), Repository: convert.ToRepo(ctx, pull.Issue.Repo, mode), - Sender: convert.ToUser(pull.Issue.Poster, nil), + Sender: convert.ToUser(ctx, pull.Issue.Poster, nil), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -339,7 +339,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us }, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } else { err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{ @@ -352,7 +352,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us }, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } if err != nil { @@ -386,14 +386,14 @@ func (m *webhookNotifier) NotifyUpdateComment(ctx context.Context, doer *user_mo if err := PrepareWebhooks(ctx, EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentEdited, Issue: convert.ToAPIIssue(ctx, c.Issue), - Comment: convert.ToComment(c), + Comment: convert.ToComment(ctx, c), Changes: &api.ChangesPayload{ Body: &api.ChangesFromPayload{ From: oldContent, }, }, Repository: convert.ToRepo(ctx, c.Issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), IsPull: c.Issue.IsPull, }); err != nil { log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err) @@ -414,9 +414,9 @@ func (m *webhookNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(ctx, issue), - Comment: convert.ToComment(comment), + Comment: convert.ToComment(ctx, comment), Repository: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), IsPull: issue.IsPull, }); err != nil { log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) @@ -451,9 +451,9 @@ func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_mo if err := PrepareWebhooks(ctx, EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{ Action: api.HookIssueCommentDeleted, Issue: convert.ToAPIIssue(ctx, comment.Issue), - Comment: convert.ToComment(comment), + Comment: convert.ToComment(ctx, comment), Repository: convert.ToRepo(ctx, comment.Issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), IsPull: comment.Issue.IsPull, }); err != nil { log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) @@ -465,7 +465,7 @@ func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_mode if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiCreated, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), Page: page, Comment: comment, }); err != nil { @@ -478,7 +478,7 @@ func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_mod if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiEdited, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), Page: page, Comment: comment, }); err != nil { @@ -491,7 +491,7 @@ func (m *webhookNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_m if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{ Action: api.HookWikiDeleted, Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), Page: page, }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) @@ -528,7 +528,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, perm.AccessModeNone), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } else { err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{ @@ -536,7 +536,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } if err != nil { @@ -570,7 +570,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer * Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } else { err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueMilestone, &api.IssuePayload{ @@ -578,7 +578,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer * Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }) } if err != nil { @@ -587,7 +587,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer * } func (m *webhookNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -643,7 +643,7 @@ func (*webhookNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), Action: api.HookIssueClosed, } @@ -671,7 +671,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex }, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks [pr: %d]: %v", pr.ID, err) } @@ -708,7 +708,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, review.Issue.Repo, mode), - Sender: convert.ToUser(review.Reviewer, nil), + Sender: convert.ToUser(ctx, review.Reviewer, nil), Review: &api.ReviewPayload{ Type: string(reviewHookType), Content: review.Content, @@ -719,7 +719,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue } func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) refName := git.RefEndName(refFullName) @@ -749,14 +749,14 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, perm.AccessModeNone), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err) } } func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) refName := git.RefEndName(refFullName) @@ -780,9 +780,9 @@ func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model mode, _ := access_model.AccessLevel(ctx, doer, rel.Repo) if err := PrepareWebhooks(ctx, EventSource{Repository: rel.Repo}, webhook_module.HookEventRelease, &api.ReleasePayload{ Action: action, - Release: convert.ToRelease(rel), + Release: convert.ToRelease(ctx, rel), Repository: convert.ToRepo(ctx, rel.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -801,7 +801,7 @@ func (m *webhookNotifier) NotifyDeleteRelease(ctx context.Context, doer *user_mo } func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -855,7 +855,7 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo if err := PrepareWebhooks(ctx, source, webhook_module.HookEventPackage, &api.PackagePayload{ Action: action, Package: apiPackage, - Sender: convert.ToUser(sender, nil), + Sender: convert.ToUser(ctx, sender, nil), }); err != nil { log.Error("PrepareWebhooks: %v", err) } |