Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 9ee7fa1

Browse files
committed
Add test and comments about PR base/head fetch order
The PR base branch might no longer exist, so we fetch using `refs/pull/<PR>/head` first. This will often fetch the base commits, even when the base branch no longer exists.
1 parent 5833138 commit 9ee7fa1

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/GitHub.App/Services/GitClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ public async Task<string> GetPullRequestMergeBase(IRepository repo,
389389
var headCommit = repo.Lookup<Commit>(headSha);
390390
if (headCommit == null)
391391
{
392+
// The PR base branch might no longer exist, so we fetch using `refs/pull/<PR>/head` first.
393+
// This will often fetch the base commits, even when the base branch no longer exists.
392394
var headRef = $"refs/pull/{pullNumber}/head";
393395
await Fetch(repo, targetCloneUrl, headRef);
394396
headCommit = repo.Lookup<Commit>(headSha);

src/UnitTests/GitHub.App/Services/GitClientTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ public async Task WhenToFetch(string targetCloneUrl, string baseSha, string head
204204
[Theory]
205205
[InlineData("baseSha", null, "mergeBaseSha", "baseRef", 777, "refs/pull/777/head")]
206206
[InlineData(null, "headSha", "mergeBaseSha", "baseRef", 777, "baseRef")]
207+
208+
// PR base might not exist, so we must fetch `refs/pull/<PR>/head` first.
209+
[InlineData(null, null, "mergeBaseSha", "baseRef", 777, "refs/pull/777/head")]
207210
public async Task WhatToFetch(string baseSha, string headSha, string mergeBaseSha, string baseRef, int pullNumber,
208211
string expectRefSpec)
209212
{
@@ -230,12 +233,12 @@ static IRepository MockRepo(string baseSha, string headSha, string mergeBaseSha)
230233

231234
if (baseSha != null)
232235
{
233-
repo.Lookup<Commit>(baseSha).Returns(baseCommit);
236+
repo.Lookup<Commit>(baseSha).Returns(baseSha != null ? baseCommit : null);
234237
}
235238

236239
if (headSha != null)
237240
{
238-
repo.Lookup<Commit>(headSha).Returns(headCommit);
241+
repo.Lookup<Commit>(headSha).Returns(headSha != null ? headCommit : null);
239242
}
240243

241244
repo.ObjectDatabase.FindMergeBase(baseCommit, headCommit).Returns(mergeBaseCommit);

0 commit comments

Comments
 (0)