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

Commit 4526845

Browse files
committed
PullRequest.Head can be null.
1 parent 47ee8e0 commit 4526845

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/GitHub.App/Models/PullRequestModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public string Body
154154
}
155155

156156
public GitReferenceModel Base { get; set; }
157+
[AllowNull]
157158
public GitReferenceModel Head { get; set; }
158159
public DateTimeOffset CreatedAt { get; set; }
159160
public DateTimeOffset UpdatedAt { get; set; }

src/GitHub.App/Services/ModelService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ IPullRequestModel Create(PullRequestCacheItem prCacheItem)
370370
CommentCount = prCacheItem.CommentCount,
371371
CommitCount = prCacheItem.CommitCount,
372372
CreatedAt = prCacheItem.CreatedAt,
373-
Head = prCacheItem.Head ?? new GitReferenceModel(),
373+
Head = prCacheItem.Head,
374374
State = prCacheItem.State.HasValue ?
375375
prCacheItem.State.Value :
376376
prCacheItem.IsOpen.Value ? PullRequestStateEnum.Open : PullRequestStateEnum.Closed,
@@ -476,7 +476,9 @@ public PullRequestCacheItem(PullRequest pr, IReadOnlyList<PullRequestFile> files
476476
Title = pr.Title;
477477
Number = pr.Number;
478478
Base = new GitReferenceModel { Label = pr.Base.Label, Ref = pr.Base.Ref, RepositoryCloneUrl = pr.Base.Repository.CloneUrl };
479-
Head = new GitReferenceModel { Label = pr.Head.Label, Ref = pr.Head.Ref, RepositoryCloneUrl = pr.Head.Repository.CloneUrl };
479+
Head = pr.Head != null ?
480+
new GitReferenceModel { Label = pr.Head.Label, Ref = pr.Head.Ref, RepositoryCloneUrl = pr.Head.Repository.CloneUrl } :
481+
null;
480482
CommentCount = pr.Comments + pr.ReviewComments;
481483
CommitCount = pr.Commits;
482484
Author = new AccountCacheItem(pr.User);

src/GitHub.App/ViewModels/PullRequestDetailViewModel.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public override void Initialize([AllowNull] ViewWithData data)
254254
public async Task Load(IPullRequestModel pullRequest)
255255
{
256256
Model = pullRequest;
257-
SourceBranchDisplayName = GetBranchDisplayName(pullRequest.Head.Label);
257+
SourceBranchDisplayName = GetBranchDisplayName(pullRequest.Head?.Label);
258258
TargetBranchDisplayName = GetBranchDisplayName(pullRequest.Base.Label);
259259
Body = !string.IsNullOrWhiteSpace(pullRequest.Body) ? pullRequest.Body : "*No description provided.*";
260260

@@ -376,9 +376,16 @@ static PullRequestDirectoryNode GetDirectory(string path, Dictionary<string, Pul
376376

377377
string GetBranchDisplayName(string targetBranchLabel)
378378
{
379-
var parts = targetBranchLabel.Split(':');
380-
var owner = parts[0];
381-
return owner == repository.CloneUrl.Owner ? parts[1] : targetBranchLabel;
379+
if (targetBranchLabel != null)
380+
{
381+
var parts = targetBranchLabel.Split(':');
382+
var owner = parts[0];
383+
return owner == repository.CloneUrl.Owner ? parts[1] : targetBranchLabel;
384+
}
385+
else
386+
{
387+
return "[Invalid]";
388+
}
382389
}
383390

384391
IObservable<Unit> DoCheckout(object unused)

src/UnitTests/GitHub.App/ViewModels/PullRequestDetailViewModelTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ public async Task CheckoutInvalidStateShouldCallService()
285285
unused = target.Item2.Received().FetchAndCheckout(Arg.Any<ILocalRepositoryModel>(), target.Item1.Model.Number, "pr/1-foo");
286286
}
287287

288+
[Fact]
289+
public async Task ShouldAcceptNullHead()
290+
{
291+
var target = CreateTarget();
292+
var model = CreatePullRequest();
293+
294+
// PullRequest.Head can be null for example if a user deletes the repository after creating the PR.
295+
model.Head = null;
296+
297+
await target.Load(model);
298+
299+
Assert.Equal("[Invalid]", target.SourceBranchDisplayName);
300+
}
301+
288302
PullRequestDetailViewModel CreateTarget(
289303
string currentBranch = "master",
290304
string existingPrBranch = null,

0 commit comments

Comments
 (0)