From 20403f75fbf043747eb0c9e571a8ff7c4594ad2a Mon Sep 17 00:00:00 2001 From: Martin Hartkorn Date: Sat, 30 Jan 2016 23:56:38 +0100 Subject: [PATCH 1/3] Enable a way to checkout Pull Requests from remote refs --- cmd/pull.go | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ gogs.go | 1 + models/pull.go | 39 +++++++++++++++++++ models/repo.go | 19 ++++++++++ 4 files changed, 159 insertions(+) create mode 100644 cmd/pull.go diff --git a/cmd/pull.go b/cmd/pull.go new file mode 100644 index 0000000000..cf5016eb54 --- /dev/null +++ b/cmd/pull.go @@ -0,0 +1,100 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "bufio" + "log" + "os" + "strconv" + "strings" + + "github.com/codegangsta/cli" + "github.com/gogits/git-module" + "github.com/gogits/gogs/models" +) + +var CmdPull = cli.Command{ + Name: "pull", + Usage: "Filter commits for Pull Request actions", + Description: `Checks commits for potential pull request updates and takes appropriate actions.`, + Action: runPull, + Flags: []cli.Flag{ + stringFlag("path, p", "", "repository path"), + }, +} + +func runPull(ctx *cli.Context) { + setup("pull.log") + + if !ctx.IsSet("path") { + log.Fatal("Missing argument --path") + } + + workingDirectory := ctx.String("path") + + // Scan standard input (stdin) for updated refs + stdin := bufio.NewScanner(os.Stdin) + for stdin.Scan() { + // Format from post-receive is: + args := strings.Split(stdin.Text(), " ") + if len(args) < 3 { + continue + } + + refName := args[2] + refSplits := strings.Split(refName, "/") + + if len(refSplits) < 3 { + log.Fatal("Not enough elements in refs element.") + } + + // if refSplits[1] == "pull" { + // log.Fatal("Not allowed to push to \"pull\" refs. Reserved for Pull Requests.") + // } else + if refSplits[1] != "heads" { + // Only push branches of ref "heads" + continue + } + + branch := strings.Join(refSplits[2:], "/") + + repoPathSplits := strings.Split(workingDirectory, string(os.PathSeparator)) + userName := repoPathSplits[len(repoPathSplits)-2] + repoName := repoPathSplits[len(repoPathSplits)-1] + repoName = repoName[0 : len(repoName)-4] + + pr, err := models.GetUnmergedPullRequestByRepoPathAndHeadBranch(userName, repoName, branch) + if _, ok := err.(models.ErrPullRequestNotExist); ok { + // Nothing to do here if the branch has no Pull Request open + log.Printf("Skipping for %s/%s.git branch '%s'", userName, repoName, branch) + continue + } else if err != nil { + log.Fatal("Database operation failed: " + err.Error()) + } + + err = pr.BaseRepo.GetOwner() + if err != nil { + log.Fatal("Could not get owner data: " + err.Error()) + } + + prIdStr := strconv.FormatInt(pr.ID, 10) + tmpRemoteName := "tmp-pull-" + branch + "-" + prIdStr + remoteUrl := "../../" + pr.BaseRepo.Owner.LowerName + "/" + pr.BaseRepo.LowerName + ".git" + repo, err := git.OpenRepository(workingDirectory) + repo.AddRemote(tmpRemoteName, remoteUrl, false) + + err = git.Push(workingDirectory, tmpRemoteName, branch+":"+"refs/pull/"+prIdStr+"/head") + if err != nil { + log.Fatal("Error pushing: " + err.Error()) + } + + err = repo.RemoveRemote(tmpRemoteName) + + if err != nil { + log.Fatal("Error deleting temporary remote: " + err.Error()) + } + } +} diff --git a/gogs.go b/gogs.go index 01bf158290..66fc90554c 100644 --- a/gogs.go +++ b/gogs.go @@ -35,6 +35,7 @@ func main() { cmd.CmdUpdate, cmd.CmdDump, cmd.CmdCert, + cmd.CmdPull, } app.Flags = append(app.Flags, []cli.Flag{}...) app.Run(os.Args) diff --git a/models/pull.go b/models/pull.go index 47d80473a0..3935c13010 100644 --- a/models/pull.go +++ b/models/pull.go @@ -5,6 +5,7 @@ package models import ( + "errors" "fmt" "os" "path" @@ -395,6 +396,44 @@ func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequ Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs) } +// Gets a Pull Request by the path of the forked repo and the branch from where the PR +// got submitted. +func GetUnmergedPullRequestByRepoPathAndHeadBranch(user, repo, branch string) (*PullRequest, error) { + userLower := strings.ToLower(user) + repoLower := strings.ToLower(repo) + + pr := new(PullRequest) + if x == nil { + return nil, errors.New("Fail") + } + has, err := x. + Where("head_user_name=? AND head_branch=? AND has_merged=? AND issue.is_closed=? AND repository.lower_name=?", userLower, branch, 0, 0, repoLower). + Join("INNER", "repository", "repository.id=pull_request.head_repo_id"). + Join("INNER", "issue", "issue.id=pull_request.issue_id"). + Get(pr) + + if err != nil { + return nil, err + } else if !has { + return nil, ErrPullRequestNotExist{0, 0, 0, 0, branch, ""} + } + + baseRepo := new(Repository) + has, err = x.Where("repository.id=?", pr.BaseRepoID). + Join("LEFT", "user", "user.id=repository.owner_id"). + Get(baseRepo) + + if err != nil { + return nil, err + } else if !has { + return nil, ErrRepoNotExist{pr.BaseRepoID, 0, ""} + } + + pr.BaseRepo = baseRepo + + return pr, nil +} + // GetPullRequestByID returns a pull request by given ID. func GetPullRequestByID(id int64) (*PullRequest, error) { pr := new(PullRequest) diff --git a/models/repo.go b/models/repo.go index 8ce1f7190c..4d45b744ff 100644 --- a/models/repo.go +++ b/models/repo.go @@ -39,6 +39,7 @@ import ( const ( _TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3 --config='%s'\n" + _TPL_POST_RECEIVE_HOOK = "#!/usr/bin/env %s\n%s pull --path \"$(pwd)\"\n" ) var ( @@ -596,6 +597,11 @@ func createUpdateHook(repoPath string) error { fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"", setting.CustomConf)) } +func createPostReceiveHook(repoPath string) error { + return git.SetPostReceiveHook(repoPath, + fmt.Sprintf(_TPL_POST_RECEIVE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"")) +} + type MigrateRepoOptions struct { Name string Description string @@ -818,6 +824,8 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C return fmt.Errorf("InitRepository: %v", err) } else if err = createUpdateHook(repoPath); err != nil { return fmt.Errorf("createUpdateHook: %v", err) + } else if err = createPostReceiveHook(repoPath); err != nil { + return fmt.Errorf("createPostReceiveHook: %v", err) } tmpDir := filepath.Join(os.TempDir(), "gogs-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond())) @@ -1498,6 +1506,15 @@ func RewriteRepositoryUpdateHook() error { }) } +// RewriteRepositoryPostReceiveHook rewrites all repositories' update hook. +func RewriteRepositoryPostReceiveHook() error { + return x.Where("id > 0").Iterate(new(Repository), + func(idx int, bean interface{}) error { + repo := bean.(*Repository) + return createPostReceiveHook(repo.RepoPath()) + }) +} + // statusPool represents a pool of status with true/false. type statusPool struct { lock sync.RWMutex @@ -2045,6 +2062,8 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit if err = createUpdateHook(repoPath); err != nil { return nil, fmt.Errorf("createUpdateHook: %v", err) + } else if err = createPostReceiveHook(repoPath); err != nil { + return nil, fmt.Errorf("createPostReceiveHook: %v", err) } return repo, sess.Commit() From d91004ee711c1db77ec3d5fd3aa823aab8c494f6 Mon Sep 17 00:00:00 2001 From: Martin Hartkorn Date: Thu, 4 Feb 2016 19:00:42 +0100 Subject: [PATCH 2/3] Removed dependency on post-receive hook and use TriggerTask instead --- cmd/pull.go | 100 ------------------------------------------------- gogs.go | 1 - models/pull.go | 38 +++++++++++++++++++ models/repo.go | 19 ---------- 4 files changed, 38 insertions(+), 120 deletions(-) delete mode 100644 cmd/pull.go diff --git a/cmd/pull.go b/cmd/pull.go deleted file mode 100644 index cf5016eb54..0000000000 --- a/cmd/pull.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package cmd - -import ( - "bufio" - "log" - "os" - "strconv" - "strings" - - "github.com/codegangsta/cli" - "github.com/gogits/git-module" - "github.com/gogits/gogs/models" -) - -var CmdPull = cli.Command{ - Name: "pull", - Usage: "Filter commits for Pull Request actions", - Description: `Checks commits for potential pull request updates and takes appropriate actions.`, - Action: runPull, - Flags: []cli.Flag{ - stringFlag("path, p", "", "repository path"), - }, -} - -func runPull(ctx *cli.Context) { - setup("pull.log") - - if !ctx.IsSet("path") { - log.Fatal("Missing argument --path") - } - - workingDirectory := ctx.String("path") - - // Scan standard input (stdin) for updated refs - stdin := bufio.NewScanner(os.Stdin) - for stdin.Scan() { - // Format from post-receive is: - args := strings.Split(stdin.Text(), " ") - if len(args) < 3 { - continue - } - - refName := args[2] - refSplits := strings.Split(refName, "/") - - if len(refSplits) < 3 { - log.Fatal("Not enough elements in refs element.") - } - - // if refSplits[1] == "pull" { - // log.Fatal("Not allowed to push to \"pull\" refs. Reserved for Pull Requests.") - // } else - if refSplits[1] != "heads" { - // Only push branches of ref "heads" - continue - } - - branch := strings.Join(refSplits[2:], "/") - - repoPathSplits := strings.Split(workingDirectory, string(os.PathSeparator)) - userName := repoPathSplits[len(repoPathSplits)-2] - repoName := repoPathSplits[len(repoPathSplits)-1] - repoName = repoName[0 : len(repoName)-4] - - pr, err := models.GetUnmergedPullRequestByRepoPathAndHeadBranch(userName, repoName, branch) - if _, ok := err.(models.ErrPullRequestNotExist); ok { - // Nothing to do here if the branch has no Pull Request open - log.Printf("Skipping for %s/%s.git branch '%s'", userName, repoName, branch) - continue - } else if err != nil { - log.Fatal("Database operation failed: " + err.Error()) - } - - err = pr.BaseRepo.GetOwner() - if err != nil { - log.Fatal("Could not get owner data: " + err.Error()) - } - - prIdStr := strconv.FormatInt(pr.ID, 10) - tmpRemoteName := "tmp-pull-" + branch + "-" + prIdStr - remoteUrl := "../../" + pr.BaseRepo.Owner.LowerName + "/" + pr.BaseRepo.LowerName + ".git" - repo, err := git.OpenRepository(workingDirectory) - repo.AddRemote(tmpRemoteName, remoteUrl, false) - - err = git.Push(workingDirectory, tmpRemoteName, branch+":"+"refs/pull/"+prIdStr+"/head") - if err != nil { - log.Fatal("Error pushing: " + err.Error()) - } - - err = repo.RemoveRemote(tmpRemoteName) - - if err != nil { - log.Fatal("Error deleting temporary remote: " + err.Error()) - } - } -} diff --git a/gogs.go b/gogs.go index 66fc90554c..01bf158290 100644 --- a/gogs.go +++ b/gogs.go @@ -35,7 +35,6 @@ func main() { cmd.CmdUpdate, cmd.CmdDump, cmd.CmdCert, - cmd.CmdPull, } app.Flags = append(app.Flags, []cli.Flag{}...) app.Run(os.Args) diff --git a/models/pull.go b/models/pull.go index 3935c13010..a6c15c1322 100644 --- a/models/pull.go +++ b/models/pull.go @@ -21,6 +21,7 @@ import ( "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/setting" + "strconv" ) type PullRequestType int @@ -520,6 +521,40 @@ func (pr *PullRequest) UpdatePatch() (err error) { return nil } +func (pr *PullRequest) PushToBaseRepo() (err error) { + log.Trace("PushToBase[%d]: pushing commits to base repo refs/pull/%d/head", pr.ID, pr.ID) + + branch := pr.HeadBranch + + if err = pr.BaseRepo.GetOwner(); err != nil { + return fmt.Errorf("Could not get base repo owner data: %v", err) + } else if err = pr.HeadRepo.GetOwner(); err != nil { + return fmt.Errorf("Could not get head repo owner data: %v", err) + } + + headRepoPath := RepoPath(pr.HeadRepo.Owner.Name, pr.HeadRepo.Name) + + prIdStr := strconv.FormatInt(pr.ID, 10) + tmpRemoteName := "tmp-pull-" + branch + "-" + prIdStr + repo, err := git.OpenRepository(headRepoPath) + if err != nil { + return fmt.Errorf("Unable to open head repository: %v", err) + } + + if err = repo.AddRemote(tmpRemoteName, RepoPath(pr.BaseRepo.Owner.Name, pr.BaseRepo.Name), false); err != nil { + return fmt.Errorf("Unable to add remote to head repository: %v", err) + } + // Make sure to remove the remote even if the push fails + defer repo.RemoveRemote(tmpRemoteName) + + pushRef := branch+":"+"refs/pull/"+prIdStr+"/head" + if err = git.Push(headRepoPath, tmpRemoteName, pushRef); err != nil { + return fmt.Errorf("Error pushing: %v", err) + } + + return nil +} + // AddToTaskQueue adds itself to pull request test task queue. func (pr *PullRequest) AddToTaskQueue() { go PullRequestQueue.AddFunc(pr.ID, func() { @@ -536,6 +571,9 @@ func addHeadRepoTasks(prs []*PullRequest) { if err := pr.UpdatePatch(); err != nil { log.Error(4, "UpdatePatch: %v", err) continue + } else if err := pr.PushToBaseRepo(); err != nil { + log.Error(4, "PushToBaseRepo: %v", err) + continue } pr.AddToTaskQueue() diff --git a/models/repo.go b/models/repo.go index 4d45b744ff..8ce1f7190c 100644 --- a/models/repo.go +++ b/models/repo.go @@ -39,7 +39,6 @@ import ( const ( _TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3 --config='%s'\n" - _TPL_POST_RECEIVE_HOOK = "#!/usr/bin/env %s\n%s pull --path \"$(pwd)\"\n" ) var ( @@ -597,11 +596,6 @@ func createUpdateHook(repoPath string) error { fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"", setting.CustomConf)) } -func createPostReceiveHook(repoPath string) error { - return git.SetPostReceiveHook(repoPath, - fmt.Sprintf(_TPL_POST_RECEIVE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"")) -} - type MigrateRepoOptions struct { Name string Description string @@ -824,8 +818,6 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C return fmt.Errorf("InitRepository: %v", err) } else if err = createUpdateHook(repoPath); err != nil { return fmt.Errorf("createUpdateHook: %v", err) - } else if err = createPostReceiveHook(repoPath); err != nil { - return fmt.Errorf("createPostReceiveHook: %v", err) } tmpDir := filepath.Join(os.TempDir(), "gogs-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond())) @@ -1506,15 +1498,6 @@ func RewriteRepositoryUpdateHook() error { }) } -// RewriteRepositoryPostReceiveHook rewrites all repositories' update hook. -func RewriteRepositoryPostReceiveHook() error { - return x.Where("id > 0").Iterate(new(Repository), - func(idx int, bean interface{}) error { - repo := bean.(*Repository) - return createPostReceiveHook(repo.RepoPath()) - }) -} - // statusPool represents a pool of status with true/false. type statusPool struct { lock sync.RWMutex @@ -2062,8 +2045,6 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit if err = createUpdateHook(repoPath); err != nil { return nil, fmt.Errorf("createUpdateHook: %v", err) - } else if err = createPostReceiveHook(repoPath); err != nil { - return nil, fmt.Errorf("createPostReceiveHook: %v", err) } return repo, sess.Commit() From a3bdede2ce0701e1ffcfa7510cea404d6d74e2cb Mon Sep 17 00:00:00 2001 From: Martin Hartkorn Date: Thu, 4 Feb 2016 19:15:21 +0100 Subject: [PATCH 3/3] Removed unused method GetUnmergedPullRequestByRepoPathAndHeadBranch --- models/pull.go | 39 --------------------------------------- 1 file changed, 39 deletions(-) diff --git a/models/pull.go b/models/pull.go index a6c15c1322..c52c871c3f 100644 --- a/models/pull.go +++ b/models/pull.go @@ -5,7 +5,6 @@ package models import ( - "errors" "fmt" "os" "path" @@ -397,44 +396,6 @@ func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequ Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs) } -// Gets a Pull Request by the path of the forked repo and the branch from where the PR -// got submitted. -func GetUnmergedPullRequestByRepoPathAndHeadBranch(user, repo, branch string) (*PullRequest, error) { - userLower := strings.ToLower(user) - repoLower := strings.ToLower(repo) - - pr := new(PullRequest) - if x == nil { - return nil, errors.New("Fail") - } - has, err := x. - Where("head_user_name=? AND head_branch=? AND has_merged=? AND issue.is_closed=? AND repository.lower_name=?", userLower, branch, 0, 0, repoLower). - Join("INNER", "repository", "repository.id=pull_request.head_repo_id"). - Join("INNER", "issue", "issue.id=pull_request.issue_id"). - Get(pr) - - if err != nil { - return nil, err - } else if !has { - return nil, ErrPullRequestNotExist{0, 0, 0, 0, branch, ""} - } - - baseRepo := new(Repository) - has, err = x.Where("repository.id=?", pr.BaseRepoID). - Join("LEFT", "user", "user.id=repository.owner_id"). - Get(baseRepo) - - if err != nil { - return nil, err - } else if !has { - return nil, ErrRepoNotExist{pr.BaseRepoID, 0, ""} - } - - pr.BaseRepo = baseRepo - - return pr, nil -} - // GetPullRequestByID returns a pull request by given ID. func GetPullRequestByID(id int64) (*PullRequest, error) { pr := new(PullRequest)