1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-03 09:07:19 +00:00

refactor some functions to support ctx as first parameter (#21878)

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
Lunny Xiao
2022-12-03 10:48:26 +08:00
committed by GitHub
parent 8698458f48
commit 0a7d3ff786
145 changed files with 360 additions and 369 deletions

View File

@ -63,7 +63,7 @@ func ListForks(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
return
}
apiForks[i] = convert.ToRepo(fork, access)
apiForks[i] = convert.ToRepo(ctx, fork, access)
}
ctx.SetTotalCountHeader(int64(ctx.Repo.Repository.NumForks))
@ -150,5 +150,5 @@ func CreateFork(ctx *context.APIContext) {
}
// TODO change back to 201
ctx.JSON(http.StatusAccepted, convert.ToRepo(fork, perm.AccessModeOwner))
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx, fork, perm.AccessModeOwner))
}

View File

@ -184,7 +184,7 @@ func TestHook(ctx *context.APIContext) {
Commits: []*api.PayloadCommit{commit},
TotalCommits: 1,
HeadCommit: commit,
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
Repo: convert.ToRepo(ctx, ctx.Repo.Repository, perm.AccessModeNone),
Pusher: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
Sender: convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone),
}); err != nil {

View File

@ -623,7 +623,7 @@ func CreateIssue(ctx *context.APIContext) {
// Check if the passed assignees is assignable
for _, aID := range assigneeIDs {
assignee, err := user_model.GetUserByID(aID)
assignee, err := user_model.GetUserByID(ctx, aID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
return

View File

@ -199,7 +199,7 @@ func isXRefCommentAccessible(ctx stdCtx.Context, user *user_model.User, c *issue
if issues_model.CommentTypeIsRef(c.Type) && c.RefRepoID != issueRepoID && c.RefRepoID != 0 {
var err error
// Set RefRepo for description in template
c.RefRepo, err = repo_model.GetRepositoryByIDCtx(ctx, c.RefRepoID)
c.RefRepo, err = repo_model.GetRepositoryByID(ctx, c.RefRepoID)
if err != nil {
return false
}

View File

@ -5,6 +5,7 @@
package repo
import (
stdCtx "context"
"fmt"
"net/http"
"net/url"
@ -23,16 +24,16 @@ import (
)
// appendPrivateInformation appends the owner and key type information to api.PublicKey
func appendPrivateInformation(apiKey *api.DeployKey, key *asymkey_model.DeployKey, repository *repo_model.Repository) (*api.DeployKey, error) {
func appendPrivateInformation(ctx stdCtx.Context, apiKey *api.DeployKey, key *asymkey_model.DeployKey, repository *repo_model.Repository) (*api.DeployKey, error) {
apiKey.ReadOnly = key.Mode == perm.AccessModeRead
if repository.ID == key.RepoID {
apiKey.Repository = convert.ToRepo(repository, key.Mode)
apiKey.Repository = convert.ToRepo(ctx, repository, key.Mode)
} else {
repo, err := repo_model.GetRepositoryByID(key.RepoID)
repo, err := repo_model.GetRepositoryByID(ctx, key.RepoID)
if err != nil {
return apiKey, err
}
apiKey.Repository = convert.ToRepo(repo, key.Mode)
apiKey.Repository = convert.ToRepo(ctx, repo, key.Mode)
}
return apiKey, nil
}
@ -107,7 +108,7 @@ func ListDeployKeys(ctx *context.APIContext) {
}
apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
if ctx.Doer.IsAdmin || ((ctx.Repo.Repository.ID == keys[i].RepoID) && (ctx.Doer.ID == ctx.Repo.Owner.ID)) {
apiKeys[i], _ = appendPrivateInformation(apiKeys[i], keys[i], ctx.Repo.Repository)
apiKeys[i], _ = appendPrivateInformation(ctx, apiKeys[i], keys[i], ctx.Repo.Repository)
}
}
@ -161,7 +162,7 @@ func GetDeployKey(ctx *context.APIContext) {
apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
apiKey := convert.ToDeployKey(apiLink, key)
if ctx.Doer.IsAdmin || ((ctx.Repo.Repository.ID == key.RepoID) && (ctx.Doer.ID == ctx.Repo.Owner.ID)) {
apiKey, _ = appendPrivateInformation(apiKey, key, ctx.Repo.Repository)
apiKey, _ = appendPrivateInformation(ctx, apiKey, key, ctx.Repo.Repository)
}
ctx.JSON(http.StatusOK, apiKey)
}

View File

@ -66,7 +66,7 @@ func Migrate(ctx *context.APIContext) {
if len(form.RepoOwner) != 0 {
repoOwner, err = user_model.GetUserByName(ctx, form.RepoOwner)
} else if form.RepoOwnerID != 0 {
repoOwner, err = user_model.GetUserByID(form.RepoOwnerID)
repoOwner, err = user_model.GetUserByID(ctx, form.RepoOwnerID)
} else {
repoOwner = ctx.Doer
}
@ -211,7 +211,7 @@ func Migrate(ctx *context.APIContext) {
}
log.Trace("Repository migrated: %s/%s", repoOwner.Name, form.RepoName)
ctx.JSON(http.StatusCreated, convert.ToRepo(repo, perm.AccessModeAdmin))
ctx.JSON(http.StatusCreated, convert.ToRepo(ctx, repo, perm.AccessModeAdmin))
}
func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, remoteAddr string, err error) {

View File

@ -399,7 +399,7 @@ func CreatePullRequest(ctx *context.APIContext) {
}
// Check if the passed assignees is assignable
for _, aID := range assigneeIDs {
assignee, err := user_model.GetUserByIDCtx(ctx, aID)
assignee, err := user_model.GetUserByID(ctx, aID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
return

View File

@ -215,7 +215,7 @@ func Search(ctx *context.APIContext) {
Error: err.Error(),
})
}
results[i] = convert.ToRepo(repo, accessMode)
results[i] = convert.ToRepo(ctx, repo, accessMode)
}
ctx.SetLinkHeader(int(count), opts.PageSize)
ctx.SetTotalCountHeader(count)
@ -257,12 +257,12 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
}
// reload repo from db to get a real state after creation
repo, err = repo_model.GetRepositoryByID(repo.ID)
repo, err = repo_model.GetRepositoryByID(ctx, repo.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetRepositoryByID", err)
}
ctx.JSON(http.StatusCreated, convert.ToRepo(repo, perm.AccessModeOwner))
ctx.JSON(http.StatusCreated, convert.ToRepo(ctx, repo, perm.AccessModeOwner))
}
// Create one repository of mine
@ -407,7 +407,7 @@ func Generate(ctx *context.APIContext) {
}
log.Trace("Repository generated [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
ctx.JSON(http.StatusCreated, convert.ToRepo(repo, perm.AccessModeOwner))
ctx.JSON(http.StatusCreated, convert.ToRepo(ctx, repo, perm.AccessModeOwner))
}
// CreateOrgRepoDeprecated create one repository of the organization
@ -523,7 +523,7 @@ func Get(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusOK, convert.ToRepo(ctx.Repo.Repository, ctx.Repo.AccessMode))
ctx.JSON(http.StatusOK, convert.ToRepo(ctx, ctx.Repo.Repository, ctx.Repo.AccessMode))
}
// GetByID returns a single Repository
@ -544,7 +544,7 @@ func GetByID(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/Repository"
repo, err := repo_model.GetRepositoryByID(ctx.ParamsInt64(":id"))
repo, err := repo_model.GetRepositoryByID(ctx, ctx.ParamsInt64(":id"))
if err != nil {
if repo_model.IsErrRepoNotExist(err) {
ctx.NotFound()
@ -562,7 +562,7 @@ func GetByID(ctx *context.APIContext) {
ctx.NotFound()
return
}
ctx.JSON(http.StatusOK, convert.ToRepo(repo, perm.AccessMode))
ctx.JSON(http.StatusOK, convert.ToRepo(ctx, repo, perm.AccessMode))
}
// Edit edit repository properties
@ -618,13 +618,13 @@ func Edit(ctx *context.APIContext) {
}
}
repo, err := repo_model.GetRepositoryByID(ctx.Repo.Repository.ID)
repo, err := repo_model.GetRepositoryByID(ctx, ctx.Repo.Repository.ID)
if err != nil {
ctx.InternalServerError(err)
return
}
ctx.JSON(http.StatusOK, convert.ToRepo(repo, ctx.Repo.AccessMode))
ctx.JSON(http.StatusOK, convert.ToRepo(ctx, repo, ctx.Repo.AccessMode))
}
// updateBasicProperties updates the basic properties of a repo: Name, Description, Website and Visibility
@ -669,7 +669,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
if opts.Private != nil {
// Visibility of forked repository is forced sync with base repository.
if repo.IsFork {
if err := repo.GetBaseRepo(); err != nil {
if err := repo.GetBaseRepo(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, "Unable to load base repository", err)
return err
}

View File

@ -66,7 +66,7 @@ func NewCommitStatus(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusCreated, convert.ToCommitStatus(status))
ctx.JSON(http.StatusCreated, convert.ToCommitStatus(ctx, status))
}
// GetCommitStatuses returns all statuses for any given commit hash
@ -199,7 +199,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
apiStatuses := make([]*api.CommitStatus, 0, len(statuses))
for _, status := range statuses {
apiStatuses = append(apiStatuses, convert.ToCommitStatus(status))
apiStatuses = append(apiStatuses, convert.ToCommitStatus(ctx, status))
}
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
@ -263,7 +263,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
return
}
combiStatus := convert.ToCombinedStatus(statuses, convert.ToRepo(repo, ctx.Repo.AccessMode))
combiStatus := convert.ToCombinedStatus(ctx, statuses, convert.ToRepo(ctx, repo, ctx.Repo.AccessMode))
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, combiStatus)

View File

@ -122,12 +122,12 @@ func Transfer(ctx *context.APIContext) {
if ctx.Repo.Repository.Status == repo_model.RepositoryPendingTransfer {
log.Trace("Repository transfer initiated: %s -> %s", oldFullname, ctx.Repo.Repository.FullName())
ctx.JSON(http.StatusCreated, convert.ToRepo(ctx.Repo.Repository, perm.AccessModeAdmin))
ctx.JSON(http.StatusCreated, convert.ToRepo(ctx, ctx.Repo.Repository, perm.AccessModeAdmin))
return
}
log.Trace("Repository transferred: %s -> %s", oldFullname, ctx.Repo.Repository.FullName())
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx.Repo.Repository, perm.AccessModeAdmin))
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx, ctx.Repo.Repository, perm.AccessModeAdmin))
}
// AcceptTransfer accept a repo transfer
@ -165,7 +165,7 @@ func AcceptTransfer(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx.Repo.Repository, ctx.Repo.AccessMode))
ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx, ctx.Repo.Repository, ctx.Repo.AccessMode))
}
// RejectTransfer reject a repo transfer
@ -203,7 +203,7 @@ func RejectTransfer(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusOK, convert.ToRepo(ctx.Repo.Repository, ctx.Repo.AccessMode))
ctx.JSON(http.StatusOK, convert.ToRepo(ctx, ctx.Repo.Repository, ctx.Repo.AccessMode))
}
func acceptOrRejectRepoTransfer(ctx *context.APIContext, accept bool) error {