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

Commit b890f32

Browse files
committed
Make GitReferenceModel immutable.
To try and minimize the chances of null exceptions.
1 parent 4526845 commit b890f32

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

src/GitHub.App/Services/ModelService.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ IRemoteRepositoryModel Create(RepositoryCacheItem item)
354354
};
355355
}
356356

357+
private GitReferenceModel Create(GitReferenceCacheItem item)
358+
{
359+
return item != null ? new GitReferenceModel(item.Ref, item.Label, item.RepositoryCloneUrl) : null;
360+
}
361+
357362
IPullRequestModel Create(PullRequestCacheItem prCacheItem)
358363
{
359364
return new PullRequestModel(
@@ -364,13 +369,13 @@ IPullRequestModel Create(PullRequestCacheItem prCacheItem)
364369
prCacheItem.UpdatedAt)
365370
{
366371
Assignee = prCacheItem.Assignee != null ? Create(prCacheItem.Assignee) : null,
367-
Base = prCacheItem.Base ?? new GitReferenceModel(),
372+
Base = Create(prCacheItem.Base),
368373
Body = prCacheItem.Body ?? string.Empty,
369374
ChangedFiles = prCacheItem.ChangedFiles.Select(x => (IPullRequestFileModel)new PullRequestFileModel(x.FileName, x.Status)).ToList(),
370375
CommentCount = prCacheItem.CommentCount,
371376
CommitCount = prCacheItem.CommitCount,
372377
CreatedAt = prCacheItem.CreatedAt,
373-
Head = prCacheItem.Head,
378+
Head = Create(prCacheItem.Head),
374379
State = prCacheItem.State.HasValue ?
375380
prCacheItem.State.Value :
376381
prCacheItem.IsOpen.Value ? PullRequestStateEnum.Open : PullRequestStateEnum.Closed,
@@ -475,10 +480,8 @@ public PullRequestCacheItem(PullRequest pr, IReadOnlyList<PullRequestFile> files
475480
{
476481
Title = pr.Title;
477482
Number = pr.Number;
478-
Base = new GitReferenceModel { Label = pr.Base.Label, Ref = pr.Base.Ref, RepositoryCloneUrl = pr.Base.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;
483+
Base = new GitReferenceCacheItem { Label = pr.Base.Label, Ref = pr.Base.Ref, RepositoryCloneUrl = pr.Base.Repository.CloneUrl };
484+
Head = pr.Head != null ? new GitReferenceCacheItem { Label = pr.Head.Label, Ref = pr.Head.Ref, RepositoryCloneUrl = pr.Head.Repository.CloneUrl } : null;
482485
CommentCount = pr.Comments + pr.ReviewComments;
483486
CommitCount = pr.Commits;
484487
Author = new AccountCacheItem(pr.User);
@@ -496,8 +499,8 @@ public PullRequestCacheItem(PullRequest pr, IReadOnlyList<PullRequestFile> files
496499

497500
public string Title {get; set; }
498501
public int Number { get; set; }
499-
public GitReferenceModel Base { get; set; }
500-
public GitReferenceModel Head { get; set; }
502+
public GitReferenceCacheItem Base { get; set; }
503+
public GitReferenceCacheItem Head { get; set; }
501504
public int CommentCount { get; set; }
502505
public int CommitCount { get; set; }
503506
public AccountCacheItem Author { get; set; }
@@ -531,6 +534,7 @@ static PullRequestStateEnum GetState(PullRequest pullRequest)
531534
}
532535
}
533536

537+
[NullGuard(ValidationFlags.None)]
534538
public class PullRequestFileCacheItem
535539
{
536540
public PullRequestFileCacheItem()
@@ -546,5 +550,13 @@ public PullRequestFileCacheItem(PullRequestFile file)
546550
public string FileName { get; set; }
547551
public PullRequestFileStatus Status { get; set; }
548552
}
553+
554+
[NullGuard(ValidationFlags.None)]
555+
public class GitReferenceCacheItem
556+
{
557+
public string Ref { get; set; }
558+
public string Label { get; set; }
559+
public string RepositoryCloneUrl { get; set; }
560+
}
549561
}
550562
}
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1-
using GitHub.Primitives;
1+
using GitHub.Extensions;
2+
using GitHub.Primitives;
23

34
namespace GitHub.Models
45
{
56
public class GitReferenceModel
67
{
7-
public string Ref { get; set; }
8-
public string Label { get; set; }
9-
public UriString RepositoryCloneUrl { get; set; }
8+
public GitReferenceModel(string @ref, string label, string repositoryCloneUri)
9+
: this(@ref, label, new UriString(repositoryCloneUri))
10+
{
11+
}
12+
13+
public GitReferenceModel(string @ref, string label, UriString repositoryCloneUri)
14+
{
15+
Guard.ArgumentNotEmptyString(@ref, nameof(@ref));
16+
Guard.ArgumentNotEmptyString(label, nameof(label));
17+
Guard.ArgumentNotNull(repositoryCloneUri, nameof(repositoryCloneUri));
18+
Guard.ArgumentNotEmptyString(repositoryCloneUri.ToString(), nameof(repositoryCloneUri));
19+
20+
Ref = @ref;
21+
Label = label;
22+
RepositoryCloneUrl = repositoryCloneUri;
23+
}
24+
25+
public string Ref { get; }
26+
public string Label { get; }
27+
public UriString RepositoryCloneUrl { get; }
1028
}
1129
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ static IPullRequestModel CreatePullRequest()
158158
{
159159
State = PullRequestStateEnum.Open,
160160
Body = string.Empty,
161-
Head = new GitReferenceModel { Label = "foo:baz", Ref = "source", RepositoryCloneUrl = "https://github.com/foo/bar.git" },
162-
Base = new GitReferenceModel { Label = "foo:bar", Ref = "source", RepositoryCloneUrl = "https://github.com/foo/bar.git" },
161+
Head = new GitReferenceModel("source", "foo:baz", "https://github.com/foo/bar.git"),
162+
Base = new GitReferenceModel("dest", "foo:bar", "https://github.com/foo/bar.git"),
163163
};
164164
}
165165

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ PullRequestModel CreatePullRequest(string body = "PR Body")
368368
{
369369
State = PullRequestStateEnum.Open,
370370
Body = string.Empty,
371-
Head = new GitReferenceModel { Label = "foo:baz", Ref = "source", RepositoryCloneUrl = "https://github.com/foo/bar.git" },
372-
Base = new GitReferenceModel { Label = "foo:bar", Ref = "source", RepositoryCloneUrl = "https://github.com/foo/bar.git" },
371+
Head = new GitReferenceModel("source", "foo:baz", "https://github.com/foo/bar.git"),
372+
Base = new GitReferenceModel("dest", "foo:bar", "https://github.com/foo/bar.git"),
373373
};
374374
}
375375
}

src/UnitTests/Helpers/TestBaseClass.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ protected static PullRequest CreatePullRequest(User user, int id, ItemState stat
5858
DateTimeOffset createdAt, DateTimeOffset updatedAt, int commentCount = 0, int reviewCommentCount = 0)
5959
{
6060
var uri = new Uri("https://url");
61-
var repo = new Repository();
61+
var uris = uri.ToString();
62+
var repo = new Repository(uris, uris, uris, uris, uris, uris, uris,
63+
1, user, "Repo", "Repo", string.Empty, string.Empty, string.Empty,
64+
false, false, 0, 0, "master",
65+
0, null, createdAt, updatedAt,
66+
null, null, null,
67+
false, false, false);
6268
return new PullRequest(uri, uri, uri, uri, uri, uri,
6369
id, state, title, "", createdAt, updatedAt,
6470
null, null,

0 commit comments

Comments
 (0)