Skip to content

Commit

Permalink
Merge pull request #26 from screwdriver-cd/mergePR
Browse files Browse the repository at this point in the history
Add checkIfPR, Fetch, and Merge functions
  • Loading branch information
jer authored Aug 5, 2016
2 parents 88b9b67 + 0a1218e commit 7a7b2c2
Show file tree
Hide file tree
Showing 6 changed files with 473 additions and 60 deletions.
81 changes: 64 additions & 17 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import (
)

var execCommand = exec.Command
var clone = Clone
var setConfig = SetConfig
var mergePR = MergePR
var fetchPR = FetchPR
var merge = Merge

// command executes the git command
func command(arguments ...string) error {
cmd := execCommand("git", arguments...)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return fmt.Errorf("starting git command: %v", err)
}

err = cmd.Wait()
if err != nil {
return fmt.Errorf("running git command: %v", err)
}
return nil
}

// Clone clones a git repo into a destination directory
func Clone(scmURL, destination string) error {
Expand All @@ -20,36 +43,60 @@ func Clone(scmURL, destination string) error {

repo := parts[0]
branch := parts[1]
cmd := execCommand("git", "clone", "--quiet", "--progress", "--branch", branch, repo, destination)
return command("clone", "--quiet", "--progress", "--branch", branch, repo, destination)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
// SetConfig sets up git configuration
func SetConfig(setting, name string) error {
return command("config", setting, name)
}

// FetchPR fetches a pull request into a specified branch
func FetchPR(prNumber string, branch string) error {
return command("fetch", "origin", "pull/"+prNumber+"/head:"+branch)
}

// Merge merges changes on the specified branch
func Merge(branch string) error {
return command("merge", "--no-edit", branch)
}

// MergePR calls FetchPR and Merge
func MergePR(prNumber string, branch string) error {
err := fetchPR(prNumber, branch)
if err != nil {
return fmt.Errorf("starting git clone command: %v", err)
return fmt.Errorf("fetching pr: %v", err)
}

err = cmd.Wait()
err = merge(branch)
if err != nil {
return fmt.Errorf("cloning git repo: %v", err)
return fmt.Errorf("merging pr: %v", err)
}
return nil
}

//SetConfig sets up git configuration
func SetConfig(setting, name string) error {
cmd := execCommand("git", "config", setting, name)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Setup clones a repository, sets the local config, and merges a PR if necessary
func Setup(scmURL, destination, pr string) error {
err := clone(scmURL, destination)
if err != nil {
return fmt.Errorf("cloning repository: %v", err)
}

err := cmd.Start()
err = setConfig("user.name", "sd-buildbot")
if err != nil {
return fmt.Errorf("starting git config command: %v", err)
return fmt.Errorf("setting username: %v", err)
}

err = cmd.Wait()
err = setConfig("user.email", "dev-null@screwdriver.cd")
if err != nil {
return fmt.Errorf("setting git config: %v", err)
return fmt.Errorf("setting email: %v", err)
}

if pr != "" {
err = mergePR(pr, "_pr")
if err != nil {
return fmt.Errorf("merging pr: %v", err)
}
}

return nil
}
Loading

0 comments on commit 7a7b2c2

Please sign in to comment.