Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
use smarter pull request fetching.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Sep 19, 2017
1 parent 998eb05 commit e91a580
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/scm/scm_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (g *scmGithub) CheckoutPullRequestPayload(payload *Payload) error {
g.PipelineData.GitLocalPath = gitLocalPath
g.PipelineData.GitLocalBranch = fmt.Sprintf("pr_%s", payload.PullRequestNumber)

ferr := utils.GitFetch(g.PipelineData.GitLocalPath, fmt.Sprintf("refs/pull/%s/merge", payload.PullRequestNumber), g.PipelineData.GitLocalBranch)
ferr := utils.GitFetchPullRequest(g.PipelineData.GitLocalPath, payload.PullRequestNumber, g.PipelineData.GitLocalBranch)
if ferr != nil {
return ferr
}
Expand Down
58 changes: 45 additions & 13 deletions pkg/utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func GitClone(parentPath string, repositoryName string, gitRemote string) (strin
return absPath, err
}

func GitFetch(repoPath string, remoteRef string, localBranchName string) error {

// https://stackoverflow.com/questions/13638235/git-checkout-remote-reference
// https://gist.github.com/danielfbm/ba4ae91efa96bb4771351bdbd2c8b06f
// https://github.com/libgit2/git2go/issues/126
func GitFetchPullRequest(repoPath string, pullRequestNumber string, localBranchName string) error {

repo, oerr := git2go.OpenRepository(repoPath)
if oerr != nil {
Expand All @@ -50,39 +54,67 @@ func GitFetch(repoPath string, remoteRef string, localBranchName string) error {
}
time.Sleep(time.Second)

ferr := remote.Fetch([]string{fmt.Sprintf("%s:%s", remoteRef, localBranchName)}, new(git2go.FetchOptions), "")

// fetch the pull request merge and head references into this repo.
ferr := remote.Fetch([]string{"+refs/pull/*:refs/remotes/origin/pr/*"}, new(git2go.FetchOptions), "")
if ferr != nil {
log.Print("Failed to fetch remote ref into new local branch " + fmt.Sprintf("%s:%s", remoteRef, localBranchName))
log.Print("Failed to fetch PR references from remote")
return ferr
}

time.Sleep(time.Second)
//should not raise an error when looking for branch (we just created it above)
localBranch, berr := repo.LookupBranch(localBranchName, git2go.BranchLocal)
if berr != nil {
log.Print("Failed to lookup new local branch " + fmt.Sprintf("%s:%s", remoteRef, localBranchName))
return berr

// Get a reference to the PR merge branch in this repo
prRef, err := repo.References.Lookup(fmt.Sprintf("refs/remotes/origin/pr/%s/merge", pullRequestNumber))
if err != nil {
log.Print("Failed to find PR reference locally: " + fmt.Sprintf("refs/remotes/origin/pr/%s/merge", pullRequestNumber))
return err
}

// Lookup commmit for PR branch
prCommit, err := repo.LookupCommit(prRef.Target())
if err != nil {
log.Print(fmt.Sprintf("Failed to find PR head commit: %s", prRef.Target()))
return err
}
defer prCommit.Free()



prLocalBranch, err := repo.LookupBranch(localBranchName, git2go.BranchLocal)
// No local branch, lets create one
if prLocalBranch == nil || err != nil {
// Creating local branch
prLocalBranch, err = repo.CreateBranch(localBranchName, prCommit, false)
if err != nil {
log.Print("Failed to create local branch: " + localBranchName)
return err
}
}
if prLocalBranch == nil {
return errors.ScmFilesystemError("Error while locating/creating local branch")
}
defer prLocalBranch.Free()


// Getting the tree for the branch
localCommit, err := repo.LookupCommit(localBranch.Target())
localCommit, err := repo.LookupCommit(prLocalBranch.Target())
if err != nil {
log.Print("Failed to lookup for commit in local branch " + fmt.Sprintf("%s:%s", remoteRef, localBranchName))
log.Print("Failed to lookup for commit in local branch " + localBranchName)
return err
}
//defer localCommit.Free()

tree, err := repo.LookupTree(localCommit.TreeId())
if err != nil {
log.Print("Failed to lookup for tree " + fmt.Sprintf("%s:%s", remoteRef, localBranchName))
log.Print("Failed to lookup for tree " + localBranchName)
return err
}
//defer tree.Free()

// Checkout the tree
err = repo.CheckoutTree(tree, checkoutOpts)
if err != nil {
log.Print("Failed to checkout tree " + fmt.Sprintf("%s:%s", remoteRef, localBranchName))
log.Print("Failed to checkout tree " + localBranchName)
return err
}
// Setting the Head to point to our branch
Expand Down
29 changes: 23 additions & 6 deletions pkg/utils/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,37 @@ func TestGitFetch(t *testing.T) {
//test
clonePath, cerr := utils.GitClone(dirPath, "cookbook_analogj_test", "https://github.com/AnalogJ/cookbook_analogj_test.git")
require.NoError(t, cerr)
ferr := utils.GitFetch(clonePath, "refs/pull/12/merge", "localBranchName")
ferr := utils.GitFetchPullRequest(clonePath, "12", "localBranchName")

//assert
require.NoError(t, ferr)
}

func TestGitFetch_LexiconFailure(t *testing.T) {
t.Parallel()

//setup
dirPath, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer deleteTestRepo(dirPath)

//test
clonePath, cerr := utils.GitClone(dirPath, "lexicon", "https://github.com/AnalogJ/lexicon.git")
require.NoError(t, cerr)
ferr := utils.GitFetchPullRequest(clonePath, "141", "pr_141")

//assert
require.Error(t, ferr)
}

func TestGitFetch_InvalidDirectory(t *testing.T) {
t.Parallel()

//setup
dirPath := path.Join("this", "path", "does", "not", "exist")

//test
ferr := utils.GitFetch(dirPath, "refs/pull/12/merge", "localBranchName")
ferr := utils.GitFetchPullRequest(dirPath, "12", "localBranchName")

//assert
require.Error(t, ferr)
Expand Down Expand Up @@ -245,7 +262,7 @@ func TestGitPush_PullRequest(t *testing.T) {
require.NoError(t, cerr)

//test
ferr := utils.GitFetch(clonePath, "refs/pull/13/merge", "localBranchName")
ferr := utils.GitFetchPullRequest(clonePath, "13", "localBranchName")
require.NoError(t, ferr)
d1 := []byte("hello\nworld\n")
werr := ioutil.WriteFile(clonePath+"/push_testfile.txt", d1, 0644)
Expand Down Expand Up @@ -343,7 +360,7 @@ func TestGitFindNearestTagName_FetchPullRequest(t *testing.T) {
defer deleteTestRepo(dirPath)
clonePath, cerr := utils.GitClone(dirPath, "tags_analogj_test2", "https://github.com/AnalogJ/tags_analogj_test2.git")
require.NoError(t, cerr)
cerr = utils.GitFetch(clonePath, "refs/pull/1/merge", "tagsAnalogJTest2_pr1")
cerr = utils.GitFetchPullRequest(clonePath, "1", "tagsAnalogJTest2_pr1")
require.NoError(t, cerr)

//test
Expand Down Expand Up @@ -418,7 +435,7 @@ func TestGitGenerateChangelog_TagSincePROpened(t *testing.T) {
defer deleteTestRepo(dirPath)
clonePath, cerr := utils.GitClone(dirPath, "tags_analogj_test2", "https://github.com/AnalogJ/tags_analogj_test2.git")
require.NoError(t, cerr)
cerr = utils.GitFetch(clonePath, "refs/pull/1/merge", "tagsAnalogJTest2_pr1")
cerr = utils.GitFetchPullRequest(clonePath, "1", "tagsAnalogJTest2_pr1")
require.NoError(t, cerr)
//headSha, err := utils.GitHead(clonePath)
//require.NoError(t, err)
Expand Down Expand Up @@ -478,7 +495,7 @@ func TestGitGetTagDetails(t *testing.T) {
defer deleteTestRepo(dirPath)
clonePath, cerr := utils.GitClone(dirPath, "tags_analogj_test", "https://github.com/AnalogJ/tags_analogj_test.git")
require.NoError(t, cerr)
cerr = utils.GitFetch(clonePath, "refs/pull/1/merge", "tagsAnalogJTest_pr1")
cerr = utils.GitFetchPullRequest(clonePath, "1", "tagsAnalogJTest_pr1")
require.NoError(t, cerr)

//test
Expand Down

0 comments on commit e91a580

Please sign in to comment.