refactor: deploy key and private route handlers (#38999)

clean up legacy code, fix various bugs:

* add missing "return"
This commit is contained in:
wxiaoguang
2026-08-20 15:57:17 +00:00
committed by GitHub
parent 61be9fcdfa
commit 943f026844
23 changed files with 185 additions and 411 deletions
+5 -20
View File
@@ -5,7 +5,6 @@ package private
import (
"errors"
"fmt"
"net/http"
"strings"
@@ -13,7 +12,6 @@ import (
repo_model "gitea.dev/models/repo"
user_model "gitea.dev/models/user"
"gitea.dev/modules/json"
"gitea.dev/modules/log"
"gitea.dev/modules/private"
"gitea.dev/modules/util"
"gitea.dev/services/context"
@@ -26,38 +24,25 @@ func GenerateActionsRunnerToken(ctx *context.PrivateContext) {
defer rd.Close()
if err := json.NewDecoder(rd).Decode(&genRequest); err != nil {
log.Error("JSON Decode failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("JSON Decode failed: %v", err)
return
}
owner, repo, err := parseScope(ctx, genRequest.Scope)
if err != nil {
log.Error("parseScope failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("parseScope failed: %v", err)
return
}
token, err := actions_model.GetLatestRunnerToken(ctx, owner, repo)
if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
token, err = actions_model.NewRunnerToken(ctx, owner, repo)
if err != nil {
errMsg := fmt.Sprintf("error while creating runner token: %v", err)
log.Error("NewRunnerToken failed: %v", errMsg)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: errMsg,
})
ctx.PrivateInternalErrorf("error while creating runner token: %v", err)
return
}
} else if err != nil {
errMsg := fmt.Sprintf("could not get unactivated runner token: %v", err)
log.Error("GetLatestRunnerToken failed: %v", errMsg)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: errMsg,
})
ctx.PrivateInternalErrorf("could not get unactivated runner token: %v", err)
return
}
+34 -125
View File
@@ -5,7 +5,6 @@ package private
import (
"errors"
"fmt"
"net/http"
"os"
@@ -18,7 +17,6 @@ import (
user_model "gitea.dev/models/user"
"gitea.dev/modules/git"
"gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/log"
"gitea.dev/modules/private"
"gitea.dev/modules/util"
"gitea.dev/modules/web"
@@ -74,9 +72,7 @@ func (ctx *preReceiveContext) assertCanWriteRef(refFullName git.RefName) bool {
if ctx.Written() {
return false
}
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: "User permission denied for writing.",
})
ctx.PrivateUserErrorf(http.StatusForbidden, "User permission denied for writing.")
return false
}
return true
@@ -97,9 +93,7 @@ func (ctx *preReceiveContext) AssertCreatePullRequest() bool {
if ctx.Written() {
return false
}
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: "User permission denied for creating pull-request.",
})
ctx.PrivateUserErrorf(http.StatusForbidden, "User permission denied for creating pull-request.")
return false
}
return true
@@ -159,19 +153,13 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
defaultBranch = repo.DefaultWikiBranch
}
if branchName == defaultBranch && newCommitID == objectFormat.EmptyObjectID().String() {
log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("branch %s is the default branch and cannot be deleted", branchName),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Branch %s is the default branch and cannot be deleted", branchName)
return
}
protectBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, branchName)
if err != nil {
log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("Unable to get protected branch: %v", err)
return
}
@@ -187,10 +175,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
//
// 1. Detect and prevent deletion of the branch
if newCommitID == objectFormat.EmptyObjectID().String() {
log.Warn("Forbidden: Branch: %s in %-v is protected from deletion", branchName, repo)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("branch %s is protected from deletion", branchName),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Branch %s is protected from deletion", branchName)
return
}
@@ -202,19 +187,13 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
AddDynamicArguments(oldCommitID, "^"+newCommitID).
WithEnv(ctx.env).WithRepo(repo).RunStdString(ctx)
if err != nil {
log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, newCommitID, repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Fail to detect force push: %v", err),
})
ctx.PrivateInternalErrorf("Unable to detect force push between %s and %s in %s: %v", oldCommitID, newCommitID, repo.FullName(), err)
return
} else if len(output) > 0 {
if protectBranch.CanForcePush {
isForcePush = true
} else {
log.Warn("Forbidden: Branch: %s in %-v is protected from force push", branchName, repo)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("branch %s is protected from force push", branchName),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Branch %s is protected from force push", branchName)
return
}
}
@@ -226,16 +205,10 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
if err != nil {
errUnverified, ok := err.(*errUnverifiedCommit)
if !ok {
log.Error("Unable to check commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to check commits from %s to %s: %v", oldCommitID, newCommitID, err),
})
ctx.PrivateInternalErrorf("Unable to check commits from %s to %s: %v", oldCommitID, newCommitID, err)
return
}
log.Warn("Forbidden: Branch: %s in %-v is protected from unverified commit %s", branchName, repo, errUnverified.sha)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("branch %s is protected from unverified commit %s", branchName, errUnverified.sha),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Branch %s is protected from unverified commit %s", branchName, errUnverified.sha)
return
}
}
@@ -252,10 +225,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
if err != nil {
errFilePathProtected, ok := errors.AsType[pull_service.ErrFilePathProtected](err)
if !ok {
log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to check file protection for commits from %s to %s: %v", oldCommitID, newCommitID, err),
})
ctx.PrivateInternalErrorf("Unable to check file protection for commits from %s to %s: %v", oldCommitID, newCommitID, err)
return
}
@@ -287,12 +257,9 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
if ctx.opts.PullRequestID == 0 {
// 6a. If we're not merging from the UI/API then there are two ways we got here:
//
// We are changing a protected file and we're not allowed to do that
// We are changing a protected file, and we're not allowed to do that
if changedProtectedfiles {
log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Branch %s is protected from changing file %s", branchName, protectedFilePath)
return
}
@@ -301,10 +268,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
if len(globs) > 0 {
unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(ctx, gitRepo, branchName, oldCommitID, newCommitID, globs, ctx.env)
if err != nil {
log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to check file protection for commits from %s to %s: %v", oldCommitID, newCommitID, err),
})
ctx.PrivateInternalErrorf("Unable to check file protection for commits from %s to %s: %v", oldCommitID, newCommitID, err)
return
}
if unprotectedFilesOnly {
@@ -315,16 +279,10 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
// Or we're simply not able to push to this protected branch
if isForcePush {
log.Warn("Forbidden: User %d is not allowed to force-push to protected branch: %s in %-v", ctx.opts.UserID, branchName, repo)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: "Not allowed to force-push to protected branch " + branchName,
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Not allowed to force-push to protected branch %s", branchName)
return
}
log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v", ctx.opts.UserID, branchName, repo)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: "Not allowed to push to protected branch " + branchName,
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Not allowed to push to protected branch %s", branchName)
return
}
// 6b. Merge (from UI or API)
@@ -332,10 +290,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
// Get the PR, user and permissions for the user in the repository
pr, err := issues_model.GetPullRequestByID(ctx, ctx.opts.PullRequestID)
if err != nil {
log.Error("Unable to get PullRequest %d Error: %v", ctx.opts.PullRequestID, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to get PullRequest %d Error: %v", ctx.opts.PullRequestID, err),
})
ctx.PrivateInternalErrorf("Unable to get PullRequest %d Error: %v", ctx.opts.PullRequestID, err)
return
}
@@ -343,18 +298,12 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
// Note: we can use ctx.perm and ctx.user directly as they will have been loaded above
allowedMerge, err := pull_service.IsUserAllowedToMerge(ctx, pr, ctx.userPerm, ctx.user)
if err != nil {
log.Error("Error calculating if allowed to merge: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Error calculating if allowed to merge: %v", err),
})
ctx.PrivateInternalErrorf("Error calculating if allowed to merge: %v", err)
return
}
if !allowedMerge {
log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v and is not allowed to merge pr #%d", ctx.opts.UserID, branchName, repo, pr.Index)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: "Not allowed to push to protected branch " + branchName,
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Not allowed to push to protected branch %s", branchName)
return
}
@@ -365,26 +314,17 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
// Now if we're not an admin - we can't overwrite protected files so fail now
if changedProtectedfiles {
log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Branch %s is protected from changing file %s", branchName, protectedFilePath)
return
}
// Check all status checks and reviews are ok
if err := pull_service.CheckPullBranchProtections(ctx, pr, true); err != nil {
if errors.Is(err, pull_service.ErrNotReadyToMerge) {
log.Warn("Forbidden: User %d is not allowed push to protected branch %s in %-v and pr #%d is not ready to be merged: %s", ctx.opts.UserID, branchName, repo, pr.Index, err.Error())
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, ctx.opts.PullRequestID, err.Error()),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, ctx.opts.PullRequestID, err.Error())
return
}
log.Error("Unable to check if mergeable: protected branch %s in %-v and pr #%d. Error: %v", ctx.opts.UserID, branchName, repo, pr.Index, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to get status of pull request %d. Error: %v", ctx.opts.PullRequestID, err),
})
ctx.PrivateInternalErrorf("Unable to get status of pull request %d: %v", ctx.opts.PullRequestID, err)
return
}
}
@@ -401,10 +341,7 @@ func preReceiveTag(ctx *preReceiveContext, refFullName git.RefName) {
var err error
ctx.protectedTags, err = git_model.GetProtectedTags(ctx, ctx.Repo.Repository.ID)
if err != nil {
log.Error("Unable to get protected tags for %-v Error: %v", ctx.Repo.Repository, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("Unable to get protected tags: %v", err)
return
}
ctx.gotProtectedTags = true
@@ -412,16 +349,11 @@ func preReceiveTag(ctx *preReceiveContext, refFullName git.RefName) {
isAllowed, err := git_model.IsUserAllowedToControlTag(ctx, ctx.protectedTags, tagName, ctx.opts.UserID)
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("unable to check allowed tags: %v", err)
return
}
if !isAllowed {
log.Warn("Forbidden: Tag %s in %-v is protected", tagName, ctx.Repo.Repository)
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("Tag %s is protected", tagName),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Tag %s is protected", tagName)
return
}
}
@@ -432,29 +364,21 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) {
}
if ctx.Repo.Repository.IsEmpty {
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: "Can't create pull request for an empty repository.",
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Can't create pull request for an empty repository.")
return
}
if ctx.opts.IsWiki {
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: "Pull requests are not supported on the wiki.",
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Pull requests are not supported on the wiki.")
return
}
_, _, err := agit.GetAgitBranchInfo(ctx, ctx.Repo.Repository.ID, refFullName.ForBranchName())
if err != nil {
if !errors.Is(err, util.ErrNotExist) {
ctx.JSON(http.StatusForbidden, private.Response{
UserMsg: fmt.Sprintf("Unexpected ref: %s", refFullName),
})
ctx.PrivateUserErrorf(http.StatusForbidden, "Unexpected ref: %s", refFullName)
} else {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("Unable to get branch info for ref %s: %v", refFullName, err)
}
}
}
@@ -482,50 +406,35 @@ func (ctx *preReceiveContext) loadPusherAndPermission() bool {
taskID := ctx.opts.ActionsTaskID
ctx.user = user_model.NewActionsUserWithTaskID(taskID)
if taskID == 0 {
log.Error("HookPreReceive: ActionsUser with task ID 0")
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: "ActionsUser with task ID 0",
})
ctx.PrivateUserErrorf(http.StatusInternalServerError, "ActionsUser with task ID 0")
return false
}
userPerm, err := access_model.GetActionsUserRepoPermission(ctx, ctx.Repo.Repository, ctx.user, taskID)
if err != nil {
log.Error("Unable to get Actions user repo permission for task %d Error: %v", taskID, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to get Actions user repo permission for task %d Error: %v", taskID, err),
})
ctx.PrivateInternalErrorf("Unable to get Actions user repo permission for task %d Error: %v", taskID, err)
return false
}
ctx.userPerm = userPerm
} else {
user, err := user_model.GetUserByID(ctx, ctx.opts.UserID)
if err != nil {
log.Error("Unable to get User id %d Error: %v", ctx.opts.UserID, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to get User id %d Error: %v", ctx.opts.UserID, err),
})
ctx.PrivateInternalErrorf("Unable to get User id %d Error: %v", ctx.opts.UserID, err)
return false
}
ctx.user = user
userPerm, err := access_model.GetDoerRepoPermission(ctx, ctx.Repo.Repository, user)
if err != nil {
log.Error("Unable to get Repo permission of repo %s/%s of User %s: %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name, user.Name, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to get Repo permission of repo %s/%s of User %s: %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name, user.Name, err),
})
ctx.PrivateInternalErrorf("Unable to get Repo permission of repo %s/%s of User %s: %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name, user.Name, err)
return false
}
ctx.userPerm = userPerm
}
if ctx.opts.DeployKeyID != 0 {
deployKey, err := asymkey_model.GetDeployKeyByID(ctx, ctx.opts.DeployKeyID)
deployKey, err := asymkey_model.GetDeployKeyByID(ctx, ctx.Repo.Repository.ID, ctx.opts.DeployKeyID)
if err != nil {
log.Error("Unable to get DeployKey id %d Error: %v", ctx.opts.DeployKeyID, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to get DeployKey id %d Error: %v", ctx.opts.DeployKeyID, err),
})
ctx.PrivateInternalErrorf("Unable to get DeployKey id %d Error: %v", ctx.opts.DeployKeyID, err)
return false
}
ctx.deployKeyAccessMode = deployKey.Mode
+3 -11
View File
@@ -10,7 +10,6 @@ import (
issues_model "gitea.dev/models/issues"
user_model "gitea.dev/models/user"
"gitea.dev/modules/git"
"gitea.dev/modules/log"
"gitea.dev/modules/private"
"gitea.dev/modules/web"
"gitea.dev/services/agit"
@@ -28,18 +27,11 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
results, err := agit.ProcReceive(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, opts)
if err != nil {
if errors.Is(err, issues_model.ErrMustCollaborator) {
ctx.JSON(http.StatusUnauthorized, private.Response{
Err: err.Error(), UserMsg: "You must be a collaborator to create pull request.",
})
ctx.PrivateUserErrorf(http.StatusUnauthorized, "You must be a collaborator to create pull request.")
} else if errors.Is(err, user_model.ErrBlockedUser) {
ctx.JSON(http.StatusUnauthorized, private.Response{
Err: err.Error(), UserMsg: "Cannot create pull request because you are blocked by the repository owner.",
})
ctx.PrivateUserErrorf(http.StatusUnauthorized, "Cannot create pull request because you are blocked by the repository owner.")
} else {
log.Error("agit.ProcReceive failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("agit.ProcReceive failed: %v", err)
}
return
+2 -13
View File
@@ -4,13 +4,8 @@
package private
import (
"fmt"
"net/http"
repo_model "gitea.dev/models/repo"
"gitea.dev/modules/git"
"gitea.dev/modules/log"
"gitea.dev/modules/private"
gitea_context "gitea.dev/services/context"
)
@@ -29,10 +24,7 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) {
gitRepo, err := git.RepositoryFromRequestContextOrOpen(ctx, repo)
if err != nil {
log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err),
})
ctx.PrivateInternalErrorf("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err)
return
}
ctx.Repo = &gitea_context.Repository{
@@ -44,10 +36,7 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) {
func loadRepository(ctx *gitea_context.PrivateContext, ownerName, repoName string) *repo_model.Repository {
repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, ownerName, repoName)
if err != nil {
log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
})
ctx.PrivateInternalErrorf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
return nil
}
if repo.OwnerName == "" {
+6 -18
View File
@@ -7,7 +7,6 @@ import (
"net/http"
asymkey_model "gitea.dev/models/asymkey"
"gitea.dev/modules/private"
"gitea.dev/modules/timeutil"
"gitea.dev/services/context"
)
@@ -17,28 +16,22 @@ func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
keyID := ctx.PathParamInt64("id")
repoID := ctx.PathParamInt64("repoid")
if err := asymkey_model.UpdatePublicKeyUpdated(ctx, keyID); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("%v", err)
return
}
deployKey, err := asymkey_model.GetDeployKeyByRepo(ctx, keyID, repoID)
deployKey, err := asymkey_model.GetDeployKeyByRepoPublicKey(ctx, repoID, keyID)
if err != nil {
if asymkey_model.IsErrDeployKeyNotExist(err) {
ctx.PlainText(http.StatusOK, "success")
return
}
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("%v", err)
return
}
deployKey.UpdatedUnix = timeutil.TimeStampNow()
if err = asymkey_model.UpdateDeployKeyCols(ctx, deployKey, "updated_unix"); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("%v", err)
return
}
@@ -52,18 +45,13 @@ func AuthorizedPublicKeyByContent(ctx *context.PrivateContext) {
publicKey, err := asymkey_model.SearchPublicKeyByContent(ctx, content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("%v", err)
return
}
authorizedString, err := asymkey_model.AuthorizedStringForKey(publicKey)
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
UserMsg: "invalid public key",
})
ctx.PrivateInternalErrorf("%v", err)
return
}
ctx.PlainText(http.StatusOK, authorizedString)
+4 -19
View File
@@ -5,14 +5,12 @@ package private
import (
stdCtx "context"
"fmt"
"net/http"
"strconv"
"gitea.dev/models/db"
user_model "gitea.dev/models/user"
"gitea.dev/modules/json"
"gitea.dev/modules/log"
"gitea.dev/modules/private"
"gitea.dev/modules/setting"
"gitea.dev/services/context"
@@ -25,9 +23,7 @@ import (
// It doesn't wait before each message will be processed
func SendEmail(ctx *context.PrivateContext) {
if setting.MailService == nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: "Mail service is not enabled.",
})
ctx.PrivateInternalErrorf("Mail service is not enabled.")
return
}
@@ -36,10 +32,7 @@ func SendEmail(ctx *context.PrivateContext) {
defer rd.Close()
if err := json.NewDecoder(rd).Decode(&mail); err != nil {
log.Error("JSON Decode failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("JSON Decode failed: %v", err)
return
}
@@ -48,11 +41,7 @@ func SendEmail(ctx *context.PrivateContext) {
for _, uname := range mail.To {
user, err := user_model.GetUserByName(ctx, uname)
if err != nil {
err := fmt.Sprintf("Failed to get user information: %v", err)
log.Error(err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err,
})
ctx.PrivateInternalErrorf("Failed to get user information: %v", err)
return
}
@@ -68,11 +57,7 @@ func SendEmail(ctx *context.PrivateContext) {
return nil
})
if err != nil {
err := fmt.Sprintf("Failed to find users: %v", err)
log.Error(err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err,
})
ctx.PrivateInternalErrorf("Failed to find users: %v", err)
return
}
}
+4 -10
View File
@@ -4,7 +4,6 @@
package private
import (
"fmt"
"net/http"
"gitea.dev/models/db"
@@ -22,9 +21,7 @@ import (
func ReloadTemplates(ctx *context.PrivateContext) {
err := templates.ReloadAllTemplates()
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
UserMsg: fmt.Sprintf("Template error: %v", err),
})
ctx.PrivateInternalErrorf("Template error: %v", err)
return
}
ctx.PlainText(http.StatusOK, "success")
@@ -49,9 +46,8 @@ func FlushQueues(ctx *context.PrivateContext) {
}
err := queue.GetManager().FlushAll(ctx, opts.Timeout)
if err != nil {
ctx.JSON(http.StatusRequestTimeout, private.Response{
UserMsg: fmt.Sprintf("%v", err),
})
ctx.PrivateUserErrorf(http.StatusRequestTimeout, "%v", err)
return
}
ctx.PlainText(http.StatusOK, "success")
}
@@ -71,9 +67,7 @@ func ResumeLogging(ctx *context.PrivateContext) {
// ReleaseReopenLogging releases and reopens logging files
func ReleaseReopenLogging(ctx *context.PrivateContext) {
if err := releasereopen.GetManager().ReleaseReopen(); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Error during release and reopen: %v", err),
})
ctx.PrivateInternalErrorf("Error during release and reopen: %v", err)
return
}
ctx.PlainText(http.StatusOK, "success")
+2 -10
View File
@@ -11,8 +11,6 @@ import (
"runtime"
"time"
"gitea.dev/modules/log"
"gitea.dev/modules/private"
process_module "gitea.dev/modules/process"
"gitea.dev/services/context"
)
@@ -38,10 +36,7 @@ func Processes(ctx *context.PrivateContext) {
if stacktraces {
processes, processCount, goroutineCount, err = process_module.GetManager().ProcessStacktraces(flat, noSystem)
if err != nil {
log.Error("Unable to get stacktrace: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Failed to get stacktraces: %v", err),
})
ctx.PrivateInternalErrorf("Failed to get stacktraces: %v", err)
return
}
} else {
@@ -61,11 +56,8 @@ func Processes(ctx *context.PrivateContext) {
ctx.Resp.WriteHeader(http.StatusOK)
if err := writeProcesses(ctx.Resp, processes, processCount, goroutineCount, "", flat); err != nil {
log.Error("Unable to write out process stacktrace: %v", err)
if !ctx.Written() {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Failed to get stacktraces: %v", err),
})
ctx.PrivateInternalErrorf("Failed to get stacktraces: %v", err)
}
return
}
+1 -4
View File
@@ -9,15 +9,12 @@ import (
"net/http"
"gitea.dev/modules/graceful"
"gitea.dev/modules/private"
"gitea.dev/services/context"
)
// Restart is not implemented for Windows based servers as they can't fork
func Restart(ctx *context.PrivateContext) {
ctx.JSON(http.StatusNotImplemented, private.Response{
UserMsg: "windows servers cannot be gracefully restarted - shutdown and restart manually",
})
ctx.PrivateUserErrorf(http.StatusNotImplemented, "windows servers cannot be gracefully restarted - shutdown and restart manually")
}
// Shutdown causes the server to perform a graceful shutdown
+3 -10
View File
@@ -8,7 +8,6 @@ import (
"net/http"
"gitea.dev/modules/json"
"gitea.dev/modules/private"
myCtx "gitea.dev/services/context"
"gitea.dev/services/migrations"
)
@@ -17,9 +16,7 @@ import (
func RestoreRepo(ctx *myCtx.PrivateContext) {
bs, err := io.ReadAll(ctx.Req.Body)
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("%v", err)
return
}
params := struct {
@@ -30,9 +27,7 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
Validation bool
}{}
if err = json.Unmarshal(bs, &params); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("%v", err)
return
}
@@ -44,9 +39,7 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
params.Units,
params.Validation,
); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
ctx.PrivateInternalErrorf("%v", err)
} else {
ctx.PlainText(http.StatusOK, "success")
}
+1 -1
View File
@@ -198,7 +198,7 @@ func ServCommand(ctx *context.PrivateContext) {
ctx.PrivateUserErrorf(http.StatusNotFound, "Cannot find repository %s", repoLogName)
return
}
deployKey, err = asymkey_model.GetDeployKeyByRepo(ctx, key.ID, repo.ID)
deployKey, err = asymkey_model.GetDeployKeyByRepoPublicKey(ctx, repo.ID, key.ID)
if err != nil {
if asymkey_model.IsErrDeployKeyNotExist(err) {
ctx.PrivateUserErrorf(http.StatusNotFound, "Deploy key %d:%s has no %q permission for %s.", key.ID, key.Name, modeString, repoLogName)