aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/hook.go
diff options
context:
space:
mode:
authorJulien Tant <julien@craftyx.fr>2018-10-20 08:59:06 +0200
committerLauris BH <lauris@nix.lv>2018-10-20 09:59:06 +0300
commitdea3d849e1e21ccda6f272591cadcb6d1d338b56 (patch)
tree14302df694d167582f19fa0d0a1ae5a65cfbab98 /cmd/hook.go
parentcabdf84f1f5e55e8b449bbd3b4d4ba0c69a87c87 (diff)
downloadgitea-dea3d849e1e21ccda6f272591cadcb6d1d338b56.tar.gz
gitea-dea3d849e1e21ccda6f272591cadcb6d1d338b56.zip
Give user a link to create PR after push (#4716)
* Give user a link to create PR after push * Forks now create PR in the base repository + make sure PR creation is allowed * fix code style
Diffstat (limited to 'cmd/hook.go')
-rw-r--r--cmd/hook.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/cmd/hook.go b/cmd/hook.go
index 02eb30a13b..70849f5142 100644
--- a/cmd/hook.go
+++ b/cmd/hook.go
@@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"fmt"
+ "net/url"
"os"
"path/filepath"
"strconv"
@@ -174,6 +175,7 @@ func runHookPostReceive(c *cli.Context) error {
hookSetup("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)
@@ -211,6 +213,47 @@ func runHookPostReceive(c *cli.Context) error {
}); err != nil {
log.GitLogger.Error(2, "Update: %v", err)
}
+
+ if strings.HasPrefix(refFullName, git.BranchPrefix) {
+ branch := strings.TrimPrefix(refFullName, git.BranchPrefix)
+ repo, pullRequestAllowed, err := private.GetRepository(repoID)
+ if err != nil {
+ log.GitLogger.Error(2, "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(2, "get active pr: %v", err)
+ break
+ }
+
+ 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(), url.QueryEscape(baseRepo.DefaultBranch), url.QueryEscape(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, "")
+ }
+
}
return nil