// 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: <old-commit> <new-commit> <ref-name> | |||||
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()) | |||||
} | |||||
} | |||||
} |
cmd.CmdUpdate, | cmd.CmdUpdate, | ||||
cmd.CmdDump, | cmd.CmdDump, | ||||
cmd.CmdCert, | cmd.CmdCert, | ||||
cmd.CmdPull, | |||||
} | } | ||||
app.Flags = append(app.Flags, []cli.Flag{}...) | app.Flags = append(app.Flags, []cli.Flag{}...) | ||||
app.Run(os.Args) | app.Run(os.Args) |
"github.com/gogits/gogs/modules/log" | "github.com/gogits/gogs/modules/log" | ||||
"github.com/gogits/gogs/modules/process" | "github.com/gogits/gogs/modules/process" | ||||
"github.com/gogits/gogs/modules/setting" | "github.com/gogits/gogs/modules/setting" | ||||
"strconv" | |||||
) | ) | ||||
type PullRequestType int | type PullRequestType int | ||||
return nil | 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. | // AddToTaskQueue adds itself to pull request test task queue. | ||||
func (pr *PullRequest) AddToTaskQueue() { | func (pr *PullRequest) AddToTaskQueue() { | ||||
go PullRequestQueue.AddFunc(pr.ID, func() { | go PullRequestQueue.AddFunc(pr.ID, func() { | ||||
if err := pr.UpdatePatch(); err != nil { | if err := pr.UpdatePatch(); err != nil { | ||||
log.Error(4, "UpdatePatch: %v", err) | log.Error(4, "UpdatePatch: %v", err) | ||||
continue | continue | ||||
} else if err := pr.PushToBaseRepo(); err != nil { | |||||
log.Error(4, "PushToBaseRepo: %v", err) | |||||
continue | |||||
} | } | ||||
pr.AddToTaskQueue() | pr.AddToTaskQueue() |
const ( | const ( | ||||
_TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3 --config='%s'\n" | _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 ( | var ( | ||||
fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+setting.AppPath+"\"", setting.CustomConf)) | 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 { | type MigrateRepoOptions struct { | ||||
Name string | Name string | ||||
Description string | Description string | ||||
return fmt.Errorf("InitRepository: %v", err) | return fmt.Errorf("InitRepository: %v", err) | ||||
} else if err = createUpdateHook(repoPath); err != nil { | } else if err = createUpdateHook(repoPath); err != nil { | ||||
return fmt.Errorf("createUpdateHook: %v", err) | 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())) | tmpDir := filepath.Join(os.TempDir(), "gogs-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond())) | ||||
}) | }) | ||||
} | } | ||||
// 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. | // statusPool represents a pool of status with true/false. | ||||
type statusPool struct { | type statusPool struct { | ||||
lock sync.RWMutex | lock sync.RWMutex | ||||
if err = createUpdateHook(repoPath); err != nil { | if err = createUpdateHook(repoPath); err != nil { | ||||
return nil, fmt.Errorf("createUpdateHook: %v", err) | 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() | return repo, sess.Commit() |