summaryrefslogtreecommitdiffstats
path: root/cmd/pull.go
blob: cf5016eb54445bee15595146c74c12674a5906ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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: <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())
		}
	}
}