Small refactoring of modules/private (#15947)

* Use correct variable name.

* doer is never nil here.

* Use status code constants.

* Replaced generic map with concrete struct.

* Fixed windows lint.

* Removed unused method.

* Changed error codes.

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
pull/16236/head^2
KN4CK3R 3 years ago committed by GitHub
parent 5930d09096
commit 383ffcfa34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      cmd/hook.go
  2. 27
      models/branches.go
  3. 2
      modules/private/hook.go
  4. 1
      modules/private/serv.go
  5. 104
      routers/private/hook.go
  6. 19
      routers/private/key.go
  7. 16
      routers/private/mail.go
  8. 24
      routers/private/manager.go
  9. 5
      routers/private/manager_windows.go
  10. 16
      routers/private/restore_repo.go
  11. 196
      routers/private/serv.go
  12. 2
      routers/web/repo/issue.go
  13. 2
      services/repository/branch.go

@ -179,7 +179,7 @@ Gitea or set your environment appropriately.`, "")
GitObjectDirectory: os.Getenv(private.GitObjectDirectory), GitObjectDirectory: os.Getenv(private.GitObjectDirectory),
GitQuarantinePath: os.Getenv(private.GitQuarantinePath), GitQuarantinePath: os.Getenv(private.GitQuarantinePath),
GitPushOptions: pushOptions(), GitPushOptions: pushOptions(),
ProtectedBranchID: prID, PullRequestID: prID,
IsDeployKey: isDeployKey, IsDeployKey: isDeployKey,
} }

@ -362,11 +362,7 @@ func (repo *Repository) GetBranchProtection(branchName string) (*ProtectedBranch
} }
// IsProtectedBranch checks if branch is protected // IsProtectedBranch checks if branch is protected
func (repo *Repository) IsProtectedBranch(branchName string, doer *User) (bool, error) { func (repo *Repository) IsProtectedBranch(branchName string) (bool, error) {
if doer == nil {
return true, nil
}
protectedBranch := &ProtectedBranch{ protectedBranch := &ProtectedBranch{
RepoID: repo.ID, RepoID: repo.ID,
BranchName: branchName, BranchName: branchName,
@ -379,27 +375,6 @@ func (repo *Repository) IsProtectedBranch(branchName string, doer *User) (bool,
return has, nil return has, nil
} }
// IsProtectedBranchForPush checks if branch is protected for push
func (repo *Repository) IsProtectedBranchForPush(branchName string, doer *User) (bool, error) {
if doer == nil {
return true, nil
}
protectedBranch := &ProtectedBranch{
RepoID: repo.ID,
BranchName: branchName,
}
has, err := x.Get(protectedBranch)
if err != nil {
return true, err
} else if has {
return !protectedBranch.CanUserPush(doer.ID), nil
}
return false, nil
}
// updateApprovalWhitelist checks whether the user whitelist changed and returns a whitelist with // updateApprovalWhitelist checks whether the user whitelist changed and returns a whitelist with
// the users from newWhitelist which have explicit read or write access to the repo. // the users from newWhitelist which have explicit read or write access to the repo.
func updateApprovalWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) { func updateApprovalWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {

@ -54,7 +54,7 @@ type HookOptions struct {
GitAlternativeObjectDirectories string GitAlternativeObjectDirectories string
GitQuarantinePath string GitQuarantinePath string
GitPushOptions GitPushOptions GitPushOptions GitPushOptions
ProtectedBranchID int64 PullRequestID int64
IsDeployKey bool IsDeployKey bool
} }

@ -58,7 +58,6 @@ type ServCommandResults struct {
// ErrServCommand is an error returned from ServCommmand. // ErrServCommand is an error returned from ServCommmand.
type ErrServCommand struct { type ErrServCommand struct {
Results ServCommandResults Results ServCommandResults
Type string
Err string Err string
StatusCode int StatusCode int
} }

@ -124,8 +124,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
if err != nil { if err != nil {
log.Error("Unable to get repository: %s/%s Error: %v", ownerName, repoName, err) log.Error("Unable to get repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -133,8 +133,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
gitRepo, err := git.OpenRepository(repo.RepoPath()) gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil { if err != nil {
log.Error("Unable to get git repository for: %s/%s Error: %v", ownerName, repoName, err) log.Error("Unable to get git repository for: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -164,8 +164,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix) branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
if branchName == repo.DefaultBranch && newCommitID == git.EmptySHA { if branchName == repo.DefaultBranch && newCommitID == git.EmptySHA {
log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo) log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("branch %s is the default branch and cannot be deleted", branchName), Err: fmt.Sprintf("branch %s is the default branch and cannot be deleted", branchName),
}) })
return return
} }
@ -173,8 +173,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
protectBranch, err := models.GetProtectedBranchBy(repo.ID, branchName) protectBranch, err := models.GetProtectedBranchBy(repo.ID, branchName)
if err != nil { if err != nil {
log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err) log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -191,8 +191,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
// 1. Detect and prevent deletion of the branch // 1. Detect and prevent deletion of the branch
if newCommitID == git.EmptySHA { if newCommitID == git.EmptySHA {
log.Warn("Forbidden: Branch: %s in %-v is protected from deletion", branchName, repo) log.Warn("Forbidden: Branch: %s in %-v is protected from deletion", branchName, repo)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("branch %s is protected from deletion", branchName), Err: fmt.Sprintf("branch %s is protected from deletion", branchName),
}) })
return return
} }
@ -202,14 +202,14 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDirWithEnv(repo.RepoPath(), env) output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDirWithEnv(repo.RepoPath(), env)
if err != nil { if err != nil {
log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, newCommitID, repo, err) log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, newCommitID, repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Fail to detect force push: %v", err), Err: fmt.Sprintf("Fail to detect force push: %v", err),
}) })
return return
} else if len(output) > 0 { } else if len(output) > 0 {
log.Warn("Forbidden: Branch: %s in %-v is protected from force push", branchName, repo) log.Warn("Forbidden: Branch: %s in %-v is protected from force push", branchName, repo)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("branch %s is protected from force push", branchName), Err: fmt.Sprintf("branch %s is protected from force push", branchName),
}) })
return return
@ -222,15 +222,15 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
if err != nil { if err != nil {
if !isErrUnverifiedCommit(err) { if !isErrUnverifiedCommit(err) {
log.Error("Unable to check commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err) log.Error("Unable to check commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Unable to check commits from %s to %s: %v", oldCommitID, newCommitID, err), Err: fmt.Sprintf("Unable to check commits from %s to %s: %v", oldCommitID, newCommitID, err),
}) })
return return
} }
unverifiedCommit := err.(*errUnverifiedCommit).sha unverifiedCommit := err.(*errUnverifiedCommit).sha
log.Warn("Forbidden: Branch: %s in %-v is protected from unverified commit %s", branchName, repo, unverifiedCommit) log.Warn("Forbidden: Branch: %s in %-v is protected from unverified commit %s", branchName, repo, unverifiedCommit)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("branch %s is protected from unverified commit %s", branchName, unverifiedCommit), Err: fmt.Sprintf("branch %s is protected from unverified commit %s", branchName, unverifiedCommit),
}) })
return return
} }
@ -248,8 +248,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
if err != nil { if err != nil {
if !models.IsErrFilePathProtected(err) { if !models.IsErrFilePathProtected(err) {
log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err) log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Unable to check file protection for commits from %s to %s: %v", oldCommitID, newCommitID, err), Err: fmt.Sprintf("Unable to check file protection for commits from %s to %s: %v", oldCommitID, newCommitID, err),
}) })
return return
} }
@ -270,49 +270,49 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
// 6. If we're not allowed to push directly // 6. If we're not allowed to push directly
if !canPush { if !canPush {
// Is this is a merge from the UI/API? // Is this is a merge from the UI/API?
if opts.ProtectedBranchID == 0 { if opts.PullRequestID == 0 {
// 6a. If we're not merging from the UI/API then there are two ways we got here: // 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 { if changedProtectedfiles {
log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath) log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath), Err: fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath),
}) })
return return
} }
// Or we're simply not able to push to this protected branch // Or we're simply not able to push to this protected branch
log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v", opts.UserID, branchName, repo) log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v", opts.UserID, branchName, repo)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("Not allowed to push to protected branch %s", branchName), Err: fmt.Sprintf("Not allowed to push to protected branch %s", branchName),
}) })
return return
} }
// 6b. Merge (from UI or API) // 6b. Merge (from UI or API)
// Get the PR, user and permissions for the user in the repository // Get the PR, user and permissions for the user in the repository
pr, err := models.GetPullRequestByID(opts.ProtectedBranchID) pr, err := models.GetPullRequestByID(opts.PullRequestID)
if err != nil { if err != nil {
log.Error("Unable to get PullRequest %d Error: %v", opts.ProtectedBranchID, err) log.Error("Unable to get PullRequest %d Error: %v", opts.PullRequestID, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Unable to get PullRequest %d Error: %v", opts.ProtectedBranchID, err), Err: fmt.Sprintf("Unable to get PullRequest %d Error: %v", opts.PullRequestID, err),
}) })
return return
} }
user, err := models.GetUserByID(opts.UserID) user, err := models.GetUserByID(opts.UserID)
if err != nil { if err != nil {
log.Error("Unable to get User id %d Error: %v", opts.UserID, err) log.Error("Unable to get User id %d Error: %v", opts.UserID, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Unable to get User id %d Error: %v", opts.UserID, err), Err: fmt.Sprintf("Unable to get User id %d Error: %v", opts.UserID, err),
}) })
return return
} }
perm, err := models.GetUserRepoPermission(repo, user) perm, err := models.GetUserRepoPermission(repo, user)
if err != nil { if err != nil {
log.Error("Unable to get Repo permission of repo %s/%s of User %s", repo.OwnerName, repo.Name, user.Name, err) log.Error("Unable to get Repo permission of repo %s/%s of User %s", repo.OwnerName, repo.Name, user.Name, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Unable to get Repo permission of repo %s/%s of User %s: %v", repo.OwnerName, repo.Name, user.Name, err), Err: fmt.Sprintf("Unable to get Repo permission of repo %s/%s of User %s: %v", repo.OwnerName, repo.Name, user.Name, err),
}) })
return return
} }
@ -321,16 +321,16 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
allowedMerge, err := pull_service.IsUserAllowedToMerge(pr, perm, user) allowedMerge, err := pull_service.IsUserAllowedToMerge(pr, perm, user)
if err != nil { if err != nil {
log.Error("Error calculating if allowed to merge: %v", err) log.Error("Error calculating if allowed to merge: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Error calculating if allowed to merge: %v", err), Err: fmt.Sprintf("Error calculating if allowed to merge: %v", err),
}) })
return return
} }
if !allowedMerge { 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", opts.UserID, branchName, repo, pr.Index) log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v and is not allowed to merge pr #%d", opts.UserID, branchName, repo, pr.Index)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("Not allowed to push to protected branch %s", branchName), Err: fmt.Sprintf("Not allowed to push to protected branch %s", branchName),
}) })
return return
} }
@ -343,8 +343,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
// Now if we're not an admin - we can't overwrite protected files so fail now // Now if we're not an admin - we can't overwrite protected files so fail now
if changedProtectedfiles { if changedProtectedfiles {
log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath) log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath)
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath), Err: fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath),
}) })
return return
} }
@ -353,14 +353,14 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
if err := pull_service.CheckPRReadyToMerge(pr, true); err != nil { if err := pull_service.CheckPRReadyToMerge(pr, true); err != nil {
if models.IsErrNotAllowedToMerge(err) { if models.IsErrNotAllowedToMerge(err) {
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", opts.UserID, branchName, repo, pr.Index, err.Error()) 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", opts.UserID, branchName, repo, pr.Index, err.Error())
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": fmt.Sprintf("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, opts.ProtectedBranchID, err.Error()), Err: fmt.Sprintf("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, opts.PullRequestID, err.Error()),
}) })
return return
} }
log.Error("Unable to check if mergable: protected branch %s in %-v and pr #%d. Error: %v", opts.UserID, branchName, repo, pr.Index, err) log.Error("Unable to check if mergable: protected branch %s in %-v and pr #%d. Error: %v", opts.UserID, branchName, repo, pr.Index, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Unable to get status of pull request %d. Error: %v", opts.ProtectedBranchID, err), Err: fmt.Sprintf("Unable to get status of pull request %d. Error: %v", opts.PullRequestID, err),
}) })
return return
} }
@ -549,8 +549,8 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
if err != nil { if err != nil {
log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err) log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"Err": fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err), Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
}) })
return return
} }
@ -561,16 +561,16 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
repo.DefaultBranch = branch repo.DefaultBranch = branch
gitRepo, err := git.OpenRepository(repo.RepoPath()) gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"Err": fmt.Sprintf("Failed to get git repository: %s/%s Error: %v", ownerName, repoName, err), Err: fmt.Sprintf("Failed to get git repository: %s/%s Error: %v", ownerName, repoName, err),
}) })
return return
} }
if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
if !git.IsErrUnsupportedVersion(err) { if !git.IsErrUnsupportedVersion(err) {
gitRepo.Close() gitRepo.Close()
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"Err": fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err), Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
}) })
return return
} }
@ -578,10 +578,10 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
gitRepo.Close() gitRepo.Close()
if err := repo.UpdateDefaultBranch(); err != nil { if err := repo.UpdateDefaultBranch(); err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"Err": fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err), Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
}) })
return return
} }
ctx.PlainText(200, []byte("success")) ctx.PlainText(http.StatusOK, []byte("success"))
} }

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
) )
@ -18,8 +19,8 @@ func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
keyID := ctx.ParamsInt64(":id") keyID := ctx.ParamsInt64(":id")
repoID := ctx.ParamsInt64(":repoid") repoID := ctx.ParamsInt64(":repoid")
if err := models.UpdatePublicKeyUpdated(keyID); err != nil { if err := models.UpdatePublicKeyUpdated(keyID); err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -27,18 +28,18 @@ func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
deployKey, err := models.GetDeployKeyByRepo(keyID, repoID) deployKey, err := models.GetDeployKeyByRepo(keyID, repoID)
if err != nil { if err != nil {
if models.IsErrDeployKeyNotExist(err) { if models.IsErrDeployKeyNotExist(err) {
ctx.PlainText(200, []byte("success")) ctx.PlainText(http.StatusOK, []byte("success"))
return return
} }
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
deployKey.UpdatedUnix = timeutil.TimeStampNow() deployKey.UpdatedUnix = timeutil.TimeStampNow()
if err = models.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil { if err = models.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -53,8 +54,8 @@ func AuthorizedPublicKeyByContent(ctx *context.PrivateContext) {
publicKey, err := models.SearchPublicKeyByContent(content) publicKey, err := models.SearchPublicKeyByContent(content)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }

@ -23,8 +23,8 @@ import (
// It doesn't wait before each message will be processed // It doesn't wait before each message will be processed
func SendEmail(ctx *context.PrivateContext) { func SendEmail(ctx *context.PrivateContext) {
if setting.MailService == nil { if setting.MailService == nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": "Mail service is not enabled.", Err: "Mail service is not enabled.",
}) })
return return
} }
@ -35,8 +35,8 @@ func SendEmail(ctx *context.PrivateContext) {
json := jsoniter.ConfigCompatibleWithStandardLibrary json := jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.NewDecoder(rd).Decode(&mail); err != nil { if err := json.NewDecoder(rd).Decode(&mail); err != nil {
log.Error("%v", err) log.Error("%v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err, Err: err.Error(),
}) })
return return
} }
@ -48,8 +48,8 @@ func SendEmail(ctx *context.PrivateContext) {
if err != nil { if err != nil {
err := fmt.Sprintf("Failed to get user information: %v", err) err := fmt.Sprintf("Failed to get user information: %v", err)
log.Error(err) log.Error(err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err, Err: err,
}) })
return return
} }
@ -68,8 +68,8 @@ func SendEmail(ctx *context.PrivateContext) {
if err != nil { if err != nil {
err := fmt.Sprintf("Failed to find users: %v", err) err := fmt.Sprintf("Failed to find users: %v", err)
log.Error(err) log.Error(err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err, Err: err,
}) })
return return
} }

@ -30,15 +30,15 @@ func FlushQueues(ctx *context.PrivateContext) {
log.Error("Flushing request timed-out with error: %v", err) log.Error("Flushing request timed-out with error: %v", err)
} }
}() }()
ctx.JSON(http.StatusAccepted, map[string]interface{}{ ctx.JSON(http.StatusAccepted, private.Response{
"err": "Flushing", Err: "Flushing",
}) })
return return
} }
err := queue.GetManager().FlushAll(ctx, opts.Timeout) err := queue.GetManager().FlushAll(ctx, opts.Timeout)
if err != nil { if err != nil {
ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{ ctx.JSON(http.StatusRequestTimeout, private.Response{
"err": fmt.Sprintf("%v", err), Err: fmt.Sprintf("%v", err),
}) })
} }
ctx.PlainText(http.StatusOK, []byte("success")) ctx.PlainText(http.StatusOK, []byte("success"))
@ -59,8 +59,8 @@ func ResumeLogging(ctx *context.PrivateContext) {
// ReleaseReopenLogging releases and reopens logging files // ReleaseReopenLogging releases and reopens logging files
func ReleaseReopenLogging(ctx *context.PrivateContext) { func ReleaseReopenLogging(ctx *context.PrivateContext) {
if err := log.ReleaseReopen(); err != nil { if err := log.ReleaseReopen(); err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Error during release and reopen: %v", err), Err: fmt.Sprintf("Error during release and reopen: %v", err),
}) })
return return
} }
@ -73,8 +73,8 @@ func RemoveLogger(ctx *context.PrivateContext) {
name := ctx.Params("name") name := ctx.Params("name")
ok, err := log.GetLogger(group).DelLogger(name) ok, err := log.GetLogger(group).DelLogger(name)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Failed to remove logger: %s %s %v", group, name, err), Err: fmt.Sprintf("Failed to remove logger: %s %s %v", group, name, err),
}) })
return return
} }
@ -134,8 +134,8 @@ func AddLogger(ctx *context.PrivateContext) {
byteConfig, err := json.Marshal(opts.Config) byteConfig, err := json.Marshal(opts.Config)
if err != nil { if err != nil {
log.Error("Failed to marshal log configuration: %v %v", opts.Config, err) log.Error("Failed to marshal log configuration: %v %v", opts.Config, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Failed to marshal log configuration: %v %v", opts.Config, err), Err: fmt.Sprintf("Failed to marshal log configuration: %v %v", opts.Config, err),
}) })
return return
} }
@ -143,8 +143,8 @@ func AddLogger(ctx *context.PrivateContext) {
if err := log.NewNamedLogger(opts.Group, bufferLen, opts.Name, opts.Mode, config); err != nil { if err := log.NewNamedLogger(opts.Group, bufferLen, opts.Name, opts.Mode, config); err != nil {
log.Error("Failed to create new named logger: %s %v", config, err) log.Error("Failed to create new named logger: %s %v", config, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": fmt.Sprintf("Failed to create new named logger: %s %v", config, err), Err: fmt.Sprintf("Failed to create new named logger: %s %v", config, err),
}) })
return return
} }

@ -11,12 +11,13 @@ import (
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/private"
) )
// Restart is not implemented for Windows based servers as they can't fork // Restart is not implemented for Windows based servers as they can't fork
func Restart(ctx *context.PrivateContext) { func Restart(ctx *context.PrivateContext) {
ctx.JSON(http.StatusNotImplemented, map[string]interface{}{ ctx.JSON(http.StatusNotImplemented, private.Response{
"err": "windows servers cannot be gracefully restarted - shutdown and restart manually", Err: "windows servers cannot be gracefully restarted - shutdown and restart manually",
}) })
} }

@ -6,9 +6,11 @@ package private
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
myCtx "code.gitea.io/gitea/modules/context" myCtx "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/migrations"
"code.gitea.io/gitea/modules/private"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
) )
@ -17,8 +19,8 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
json := jsoniter.ConfigCompatibleWithStandardLibrary json := jsoniter.ConfigCompatibleWithStandardLibrary
bs, err := ioutil.ReadAll(ctx.Req.Body) bs, err := ioutil.ReadAll(ctx.Req.Body)
if err != nil { if err != nil {
ctx.JSON(500, map[string]string{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -29,8 +31,8 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
Units []string Units []string
}{} }{}
if err = json.Unmarshal(bs, &params); err != nil { if err = json.Unmarshal(bs, &params); err != nil {
ctx.JSON(500, map[string]string{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -42,10 +44,10 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
params.RepoName, params.RepoName,
params.Units, params.Units,
); err != nil { ); err != nil {
ctx.JSON(500, map[string]string{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
} else { } else {
ctx.Status(200) ctx.Status(http.StatusOK)
} }
} }

@ -23,8 +23,8 @@ import (
func ServNoCommand(ctx *context.PrivateContext) { func ServNoCommand(ctx *context.PrivateContext) {
keyID := ctx.ParamsInt64(":keyid") keyID := ctx.ParamsInt64(":keyid")
if keyID <= 0 { if keyID <= 0 {
ctx.JSON(http.StatusBadRequest, map[string]interface{}{ ctx.JSON(http.StatusBadRequest, private.Response{
"err": fmt.Sprintf("Bad key id: %d", keyID), Err: fmt.Sprintf("Bad key id: %d", keyID),
}) })
} }
results := private.KeyAndOwner{} results := private.KeyAndOwner{}
@ -32,14 +32,14 @@ func ServNoCommand(ctx *context.PrivateContext) {
key, err := models.GetPublicKeyByID(keyID) key, err := models.GetPublicKeyByID(keyID)
if err != nil { if err != nil {
if models.IsErrKeyNotExist(err) { if models.IsErrKeyNotExist(err) {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusUnauthorized, private.Response{
"err": fmt.Sprintf("Cannot find key: %d", keyID), Err: fmt.Sprintf("Cannot find key: %d", keyID),
}) })
return return
} }
log.Error("Unable to get public key: %d Error: %v", keyID, err) log.Error("Unable to get public key: %d Error: %v", keyID, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
@ -49,20 +49,20 @@ func ServNoCommand(ctx *context.PrivateContext) {
user, err := models.GetUserByID(key.OwnerID) user, err := models.GetUserByID(key.OwnerID)
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusUnauthorized, private.Response{
"err": fmt.Sprintf("Cannot find owner with id: %d for key: %d", key.OwnerID, keyID), Err: fmt.Sprintf("Cannot find owner with id: %d for key: %d", key.OwnerID, keyID),
}) })
return return
} }
log.Error("Unable to get owner with id: %d for public key: %d Error: %v", key.OwnerID, keyID, err) log.Error("Unable to get owner with id: %d for public key: %d Error: %v", key.OwnerID, keyID, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.Response{
"err": err.Error(), Err: err.Error(),
}) })
return return
} }
if !user.IsActive || user.ProhibitLogin { if !user.IsActive || user.ProhibitLogin {
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": "Your account is disabled.", Err: "Your account is disabled.",
}) })
return return
} }
@ -106,18 +106,16 @@ func ServCommand(ctx *context.PrivateContext) {
owner, err := models.GetUserByName(results.OwnerName) owner, err := models.GetUserByName(results.OwnerName)
if err != nil { if err != nil {
log.Error("Unable to get repository owner: %s/%s Error: %v", results.OwnerName, results.RepoName, err) log.Error("Unable to get repository owner: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Unable to get repository owner: %s/%s %v", results.OwnerName, results.RepoName, err),
"err": fmt.Sprintf("Unable to get repository owner: %s/%s %v", results.OwnerName, results.RepoName, err),
}) })
return return
} }
if !owner.IsOrganization() && !owner.IsActive { if !owner.IsOrganization() && !owner.IsActive {
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.ErrServCommand{
"results": results, Results: results,
"type": "ForbiddenError", Err: "Repository cannot be accessed, you could retry it later",
"err": "Repository cannot be accessed, you could retry it later",
}) })
return return
} }
@ -132,20 +130,18 @@ func ServCommand(ctx *context.PrivateContext) {
if "git-upload-pack" == verb { if "git-upload-pack" == verb {
// User is fetching/cloning a non-existent repository // User is fetching/cloning a non-existent repository
log.Error("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr()) log.Error("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr())
ctx.JSON(http.StatusNotFound, map[string]interface{}{ ctx.JSON(http.StatusNotFound, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrRepoNotExist", Err: fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
}) })
return return
} }
} }
} else { } else {
log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err) log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Unable to get repository: %s/%s %v", results.OwnerName, results.RepoName, err),
"err": fmt.Sprintf("Unable to get repository: %s/%s %v", results.OwnerName, results.RepoName, err),
}) })
return return
} }
@ -157,20 +153,18 @@ func ServCommand(ctx *context.PrivateContext) {
results.RepoID = repo.ID results.RepoID = repo.ID
if repo.IsBeingCreated() { if repo.IsBeingCreated() {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: "Repository is being created, you could retry after it finished",
"err": "Repository is being created, you could retry after it finished",
}) })
return return
} }
// We can shortcut at this point if the repo is a mirror // We can shortcut at this point if the repo is a mirror
if mode > models.AccessModeRead && repo.IsMirror { if mode > models.AccessModeRead && repo.IsMirror {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrMirrorReadOnly", Err: fmt.Sprintf("Mirror Repository %s/%s is read-only", results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Mirror Repository %s/%s is read-only", results.OwnerName, results.RepoName),
}) })
return return
} }
@ -180,18 +174,16 @@ func ServCommand(ctx *context.PrivateContext) {
key, err := models.GetPublicKeyByID(keyID) key, err := models.GetPublicKeyByID(keyID)
if err != nil { if err != nil {
if models.IsErrKeyNotExist(err) { if models.IsErrKeyNotExist(err) {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusNotFound, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrKeyNotExist", Err: fmt.Sprintf("Cannot find key: %d", keyID),
"err": fmt.Sprintf("Cannot find key: %d", keyID),
}) })
return return
} }
log.Error("Unable to get public key: %d Error: %v", keyID, err) log.Error("Unable to get public key: %d Error: %v", keyID, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Unable to get key: %d Error: %v", keyID, err),
"err": fmt.Sprintf("Unable to get key: %d Error: %v", keyID, err),
}) })
return return
} }
@ -201,10 +193,9 @@ func ServCommand(ctx *context.PrivateContext) {
// If repo doesn't exist, deploy key doesn't make sense // If repo doesn't exist, deploy key doesn't make sense
if !repoExist && key.Type == models.KeyTypeDeploy { if !repoExist && key.Type == models.KeyTypeDeploy {
ctx.JSON(http.StatusNotFound, map[string]interface{}{ ctx.JSON(http.StatusNotFound, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrRepoNotExist", Err: fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
}) })
return return
} }
@ -221,18 +212,16 @@ func ServCommand(ctx *context.PrivateContext) {
deployKey, err = models.GetDeployKeyByRepo(key.ID, repo.ID) deployKey, err = models.GetDeployKeyByRepo(key.ID, repo.ID)
if err != nil { if err != nil {
if models.IsErrDeployKeyNotExist(err) { if models.IsErrDeployKeyNotExist(err) {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusNotFound, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrDeployKeyNotExist", Err: fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
}) })
return return
} }
log.Error("Unable to get deploy for public (deploy) key: %d in %-v Error: %v", key.ID, repo, err) log.Error("Unable to get deploy for public (deploy) key: %d in %-v Error: %v", key.ID, repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Unable to get Deploy Key for Public Key: %d:%s in %s/%s.", key.ID, key.Name, results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Unable to get Deploy Key for Public Key: %d:%s in %s/%s.", key.ID, key.Name, results.OwnerName, results.RepoName),
}) })
return return
} }
@ -252,25 +241,23 @@ func ServCommand(ctx *context.PrivateContext) {
user, err = models.GetUserByID(key.OwnerID) user, err = models.GetUserByID(key.OwnerID)
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusUnauthorized, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrUserNotExist", Err: fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID),
"err": fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID),
}) })
return return
} }
log.Error("Unable to get owner: %d for public key: %d:%s Error: %v", key.OwnerID, key.ID, key.Name, err) log.Error("Unable to get owner: %d for public key: %d:%s Error: %v", key.OwnerID, key.ID, key.Name, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Unable to get Owner: %d for Deploy Key: %d:%s in %s/%s.", key.OwnerID, key.ID, key.Name, ownerName, repoName),
"err": fmt.Sprintf("Unable to get Owner: %d for Deploy Key: %d:%s in %s/%s.", key.OwnerID, key.ID, key.Name, ownerName, repoName),
}) })
return return
} }
if !user.IsActive || user.ProhibitLogin { if !user.IsActive || user.ProhibitLogin {
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.Response{
"err": "Your account is disabled.", Err: "Your account is disabled.",
}) })
return return
} }
@ -283,10 +270,9 @@ func ServCommand(ctx *context.PrivateContext) {
// Don't allow pushing if the repo is archived // Don't allow pushing if the repo is archived
if repoExist && mode > models.AccessModeRead && repo.IsArchived { if repoExist && mode > models.AccessModeRead && repo.IsArchived {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusUnauthorized, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrRepoIsArchived", Err: fmt.Sprintf("Repo: %s/%s is archived.", results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Repo: %s/%s is archived.", results.OwnerName, results.RepoName),
}) })
return return
} }
@ -295,10 +281,9 @@ func ServCommand(ctx *context.PrivateContext) {
if repoExist && (mode > models.AccessModeRead || repo.IsPrivate || setting.Service.RequireSignInView) { if repoExist && (mode > models.AccessModeRead || repo.IsPrivate || setting.Service.RequireSignInView) {
if key.Type == models.KeyTypeDeploy { if key.Type == models.KeyTypeDeploy {
if deployKey.Mode < mode { if deployKey.Mode < mode {
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusUnauthorized, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrUnauthorized", Err: fmt.Sprintf("Deploy Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Deploy Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
}) })
return return
} }
@ -306,10 +291,9 @@ func ServCommand(ctx *context.PrivateContext) {
perm, err := models.GetUserRepoPermission(repo, user) perm, err := models.GetUserRepoPermission(repo, user)
if err != nil { if err != nil {
log.Error("Unable to get permissions for %-v with key %d in %-v Error: %v", user, key.ID, repo, err) log.Error("Unable to get permissions for %-v with key %d in %-v Error: %v", user, key.ID, repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Unable to get permissions for user %d:%s with key %d in %s/%s Error: %v", user.ID, user.Name, key.ID, results.OwnerName, results.RepoName, err),
"err": fmt.Sprintf("Unable to get permissions for user %d:%s with key %d in %s/%s Error: %v", user.ID, user.Name, key.ID, results.OwnerName, results.RepoName, err),
}) })
return return
} }
@ -318,10 +302,9 @@ func ServCommand(ctx *context.PrivateContext) {
if userMode < mode { if userMode < mode {
log.Error("Failed authentication attempt for %s with key %s (not authorized to %s %s/%s) from %s", user.Name, key.Name, modeString, ownerName, repoName, ctx.RemoteAddr()) log.Error("Failed authentication attempt for %s with key %s (not authorized to %s %s/%s) from %s", user.Name, key.Name, modeString, ownerName, repoName, ctx.RemoteAddr())
ctx.JSON(http.StatusUnauthorized, map[string]interface{}{ ctx.JSON(http.StatusUnauthorized, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrUnauthorized", Err: fmt.Sprintf("User: %d:%s with Key: %d:%s is not authorized to %s %s/%s.", user.ID, user.Name, key.ID, key.Name, modeString, ownerName, repoName),
"err": fmt.Sprintf("User: %d:%s with Key: %d:%s is not authorized to %s %s/%s.", user.ID, user.Name, key.ID, key.Name, modeString, ownerName, repoName),
}) })
return return
} }
@ -332,27 +315,24 @@ func ServCommand(ctx *context.PrivateContext) {
if !repoExist { if !repoExist {
owner, err := models.GetUserByName(ownerName) owner, err := models.GetUserByName(ownerName)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Unable to get owner: %s %v", results.OwnerName, err),
"err": fmt.Sprintf("Unable to get owner: %s %v", results.OwnerName, err),
}) })
return return
} }
if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg { if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrForbidden", Err: "Push to create is not enabled for organizations.",
"err": "Push to create is not enabled for organizations.",
}) })
return return
} }
if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser { if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrForbidden", Err: "Push to create is not enabled for users.",
"err": "Push to create is not enabled for users.",
}) })
return return
} }
@ -360,10 +340,9 @@ func ServCommand(ctx *context.PrivateContext) {
repo, err = repo_service.PushCreateRepo(user, owner, results.RepoName) repo, err = repo_service.PushCreateRepo(user, owner, results.RepoName)
if err != nil { if err != nil {
log.Error("pushCreateRepo: %v", err) log.Error("pushCreateRepo: %v", err)
ctx.JSON(http.StatusNotFound, map[string]interface{}{ ctx.JSON(http.StatusNotFound, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrRepoNotExist", Err: fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
"err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
}) })
return return
} }
@ -374,18 +353,16 @@ func ServCommand(ctx *context.PrivateContext) {
// Ensure the wiki is enabled before we allow access to it // Ensure the wiki is enabled before we allow access to it
if _, err := repo.GetUnit(models.UnitTypeWiki); err != nil { if _, err := repo.GetUnit(models.UnitTypeWiki); err != nil {
if models.IsErrUnitTypeNotExist(err) { if models.IsErrUnitTypeNotExist(err) {
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(http.StatusForbidden, private.ErrServCommand{
"results": results, Results: results,
"type": "ErrForbidden", Err: "repository wiki is disabled",
"err": "repository wiki is disabled",
}) })
return return
} }
log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err) log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Failed to get the wiki unit in %s/%s Error: %v", ownerName, repoName, err),
"err": fmt.Sprintf("Failed to get the wiki unit in %s/%s Error: %v", ownerName, repoName, err),
}) })
return return
} }
@ -393,10 +370,9 @@ func ServCommand(ctx *context.PrivateContext) {
// Finally if we're trying to touch the wiki we should init it // Finally if we're trying to touch the wiki we should init it
if err = wiki_service.InitWiki(repo); err != nil { if err = wiki_service.InitWiki(repo); err != nil {
log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err) log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
"results": results, Results: results,
"type": "InternalServerError", Err: fmt.Sprintf("Failed to initialize the wiki in %s/%s Error: %v", ownerName, repoName, err),
"err": fmt.Sprintf("Failed to initialize the wiki in %s/%s Error: %v", ownerName, repoName, err),
}) })
return return
} }

@ -1461,7 +1461,7 @@ func ViewIssue(ctx *context.Context) {
} }
if perm.CanWrite(models.UnitTypeCode) { if perm.CanWrite(models.UnitTypeCode) {
// Check if branch is not protected // Check if branch is not protected
if protected, err := pull.HeadRepo.IsProtectedBranch(pull.HeadBranch, ctx.User); err != nil { if protected, err := pull.HeadRepo.IsProtectedBranch(pull.HeadBranch); err != nil {
log.Error("IsProtectedBranch: %v", err) log.Error("IsProtectedBranch: %v", err)
} else if !protected { } else if !protected {
canDelete = true canDelete = true

@ -26,7 +26,7 @@ func DeleteBranch(doer *models.User, repo *models.Repository, gitRepo *git.Repos
return ErrBranchIsDefault return ErrBranchIsDefault
} }
isProtected, err := repo.IsProtectedBranch(branchName, doer) isProtected, err := repo.IsProtectedBranch(branchName)
if err != nil { if err != nil {
return err return err
} }

Loading…
Cancel
Save