Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move serv hook functionality & drop GitLogger #6993

Merged
merged 34 commits into from Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d653811
Move hook functionality internally
zeripath May 17, 2019
04f5081
Internalise serv logic
zeripath May 19, 2019
997f16d
Remove old internal paths
zeripath May 19, 2019
8ff6618
finally remove the gitlogger
zeripath May 19, 2019
9cc4043
Disallow push on archived repositories
zeripath May 19, 2019
f26ac93
fix lint error
zeripath May 19, 2019
57c9f07
Update modules/private/key.go
zeripath May 20, 2019
bdce608
Update routers/private/hook.go
zeripath May 20, 2019
02cba7f
Update routers/private/hook.go
zeripath May 20, 2019
ee7f792
Update routers/private/hook.go
zeripath May 20, 2019
f6659b3
Updated routers/private/serv.go
zeripath May 20, 2019
f6d7623
Fix LFS Locks over SSH
zeripath May 20, 2019
cb9f80a
rev-list needs to be run by the hook process
zeripath May 20, 2019
1b58ab4
fixup
zeripath May 20, 2019
2fe133f
Improve git test
zeripath May 20, 2019
626695e
Merge branch 'move-serv-hook-functionality' of github.com:zeripath/gi…
zeripath May 20, 2019
f2555e4
Merge branch 'master' into move-serv-hook-functionality
zeripath May 20, 2019
b2d10b9
Ensure that the lfs files are created with a different prefix
zeripath May 21, 2019
4a96783
Reduce the replication in git_test.go
zeripath May 21, 2019
8f4ab6f
slight refactor
zeripath May 21, 2019
4863df6
Remove unnecessary "/"
zeripath May 21, 2019
17ca973
Restore ensureAnonymousClone
zeripath May 21, 2019
2ca27f3
Restore ensureAnonymousClone
zeripath May 21, 2019
86f98cd
Run rev-list on server side
zeripath May 21, 2019
b1aebfe
Try passing in the alternative directories instead
zeripath May 21, 2019
5209e94
Mark test as skipped
zeripath May 21, 2019
cf857fa
Merge branch 'master' into move-serv-hook-functionality
zeripath May 21, 2019
9b5ccb8
Merge branch 'master' into move-serv-hook-functionality
zeripath May 28, 2019
aaa7e5a
Merge branch 'master' into move-serv-hook-functionality
zeripath May 28, 2019
8dbb91a
Improve git test
zeripath May 20, 2019
f5ee78a
Merge branch 'master' into move-serv-hook-functionality
zeripath May 30, 2019
9e7e0ac
Merge branch 'refactor-git-test' into move-serv-hook-functionality
zeripath May 30, 2019
3d3635f
Merge branch 'master' into move-serv-hook-functionality
zeripath May 31, 2019
c09a5f3
Merge branch 'master' into move-serv-hook-functionality
lafriks Jun 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
130 changes: 41 additions & 89 deletions cmd/hook.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -89,37 +86,26 @@ func runHookPreReceive(c *cli.Context) error {
newCommitID := string(fields[1])
refFullName := string(fields[2])

output, errErr := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).Run()
zeripath marked this conversation as resolved.
Show resolved Hide resolved
err := ""
if errErr != nil {
err = errErr.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,
Output: output,
Err: err,
})
switch statusCode {
case http.StatusInternalServerError:
fail("Internal Server Error", msg)
case http.StatusForbidden:
fail(msg, "")
}
}
}
Expand All @@ -145,7 +131,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)
Expand All @@ -172,64 +157,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)
}
}
res, err := private.HookPostReceive(repoUser, repoName, private.HookOptions{
OldCommitID: oldCommitID,
NewCommitID: newCommitID,
RefFullName: refFullName,
UserID: pusherID,
UserName: pusherName,
})

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
}

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
Expand Down