Skip to content

Commit f0bb2b4

Browse files
osherdpmarcus-revjamengual
authored
fix: Allow main branch invocation with merge strategy (#5617)
Signed-off-by: Osher De Paz <osher.depaz@island.io> Co-authored-by: Marcus Bastian <marcus.bastian@rev.com> Co-authored-by: PePe Amengual <2208324+jamengual@users.noreply.github.com>
1 parent 59f16ef commit f0bb2b4

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

server/events/vcs/instrumented_client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ func (c *InstrumentedClient) PullIsMergeable(logger logging.SimpleLogging, repo
206206
}
207207

208208
func (c *InstrumentedClient) UpdateStatus(logger logging.SimpleLogging, repo models.Repo, pull models.PullRequest, state models.CommitStatus, src string, description string, url string) error {
209+
// If the plan isn't coming from a pull request,
210+
// don't attempt to update the status.
211+
if pull.Num == 0 {
212+
return nil
213+
}
214+
209215
scope := c.StatsScope.SubScope("update_status")
210216
scope = SetGitScopeTags(scope, repo.FullName, pull.Num)
211217

server/events/working_dir.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ type FileWorkspace struct {
7777
// TestingOverrideBaseCloneURL can be used during testing to override the
7878
// URL of the base repo to be cloned. If it's empty then we clone normally.
7979
TestingOverrideBaseCloneURL string
80-
// GithubAppEnabled is true if we should fetch the ref "pull/PR_NUMBER/head"
81-
// from the "origin" remote. If this is false, we fetch "+refs/heads/$HEAD_BRANCH"
82-
// from the "head" remote.
80+
// GithubAppEnabled is true and a PR number is supplied, we should fetch
81+
// the ref "pull/PR_NUMBER/head" from the "origin" remote. If this is false,
82+
// we fetch "+refs/heads/$HEAD_BRANCH" from the "head" remote.
8383
GithubAppEnabled bool
8484
// use the global setting without overriding
8585
GpgNoSigningEnabled bool
@@ -109,12 +109,13 @@ func (w *FileWorkspace) Clone(logger logging.SimpleLogging, headRepo models.Repo
109109
logger.Debug("clone directory '%s' already exists, checking if it's at the right commit", cloneDir)
110110

111111
// We use git rev-parse to see if our repo is at the right commit.
112-
// If just checking out the pull request branch, we can use HEAD.
112+
// If just checking out the pull request branch or if there is no
113+
// pull request (API triggered with a custom git ref), we can use HEAD.
113114
// If doing a merge, then HEAD won't be at the pull request's HEAD
114115
// because we'll already have performed a merge. Instead, we'll check
115116
// HEAD^2 since that will be the commit before our merge.
116117
pullHead := "HEAD"
117-
if w.CheckoutMerge {
118+
if w.CheckoutMerge && c.pr.Num > 0 {
118119
pullHead = "HEAD^2"
119120
}
120121
revParseCmd := exec.Command("git", "rev-parse", pullHead) // #nosec
@@ -351,7 +352,7 @@ func (w *FileWorkspace) wrappedGit(logger logging.SimpleLogging, c wrappedGitCon
351352
func (w *FileWorkspace) mergeToBaseBranch(logger logging.SimpleLogging, c wrappedGitContext) error {
352353
fetchRef := fmt.Sprintf("+refs/heads/%s:", c.pr.HeadBranch)
353354
fetchRemote := "head"
354-
if w.GithubAppEnabled {
355+
if w.GithubAppEnabled && c.pr.Num > 0 {
355356
fetchRef = fmt.Sprintf("pull/%d/head:", c.pr.Num)
356357
fetchRemote = "origin"
357358
}

server/events/working_dir_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,53 @@ func TestClone_NoneExisting(t *testing.T) {
5656
Equals(t, expCommit, actCommit)
5757
}
5858

59+
// Test running on main branch with merge strategy
60+
func TestClone_MainBranchWithMergeStrategy(t *testing.T) {
61+
// Initialize the git repo.
62+
repoDir := initRepo(t)
63+
expCommit := runCmd(t, repoDir, "git", "rev-parse", "main")
64+
65+
dataDir := t.TempDir()
66+
67+
logger := logging.NewNoopLogger(t)
68+
69+
overrideURL := fmt.Sprintf("file://%s", repoDir)
70+
wd := &events.FileWorkspace{
71+
DataDir: dataDir,
72+
CheckoutMerge: true,
73+
TestingOverrideHeadCloneURL: overrideURL,
74+
TestingOverrideBaseCloneURL: overrideURL,
75+
GpgNoSigningEnabled: true,
76+
}
77+
78+
_, err := wd.Clone(logger, models.Repo{}, models.PullRequest{
79+
Num: 0,
80+
BaseRepo: models.Repo{},
81+
HeadBranch: "branch",
82+
BaseBranch: "main",
83+
}, "default")
84+
Ok(t, err)
85+
86+
// Create a file that we can use to check if the repo was recloned.
87+
runCmd(t, dataDir, "touch", "repos/0/default/proof")
88+
89+
// re-clone to make sure we don't try to merge main into itself
90+
cloneDir, err := wd.Clone(logger, models.Repo{}, models.PullRequest{
91+
BaseRepo: models.Repo{},
92+
HeadBranch: "branch",
93+
BaseBranch: "main",
94+
}, "default")
95+
Ok(t, err)
96+
97+
// Use rev-parse to verify at correct commit.
98+
actCommit := runCmd(t, cloneDir, "git", "rev-parse", "HEAD")
99+
Equals(t, expCommit, actCommit)
100+
101+
// Check that our proof file is still there, proving that we didn't reclone.
102+
_, err = os.Stat(filepath.Join(cloneDir, "proof"))
103+
Ok(t, err)
104+
}
105+
59106
// Test that if we don't have any existing files, we check out the repo
60107
// successfully when we're using the merge method.
61108
func TestClone_CheckoutMergeNoneExisting(t *testing.T) {

0 commit comments

Comments
 (0)