diff options
author | Anthony Wang | 2023-02-20 22:21:24 +0000 |
---|---|---|
committer | Anthony Wang | 2023-02-20 22:21:24 +0000 |
commit | dc20c2832871f6462990751ea802e14b02bf41b0 (patch) | |
tree | 71bfef0694e6c0f8284438c290521d7297f24eab /services | |
parent | 07df0a6b1c97be4b03d23d5dfa047a108de36592 (diff) | |
parent | ef11d41639dd1e89676e395068ee453312560adb (diff) |
Merge remote-tracking branch 'origin/main' into forgejo-federation
Diffstat (limited to 'services')
38 files changed, 386 insertions, 178 deletions
diff --git a/services/actions/commit_status.go b/services/actions/commit_status.go index c17f8ef15..efb5ec6d4 100644 --- a/services/actions/commit_status.go +++ b/services/actions/commit_status.go @@ -59,7 +59,7 @@ func CreateCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er Creator: creator, CommitStatus: &git_model.CommitStatus{ SHA: sha, - TargetURL: run.HTMLURL(), + TargetURL: run.Link(), Description: "", Context: ctxname, CreatorID: payload.Pusher.ID, 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..5f2100a03 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 } @@ -304,7 +304,7 @@ func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) { cache := make(map[int64]*api.Organization) apiTeams := make([]*api.Team, len(teams)) for i := range teams { - if err := teams[i].GetUnits(); err != nil { + if err := teams[i].LoadUnits(ctx); err != nil { return nil, err } @@ -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..e79fcfccc 100644 --- a/services/convert/issue.go +++ b/services/convert/issue.go @@ -32,7 +32,7 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue { if err := issue.LoadRepo(ctx); err != nil { return &api.Issue{} } - if err := issue.Repo.GetOwner(ctx); err != nil { + if err := issue.Repo.LoadOwner(ctx); err != nil { return &api.Issue{} } @@ -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 { @@ -182,6 +182,7 @@ func ToLabel(label *issues_model.Label, repo *repo_model.Repository, org *user_m result := &api.Label{ ID: label.ID, Name: label.Name, + Exclusive: label.Exclusive, Color: strings.TrimLeft(label.Color, "#"), Description: label.Description, } 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 ce53a6669..fc965a945 100644 --- a/services/convert/repository.go +++ b/services/convert/repository.go @@ -81,6 +81,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc allowRebaseUpdate := false defaultDeleteBranchAfterMerge := false defaultMergeStyle := repo_model.MergeStyleMerge + defaultAllowMaintainerEdit := false if unit, err := repo.GetUnit(ctx, unit_model.TypePullRequests); err == nil { config := unit.PullRequestsConfig() hasPullRequests = true @@ -92,13 +93,14 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc allowRebaseUpdate = config.AllowRebaseUpdate defaultDeleteBranchAfterMerge = config.DefaultDeleteBranchAfterMerge defaultMergeStyle = config.GetDefaultMergeStyle() + defaultAllowMaintainerEdit = config.DefaultAllowMaintainerEdit } hasProjects := false if _, err := repo.GetUnit(ctx, unit_model.TypeProjects); err == nil { hasProjects = true } - if err := repo.GetOwner(ctx); err != nil { + if err := repo.LoadOwner(ctx); err != nil { return nil } @@ -124,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) } } } @@ -138,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, @@ -182,7 +184,8 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc AllowRebaseUpdate: allowRebaseUpdate, DefaultDeleteBranchAfterMerge: defaultDeleteBranchAfterMerge, DefaultMergeStyle: string(defaultMergeStyle), - AvatarURL: repo.AvatarLink(), + DefaultAllowMaintainerEdit: defaultAllowMaintainerEdit, + AvatarURL: repo.AvatarLink(ctx), Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, MirrorInterval: mirrorInterval, MirrorUpdated: mirrorUpdated, @@ -191,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/forms/repo_form.go b/services/forms/repo_form.go index 436d79df6..ff0916f8e 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -160,6 +160,7 @@ type RepoSettingForm struct { EnableAutodetectManualMerge bool PullsAllowRebaseUpdate bool DefaultDeleteBranchAfterMerge bool + DefaultAllowMaintainerEdit bool EnableTimetracker bool AllowOnlyContributorsToTrackTime bool EnableIssueDependencies bool @@ -512,6 +513,7 @@ type CreateProjectForm struct { Title string `binding:"Required;MaxSize(100)"` Content string BoardType project_model.BoardType + CardType project_model.CardType } // UserCreateProjectForm is a from for creating an individual or organization @@ -520,6 +522,7 @@ type UserCreateProjectForm struct { Title string `binding:"Required;MaxSize(100)"` Content string BoardType project_model.BoardType + CardType project_model.CardType UID int64 `binding:"Required"` } @@ -561,6 +564,7 @@ func (f *CreateMilestoneForm) Validate(req *http.Request, errs binding.Errors) b type CreateLabelForm struct { ID int64 Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"` + Exclusive bool `form:"exclusive"` Description string `binding:"MaxSize(200)" locale:"repo.issues.label_description"` Color string `binding:"Required;MaxSize(7)" locale:"repo.issues.label_color"` } diff --git a/services/migrations/main_test.go b/services/migrations/main_test.go index 30875f6e5..42c433fb0 100644 --- a/services/migrations/main_test.go +++ b/services/migrations/main_test.go @@ -59,6 +59,7 @@ func assertCommentsEqual(t *testing.T, expected, actual []*base.Comment) { func assertLabelEqual(t *testing.T, expected, actual *base.Label) { assert.Equal(t, expected.Name, actual.Name) + assert.Equal(t, expected.Exclusive, actual.Exclusive) assert.Equal(t, expected.Color, actual.Color) assert.Equal(t, expected.Description, actual.Description) } diff --git a/services/pull/check.go b/services/pull/check.go index 481491c73..310ea2e71 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -256,13 +256,13 @@ 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 { if pr.BaseRepo.Owner == nil { - if err = pr.BaseRepo.GetOwner(ctx); err != nil { - log.Error("%-v BaseRepo.GetOwner: %v", pr, err) + if err = pr.BaseRepo.LoadOwner(ctx); err != nil { + log.Error("%-v BaseRepo.LoadOwner: %v", pr, err) return false } } diff --git a/services/pull/merge.go b/services/pull/merge.go index a3d69df8d..3ac67d91b 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -199,8 +199,8 @@ func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.U if err := pr.Issue.LoadRepo(hammerCtx); err != nil { log.Error("LoadRepo for issue [%d]: %v", pr.ID, err) } - if err := pr.Issue.Repo.GetOwner(hammerCtx); err != nil { - log.Error("GetOwner for PR [%d]: %v", pr.ID, err) + if err := pr.Issue.Repo.LoadOwner(hammerCtx); err != nil { + log.Error("LoadOwner for PR [%d]: %v", pr.ID, err) } if wasAutoMerged { @@ -573,7 +573,7 @@ func rawMerge(ctx context.Context, pr *issues_model.PullRequest, doer *user_mode } var headUser *user_model.User - err = pr.HeadRepo.GetOwner(ctx) + err = pr.HeadRepo.LoadOwner(ctx) if err != nil { if !user_model.IsErrUserNotExist(err) { log.Error("Can't find user: %d for head repository - %v", pr.HeadRepo.OwnerID, err) diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index d49a15cea..e0d6b4a15 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -38,12 +38,12 @@ func createTemporaryRepo(ctx context.Context, pr *issues_model.PullRequest) (str return "", &repo_model.ErrRepoNotExist{ ID: pr.BaseRepoID, } - } else if err := pr.HeadRepo.GetOwner(ctx); err != nil { - log.Error("HeadRepo.GetOwner: %v", err) - return "", fmt.Errorf("HeadRepo.GetOwner: %w", err) - } else if err := pr.BaseRepo.GetOwner(ctx); err != nil { - log.Error("BaseRepo.GetOwner: %v", err) - return "", fmt.Errorf("BaseRepo.GetOwner: %w", err) + } else if err := pr.HeadRepo.LoadOwner(ctx); err != nil { + log.Error("HeadRepo.LoadOwner: %v", err) + return "", fmt.Errorf("HeadRepo.LoadOwner: %w", err) + } else if err := pr.BaseRepo.LoadOwner(ctx); err != nil { + log.Error("BaseRepo.LoadOwner: %v", err) + return "", fmt.Errorf("BaseRepo.LoadOwner: %w", err) } // Clone base repo. 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/content.go b/services/repository/files/content.go index a311625f3..31827e884 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" ) // ContentType repo content type @@ -158,7 +159,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref return nil, fmt.Errorf("no commit found for the ref [ref: %s]", ref) } - selfURL, err := url.Parse(fmt.Sprintf("%s/contents/%s?ref=%s", repo.APIURL(), treePath, origRef)) + selfURL, err := url.Parse(repo.APIURL() + "/contents/" + util.PathEscapeSegments(treePath) + "?ref=" + url.QueryEscape(origRef)) if err != nil { return nil, err } @@ -217,7 +218,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref } // Handle links if entry.IsRegular() || entry.IsLink() { - downloadURL, err := url.Parse(fmt.Sprintf("%s/raw/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath)) + downloadURL, err := url.Parse(repo.HTMLURL() + "/raw/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath)) if err != nil { return nil, err } @@ -225,7 +226,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref contentsResponse.DownloadURL = &downloadURLString } if !entry.IsSubModule() { - htmlURL, err := url.Parse(fmt.Sprintf("%s/src/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath)) + htmlURL, err := url.Parse(repo.HTMLURL() + "/src/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath)) if err != nil { return nil, err } @@ -233,7 +234,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref contentsResponse.HTMLURL = &htmlURLString contentsResponse.Links.HTMLURL = &htmlURLString - gitURL, err := url.Parse(fmt.Sprintf("%s/git/blobs/%s", repo.APIURL(), entry.ID.String())) + gitURL, err := url.Parse(repo.APIURL() + "/git/blobs/" + url.PathEscape(entry.ID.String())) if err != nil { return nil, err } 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/repository/review.go b/services/repository/review.go index d30d61ee0..6b5f09605 100644 --- a/services/repository/review.go +++ b/services/repository/review.go @@ -12,7 +12,7 @@ import ( // GetReviewerTeams get all teams can be requested to review func GetReviewerTeams(repo *repo_model.Repository) ([]*organization.Team, error) { - if err := repo.GetOwner(db.DefaultContext); err != nil { + if err := repo.LoadOwner(db.DefaultContext); err != nil { return nil, err } if !repo.Owner.IsOrganization() { diff --git a/services/repository/star.go b/services/repository/star.go index d6e740c94..01b3db7b2 100644 --- a/services/repository/star.go +++ b/services/repository/star.go @@ -20,7 +20,7 @@ func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { if err != nil { return err } - err = repo.GetOwner(ctx) + err = repo.LoadOwner(ctx) if err != nil { return err } diff --git a/services/repository/template.go b/services/repository/template.go index 13e074986..8c75948c4 100644 --- a/services/repository/template.go +++ b/services/repository/template.go @@ -31,6 +31,7 @@ func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *repo_m newLabels = append(newLabels, &issues_model.Label{ RepoID: generateRepo.ID, Name: templateLabel.Name, + Exclusive: templateLabel.Exclusive, Description: templateLabel.Description, Color: templateLabel.Color, }) diff --git a/services/repository/transfer.go b/services/repository/transfer.go index f4afb7e2d..8c167552d 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -26,7 +26,7 @@ var repoWorkingPool = sync.NewExclusivePool() // TransferOwnership transfers all corresponding setting from old user to new one. func TransferOwnership(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error { - if err := repo.GetOwner(ctx); err != nil { + if err := repo.LoadOwner(ctx); err != nil { return err } for _, team := range teams { diff --git a/services/user/delete.go b/services/user/delete.go new file mode 100644 index 000000000..01e3c37b3 --- /dev/null +++ b/services/user/delete.go @@ -0,0 +1,191 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package user + +import ( + "context" + "fmt" + "time" + + _ "image/jpeg" // Needed for jpeg support + + activities_model "code.gitea.io/gitea/models/activities" + asymkey_model "code.gitea.io/gitea/models/asymkey" + auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" + git_model "code.gitea.io/gitea/models/git" + issues_model "code.gitea.io/gitea/models/issues" + "code.gitea.io/gitea/models/organization" + access_model "code.gitea.io/gitea/models/perm/access" + pull_model "code.gitea.io/gitea/models/pull" + repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" + + "xorm.io/builder" +) + +// deleteUser deletes models associated to an user. +func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error) { + e := db.GetEngine(ctx) + + // ***** START: Watch ***** + watchedRepoIDs, err := db.FindIDs(ctx, "watch", "watch.repo_id", + builder.Eq{"watch.user_id": u.ID}. + And(builder.Neq{"watch.mode": repo_model.WatchModeDont})) + if err != nil { + return fmt.Errorf("get all watches: %w", err) + } + if err = db.DecrByIDs(ctx, watchedRepoIDs, "num_watches", new(repo_model.Repository)); err != nil { + return fmt.Errorf("decrease repository num_watches: %w", err) + } + // ***** END: Watch ***** + + // ***** START: Star ***** + starredRepoIDs, err := db.FindIDs(ctx, "star", "star.repo_id", + builder.Eq{"star.uid": u.ID}) + if err != nil { + return fmt.Errorf("get all stars: %w", err) + } else if err = db.DecrByIDs(ctx, starredRepoIDs, "num_stars", new(repo_model.Repository)); err != nil { + return fmt.Errorf("decrease repository num_stars: %w", err) + } + // ***** END: Star ***** + + // ***** START: Follow ***** + followeeIDs, err := db.FindIDs(ctx, "follow", "follow.follow_id", + builder.Eq{"follow.user_id": u.ID}) + if err != nil { + return fmt.Errorf("get all followees: %w", err) + } else if err = db.DecrByIDs(ctx, followeeIDs, "num_followers", new(user_model.User)); err != nil { + return fmt.Errorf("decrease user num_followers: %w", err) + } + + followerIDs, err := db.FindIDs(ctx, "follow", "follow.user_id", + builder.Eq{"follow.follow_id": u.ID}) + if err != nil { + return fmt.Errorf("get all followers: %w", err) + } else if err = db.DecrByIDs(ctx, followerIDs, "num_following", new(user_model.User)); err != nil { + return fmt.Errorf("decrease user num_following: %w", err) + } + // ***** END: Follow ***** + + if err = db.DeleteBeans(ctx, + &auth_model.AccessToken{UID: u.ID}, + &repo_model.Collaboration{UserID: u.ID}, + &access_model.Access{UserID: u.ID}, + &repo_model.Watch{UserID: u.ID}, + &repo_model.Star{UID: u.ID}, + &user_model.Follow{UserID: u.ID}, + &user_model.Follow{FollowID: u.ID}, + &activities_model.Action{UserID: u.ID}, + &issues_model.IssueUser{UID: u.ID}, + &user_model.EmailAddress{UID: u.ID}, + &user_model.UserOpenID{UID: u.ID}, + &issues_model.Reaction{UserID: u.ID}, + &organization.TeamUser{UID: u.ID}, + &issues_model.Stopwatch{UserID: u.ID}, + &user_model.Setting{UserID: u.ID}, + &user_model.UserBadge{UserID: u.ID}, + &pull_model.AutoMerge{DoerID: u.ID}, + &pull_model.ReviewState{UserID: u.ID}, + &user_model.Redirect{RedirectUserID: u.ID}, + ); err != nil { + return fmt.Errorf("deleteBeans: %w", err) + } + + if err := auth_model.DeleteOAuth2RelictsByUserID(ctx, u.ID); err != nil { + return err + } + + if purge || (setting.Service.UserDeleteWithCommentsMaxTime != 0 && + u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now())) { + + // Delete Comments + const batchSize = 50 + for { + comments := make([]*issues_model.Comment, 0, batchSize) + if err = e.Where("type=? AND poster_id=?", issues_model.CommentTypeComment, u.ID).Limit(batchSize, 0).Find(&comments); err != nil { + return err + } + if len(comments) == 0 { + break + } + + for _, comment := range comments { + if err = issues_model.DeleteComment(ctx, comment); err != nil { + return err + } + } + } + + // Delete Reactions + if err = issues_model.DeleteReaction(ctx, &issues_model.ReactionOptions{DoerID: u.ID}); err != nil { + return err + } + } + + // ***** START: Branch Protections ***** + { + const batchSize = 50 + for start := 0; ; start += batchSize { + protections := make([]*git_model.ProtectedBranch, 0, batchSize) + // @perf: We can't filter on DB side by u.ID, as those IDs are serialized as JSON strings. + // We could filter down with `WHERE repo_id IN (reposWithPushPermission(u))`, + // though that query will be quite complex and tricky to maintain (compare `getRepoAssignees()`). + // Also, as we didn't update branch protections when removing entries from `access` table, + // it's safer to iterate all protected branches. + if err = e.Limit(batchSize, start).Find(&protections); err != nil { + return fmt.Errorf("findProtectedBranches: %w", err) + } + if len(protections) == 0 { + break + } + for _, p := range protections { + if err := git_model.RemoveUserIDFromProtectedBranch(ctx, p, u.ID); err != nil { + return err + } + } + } + } + // ***** END: Branch Protections ***** + + // ***** START: PublicKey ***** + if _, err = db.DeleteByBean(ctx, &asymkey_model.PublicKey{OwnerID: u.ID}); err != nil { + return fmt.Errorf("deletePublicKeys: %w", err) + } + // ***** END: PublicKey ***** + + // ***** START: GPGPublicKey ***** + keys, err := asymkey_model.ListGPGKeys(ctx, u.ID, db.ListOptions{}) + if err != nil { + return fmt.Errorf("ListGPGKeys: %w", err) + } + // Delete GPGKeyImport(s). + for _, key := range keys { + if _, err = db.DeleteByBean(ctx, &asymkey_model.GPGKeyImport{KeyID: key.KeyID}); err != nil { + return fmt.Errorf("deleteGPGKeyImports: %w", err) + } + } + if _, err = db.DeleteByBean(ctx, &asymkey_model.GPGKey{OwnerID: u.ID}); err != nil { + return fmt.Errorf("deleteGPGKeys: %w", err) + } + // ***** END: GPGPublicKey ***** + + // Clear assignee. + if _, err = db.DeleteByBean(ctx, &issues_model.IssueAssignees{AssigneeID: u.ID}); err != nil { + return fmt.Errorf("clear assignee: %w", err) + } + + // ***** START: ExternalLoginUser ***** + if err = user_model.RemoveAllAccountLinks(ctx, u); err != nil { + return fmt.Errorf("ExternalLoginUser: %w", err) + } + // ***** END: ExternalLoginUser ***** + + if _, err = db.DeleteByID(ctx, u.ID, new(user_model.User)); err != nil { + return fmt.Errorf("delete: %w", err) + } + + return nil +} diff --git a/services/user/user.go b/services/user/user.go index 817e825ca..cba8261b5 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -165,7 +165,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { return models.ErrUserOwnPackages{UID: u.ID} } - if err := models.DeleteUser(ctx, u, purge); err != nil { + if err := deleteUser(ctx, u, purge); err != nil { return fmt.Errorf("DeleteUser: %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/main_test.go b/services/webhook/main_test.go index 6cf941073..210221b12 100644 --- a/services/webhook/main_test.go +++ b/services/webhook/main_test.go @@ -15,8 +15,8 @@ import ( ) func TestMain(m *testing.M) { - setting.LoadForTest() - setting.NewQueueService() + setting.InitProviderAndLoadCommonSettingsForTest() + setting.LoadQueueSettings() // for tests, allow only loopback IPs setting.Webhook.AllowedHostList = hostmatcher.MatchBuiltinLoopback 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) } |