summaryrefslogtreecommitdiffstats
path: root/cmd/hook.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-06-01 16:00:21 +0100
committerLunny Xiao <xiaolunwen@gmail.com>2019-06-01 23:00:21 +0800
commit356854fc5f8d7d1a7e4d68c9e00929e9ce8aa867 (patch)
treebc250740ffe65de5cd9ce3389e004ca7723d5643 /cmd/hook.go
parent8a343dda39b187627db6ffb4c24a6e0ae615867b (diff)
downloadgitea-356854fc5f8d7d1a7e4d68c9e00929e9ce8aa867.tar.gz
gitea-356854fc5f8d7d1a7e4d68c9e00929e9ce8aa867.zip
Move serv hook functionality & drop GitLogger (#6993)
* Move hook functionality internally * Internalise serv logic * Remove old internal paths * finally remove the gitlogger * Disallow push on archived repositories * fix lint error * Update modules/private/key.go * Update routers/private/hook.go * Update routers/private/hook.go * Update routers/private/hook.go * Updated routers/private/serv.go * Fix LFS Locks over SSH * rev-list needs to be run by the hook process * fixup * Improve git test * Ensure that the lfs files are created with a different prefix * Reduce the replication in git_test.go * slight refactor * Remove unnecessary "/" * Restore ensureAnonymousClone * Restore ensureAnonymousClone * Run rev-list on server side * Try passing in the alternative directories instead * Mark test as skipped * Improve git test * Ensure that the lfs files are created with a different prefix * Reduce the replication in git_test.go * Remove unnecessary "/"
Diffstat (limited to 'cmd/hook.go')
-rw-r--r--cmd/hook.go125
1 files changed, 36 insertions, 89 deletions
diff --git a/cmd/hook.go b/cmd/hook.go
index f8bd34c4e9..b3e900afee 100644
--- a/cmd/hook.go
+++ b/cmd/hook.go
@@ -8,15 +8,14 @@ import (
"bufio"
"bytes"
"fmt"
+ "net/http"
"os"
"strconv"
"strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
- "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/private"
- "code.gitea.io/gitea/modules/util"
"github.com/urfave/cli"
)
@@ -62,12 +61,10 @@ func runHookPreReceive(c *cli.Context) error {
setup("hooks/pre-receive.log")
// the environment setted on serv command
- repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
username := os.Getenv(models.EnvRepoUsername)
reponame := os.Getenv(models.EnvRepoName)
- userIDStr := os.Getenv(models.EnvPusherID)
- repoPath := models.RepoPath(username, reponame)
+ userID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
buf := bytes.NewBuffer(nil)
scanner := bufio.NewScanner(os.Stdin)
@@ -91,35 +88,19 @@ func runHookPreReceive(c *cli.Context) error {
// If the ref is a branch, check if it's protected
if strings.HasPrefix(refFullName, git.BranchPrefix) {
- branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
- protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
- if err != nil {
- fail("Internal error", fmt.Sprintf("retrieve protected branches information failed: %v", err))
- }
-
- if protectBranch != nil && protectBranch.IsProtected() {
- // check and deletion
- if newCommitID == git.EmptySHA {
- fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
- }
-
- // detect force push
- if git.EmptySHA != oldCommitID {
- output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
- if err != nil {
- fail("Internal error", "Fail to detect force push: %v", err)
- } else if len(output) > 0 {
- fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
- }
- }
-
- userID, _ := strconv.ParseInt(userIDStr, 10, 64)
- canPush, err := private.CanUserPush(protectBranch.ID, userID)
- if err != nil {
- fail("Internal error", "Fail to detect user can push: %v", err)
- } else if !canPush {
- fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
- }
+ statusCode, msg := private.HookPreReceive(username, reponame, private.HookOptions{
+ OldCommitID: oldCommitID,
+ NewCommitID: newCommitID,
+ RefFullName: refFullName,
+ UserID: userID,
+ GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories),
+ GitObjectDirectory: os.Getenv(private.GitObjectDirectory),
+ })
+ switch statusCode {
+ case http.StatusInternalServerError:
+ fail("Internal Server Error", msg)
+ case http.StatusForbidden:
+ fail(msg, "")
}
}
}
@@ -145,7 +126,6 @@ func runHookPostReceive(c *cli.Context) error {
setup("hooks/post-receive.log")
// the environment setted on serv command
- repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
repoUser := os.Getenv(models.EnvRepoUsername)
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
repoName := os.Getenv(models.EnvRepoName)
@@ -172,64 +152,31 @@ func runHookPostReceive(c *cli.Context) error {
newCommitID := string(fields[1])
refFullName := string(fields[2])
- // Only trigger activity updates for changes to branches or
- // tags. Updates to other refs (eg, refs/notes, refs/changes,
- // or other less-standard refs spaces are ignored since there
- // may be a very large number of them).
- if strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
- if err := private.PushUpdate(models.PushUpdateOptions{
- RefFullName: refFullName,
- OldCommitID: oldCommitID,
- NewCommitID: newCommitID,
- PusherID: pusherID,
- PusherName: pusherName,
- RepoUserName: repoUser,
- RepoName: repoName,
- }); err != nil {
- log.GitLogger.Error("Update: %v", err)
- }
- }
-
- if newCommitID != git.EmptySHA && strings.HasPrefix(refFullName, git.BranchPrefix) {
- branch := strings.TrimPrefix(refFullName, git.BranchPrefix)
- repo, pullRequestAllowed, err := private.GetRepository(repoID)
- if err != nil {
- log.GitLogger.Error("get repo: %v", err)
- break
- }
- if !pullRequestAllowed {
- break
- }
-
- baseRepo := repo
- if repo.IsFork {
- baseRepo = repo.BaseRepo
- }
-
- if !repo.IsFork && branch == baseRepo.DefaultBranch {
- break
- }
+ res, err := private.HookPostReceive(repoUser, repoName, private.HookOptions{
+ OldCommitID: oldCommitID,
+ NewCommitID: newCommitID,
+ RefFullName: refFullName,
+ UserID: pusherID,
+ UserName: pusherName,
+ })
- pr, err := private.ActivePullRequest(baseRepo.ID, repo.ID, baseRepo.DefaultBranch, branch)
- if err != nil {
- log.GitLogger.Error("get active pr: %v", err)
- break
- }
+ if res == nil {
+ fail("Internal Server Error", err)
+ }
- fmt.Fprintln(os.Stderr, "")
- if pr == nil {
- if repo.IsFork {
- branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch)
- }
- fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", branch)
- fmt.Fprintf(os.Stderr, " %s/compare/%s...%s\n", baseRepo.HTMLURL(), util.PathEscapeSegments(baseRepo.DefaultBranch), util.PathEscapeSegments(branch))
- } else {
- fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
- fmt.Fprintf(os.Stderr, " %s/pulls/%d\n", baseRepo.HTMLURL(), pr.Index)
- }
- fmt.Fprintln(os.Stderr, "")
+ if res["message"] == false {
+ continue
}
+ fmt.Fprintln(os.Stderr, "")
+ if res["create"] == true {
+ fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", res["branch"])
+ fmt.Fprintf(os.Stderr, " %s\n", res["url"])
+ } else {
+ fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
+ fmt.Fprintf(os.Stderr, " %s\n", res["url"])
+ }
+ fmt.Fprintln(os.Stderr, "")
}
return nil