More refactoring of db.DefaultContext (#27083)

Next step of #27065
This commit is contained in:
JakobDev 2023-09-15 08:13:19 +02:00 committed by GitHub
parent f8a1094406
commit c548dde205
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
83 changed files with 336 additions and 320 deletions

View file

@ -31,7 +31,7 @@ func apiError(ctx *context.Context, status int, obj any) {
}
func GetRepositoryKey(ctx *context.Context) {
_, pub, err := alpine_service.GetOrCreateKeyPair(ctx.Package.Owner.ID)
_, pub, err := alpine_service.GetOrCreateKeyPair(ctx, ctx.Package.Owner.ID)
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return

View file

@ -4,6 +4,7 @@
package chef
import (
"context"
"crypto"
"crypto/rsa"
"crypto/sha1"
@ -63,7 +64,7 @@ func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataS
return nil, nil
}
pub, err := getUserPublicKey(u)
pub, err := getUserPublicKey(req.Context(), u)
if err != nil {
return nil, err
}
@ -93,8 +94,8 @@ func getUserFromRequest(req *http.Request) (*user_model.User, error) {
return user_model.GetUserByName(req.Context(), username)
}
func getUserPublicKey(u *user_model.User) (crypto.PublicKey, error) {
pubKey, err := user_model.GetSetting(u.ID, chef_module.SettingPublicPem)
func getUserPublicKey(ctx context.Context, u *user_model.User) (crypto.PublicKey, error) {
pubKey, err := user_model.GetSetting(ctx, u.ID, chef_module.SettingPublicPem)
if err != nil {
return nil, err
}

View file

@ -30,7 +30,7 @@ func apiError(ctx *context.Context, status int, obj any) {
}
func GetRepositoryKey(ctx *context.Context) {
_, pub, err := debian_service.GetOrCreateKeyPair(ctx.Package.Owner.ID)
_, pub, err := debian_service.GetOrCreateKeyPair(ctx, ctx.Package.Owner.ID)
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return

View file

@ -23,7 +23,7 @@ func (a *Auth) Name() string {
// https://docs.microsoft.com/en-us/nuget/api/package-publish-resource#request-parameters
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {
token, err := auth_model.GetAccessTokenBySHA(req.Header.Get("X-NuGet-ApiKey"))
token, err := auth_model.GetAccessTokenBySHA(req.Context(), req.Header.Get("X-NuGet-ApiKey"))
if err != nil {
if !(auth_model.IsErrAccessTokenNotExist(err) || auth_model.IsErrAccessTokenEmpty(err)) {
log.Error("GetAccessTokenBySHA: %v", err)
@ -39,7 +39,7 @@ func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataS
}
token.UpdatedUnix = timeutil.TimeStampNow()
if err := auth_model.UpdateAccessToken(token); err != nil {
if err := auth_model.UpdateAccessToken(req.Context(), token); err != nil {
log.Error("UpdateAccessToken: %v", err)
}

View file

@ -45,7 +45,7 @@ gpgkey=`+url+`/repository.key`)
// Gets or creates the PGP public key used to sign repository metadata files
func GetRepositoryKey(ctx *context.Context) {
_, pub, err := rpm_service.GetOrCreateKeyPair(ctx.Package.Owner.ID)
_, pub, err := rpm_service.GetOrCreateKeyPair(ctx, ctx.Package.Owner.ID)
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return

View file

@ -66,7 +66,7 @@ func Person(ctx *context.APIContext) {
person.PublicKey.ID = ap.IRI(link + "#main-key")
person.PublicKey.Owner = ap.IRI(link)
publicKeyPem, err := activitypub.GetPublicKey(ctx.ContextUser)
publicKeyPem, err := activitypub.GetPublicKey(ctx, ctx.ContextUser)
if err != nil {
ctx.ServerError("GetPublicKey", err)
return

View file

@ -776,7 +776,7 @@ func verifyAuthWithOptions(options *common.VerifyOptions) func(ctx *context.APIC
if skip, ok := ctx.Data["SkipLocalTwoFA"]; ok && skip.(bool) {
return // Skip 2FA
}
twofa, err := auth_model.GetTwoFactorByUID(ctx.Doer.ID)
twofa, err := auth_model.GetTwoFactorByUID(ctx, ctx.Doer.ID)
if err != nil {
if auth_model.IsErrTwoFactorNotEnrolled(err) {
return // No 2FA enrollment for this user

View file

@ -693,7 +693,7 @@ func AddTeamRepository(ctx *context.APIContext) {
ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository")
return
}
if err := org_service.TeamAddRepository(ctx.Org.Team, repo); err != nil {
if err := org_service.TeamAddRepository(ctx, ctx.Org.Team, repo); err != nil {
ctx.Error(http.StatusInternalServerError, "TeamAddRepository", err)
return
}

View file

@ -189,7 +189,7 @@ func CreateIssueAttachment(ctx *context.APIContext) {
issue.Attachments = append(issue.Attachments, attachment)
if err := issue_service.ChangeContent(issue, ctx.Doer, issue.Content); err != nil {
if err := issue_service.ChangeContent(ctx, issue, ctx.Doer, issue.Content); err != nil {
ctx.Error(http.StatusInternalServerError, "ChangeContent", err)
return
}
@ -298,7 +298,7 @@ func DeleteIssueAttachment(ctx *context.APIContext) {
return
}
if err := repo_model.DeleteAttachment(attachment, true); err != nil {
if err := repo_model.DeleteAttachment(ctx, attachment, true); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err)
return
}

View file

@ -303,7 +303,7 @@ func DeleteIssueCommentAttachment(ctx *context.APIContext) {
return
}
if err := repo_model.DeleteAttachment(attach, true); err != nil {
if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err)
return
}

View file

@ -97,7 +97,7 @@ func ListPullRequests(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
prs, maxResults, err := issues_model.PullRequests(ctx.Repo.Repository.ID, &issues_model.PullRequestsOptions{
prs, maxResults, err := issues_model.PullRequests(ctx, ctx.Repo.Repository.ID, &issues_model.PullRequestsOptions{
ListOptions: listOptions,
State: ctx.FormTrim("state"),
SortType: ctx.FormTrim("sort"),

View file

@ -345,7 +345,7 @@ func DeleteReleaseAttachment(ctx *context.APIContext) {
}
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests
if err := repo_model.DeleteAttachment(attach, true); err != nil {
if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err)
return
}

View file

@ -45,7 +45,7 @@ func ListStargazers(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
stargazers, err := repo_model.GetStargazers(ctx.Repo.Repository, utils.GetListOptions(ctx))
stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetStargazers", err)
return

View file

@ -45,7 +45,7 @@ func ListSubscribers(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
subscribers, err := repo_model.GetRepoWatchers(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
subscribers, err := repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetRepoWatchers", err)
return

View file

@ -99,7 +99,7 @@ func IsTeam(ctx *context.APIContext) {
return
}
if repo_service.HasRepository(team, ctx.Repo.Repository.ID) {
if repo_service.HasRepository(ctx, team, ctx.Repo.Repository.ID) {
apiTeam, err := convert.ToTeam(ctx, team)
if err != nil {
ctx.InternalServerError(err)
@ -198,14 +198,14 @@ func changeRepoTeam(ctx *context.APIContext, add bool) {
return
}
repoHasTeam := repo_service.HasRepository(team, ctx.Repo.Repository.ID)
repoHasTeam := repo_service.HasRepository(ctx, team, ctx.Repo.Repository.ID)
var err error
if add {
if repoHasTeam {
ctx.Error(http.StatusUnprocessableEntity, "alreadyAdded", fmt.Errorf("team '%s' is already added to repo", team.Name))
return
}
err = org_service.TeamAddRepository(team, ctx.Repo.Repository)
err = org_service.TeamAddRepository(ctx, team, ctx.Repo.Repository)
} else {
if !repoHasTeam {
ctx.Error(http.StatusUnprocessableEntity, "notAdded", fmt.Errorf("team '%s' was not added to repo", team.Name))

View file

@ -46,12 +46,12 @@ func ListAccessTokens(ctx *context.APIContext) {
opts := auth_model.ListAccessTokensOptions{UserID: ctx.Doer.ID, ListOptions: utils.GetListOptions(ctx)}
count, err := auth_model.CountAccessTokens(opts)
count, err := auth_model.CountAccessTokens(ctx, opts)
if err != nil {
ctx.InternalServerError(err)
return
}
tokens, err := auth_model.ListAccessTokens(opts)
tokens, err := auth_model.ListAccessTokens(ctx, opts)
if err != nil {
ctx.InternalServerError(err)
return
@ -103,7 +103,7 @@ func CreateAccessToken(ctx *context.APIContext) {
Name: form.Name,
}
exist, err := auth_model.AccessTokenByNameExists(t)
exist, err := auth_model.AccessTokenByNameExists(ctx, t)
if err != nil {
ctx.InternalServerError(err)
return
@ -120,7 +120,7 @@ func CreateAccessToken(ctx *context.APIContext) {
}
t.Scope = scope
if err := auth_model.NewAccessToken(t); err != nil {
if err := auth_model.NewAccessToken(ctx, t); err != nil {
ctx.Error(http.StatusInternalServerError, "NewAccessToken", err)
return
}
@ -162,7 +162,7 @@ func DeleteAccessToken(ctx *context.APIContext) {
tokenID, _ := strconv.ParseInt(token, 0, 64)
if tokenID == 0 {
tokens, err := auth_model.ListAccessTokens(auth_model.ListAccessTokensOptions{
tokens, err := auth_model.ListAccessTokens(ctx, auth_model.ListAccessTokensOptions{
Name: token,
UserID: ctx.Doer.ID,
})
@ -187,7 +187,7 @@ func DeleteAccessToken(ctx *context.APIContext) {
return
}
if err := auth_model.DeleteAccessTokenByID(tokenID, ctx.Doer.ID); err != nil {
if err := auth_model.DeleteAccessTokenByID(ctx, tokenID, ctx.Doer.ID); err != nil {
if auth_model.IsErrAccessTokenNotExist(err) {
ctx.NotFound()
} else {

View file

@ -155,7 +155,7 @@ func Star(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
err := repo_model.StarRepo(ctx.Doer.ID, ctx.Repo.Repository.ID, true)
err := repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true)
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return
@ -185,7 +185,7 @@ func Unstar(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
err := repo_model.StarRepo(ctx.Doer.ID, ctx.Repo.Repository.ID, false)
err := repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false)
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return

View file

@ -124,7 +124,7 @@ func IsWatching(ctx *context.APIContext) {
// "404":
// description: User is not watching this repo or repo do not exist
if repo_model.IsWatching(ctx.Doer.ID, ctx.Repo.Repository.ID) {
if repo_model.IsWatching(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) {
ctx.JSON(http.StatusOK, api.WatchInfo{
Subscribed: true,
Ignored: false,