Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Pull watchers from repository rather than additional API call #63

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
9 changes: 8 additions & 1 deletion src/Konmaripo.Web.Tests.Unit/Helpers/RepositoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class RepositoryBuilder
private bool _isPrivate = false;
private DateTimeOffset _pushedDate = DateTimeOffset.Now;
private string _repoUrl = "";
private int _watcherCount = 0;

public RepositoryBuilder()
{
Expand All @@ -48,7 +49,7 @@ public Repository Build()
_createdDate, _updatedDate, repositoryPermissions,
dummyInternalRepository, dummyInternalRepository,
licenseMetadata,
false, false, false, false, 0, 0, false, false, false, _isArchived, 0);
false, false, false, false, 0, 0, false, false, false, _isArchived, _watcherCount);

return repo;
}
Expand Down Expand Up @@ -118,6 +119,12 @@ public RepositoryBuilder WithUrlOf(string url)
_repoUrl = url;
return this;
}

public RepositoryBuilder WithWatcherCount(int watcherCount)
{
_watcherCount = watcherCount;
return this;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public GitHubRepoBuilder WithId(long id)

public GitHubRepo Build()
{
return new GitHubRepo(_id,string.Empty,0,false,0,0,DateTimeOffset.Now,DateTimeOffset.Now, string.Empty,false,DateTimeOffset.Now, string.Empty);
return new GitHubRepo(_id,string.Empty,0,false,0,0,DateTimeOffset.Now,DateTimeOffset.Now, string.Empty,false,DateTimeOffset.Now, string.Empty, 0);
}

}
Expand Down
21 changes: 21 additions & 0 deletions src/Konmaripo.Web.Tests.Unit/Services/GitHubServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,27 @@ public async Task ReturnsRepoUrlFromTheGithubClient()
resultUrls.Should().BeEquivalentTo(urlList);
}

[Fact]
public async Task ReturnsTheWatcherCountFromTheGitHubClient()
{
var watcherCountList = new List<int> { 1, 123, 123_456 };

var repositoryObjects = watcherCountList.Select(watcherCount =>
{
var repo = new RepositoryBuilder().WithWatcherCount(watcherCount).Build();
return repo;
}).ToList().As<IReadOnlyList<Repository>>();

_mockRepoClient.Setup(x =>
x.GetAllForOrg(It.IsAny<string>()))
.Returns(Task.FromResult(repositoryObjects));

var result = await _sut.GetRepositoriesForOrganizationAsync();
var resultWatcherCounts = result.Select(repoResult => repoResult.WatcherCount).ToList();

resultWatcherCounts.Should().BeEquivalentTo(watcherCountList);
}

[Fact]
public async Task UsesTheOrganizationNameFromSettings()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Konmaripo.Web/Models/GitHubRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class GitHubRepo
public bool IsPrivate { get; }
public Maybe<DateTimeOffset> PushedDate { get; }
public string RepoUrl { get; }
public int WatcherCount { get; }

public GitHubRepo(long repoId, string name, int starCount, bool isArchived, int forkCount, int openIssues, DateTimeOffset createdDate, DateTimeOffset updatedDate, string description, bool isPrivate, DateTimeOffset? pushedDate, string url)
public GitHubRepo(long repoId, string name, int starCount, bool isArchived, int forkCount, int openIssues, DateTimeOffset createdDate, DateTimeOffset updatedDate, string description, bool isPrivate, DateTimeOffset? pushedDate, string url, int watcherCount)
{
Name = name;
StarCount = starCount;
Expand All @@ -32,6 +33,7 @@ public GitHubRepo(long repoId, string name, int starCount, bool isArchived, int
IsPrivate = isPrivate;
PushedDate = pushedDate.ToMaybe();
RepoUrl = url;
WatcherCount = watcherCount;
}
}
}
2 changes: 1 addition & 1 deletion src/Konmaripo.Web/Services/CachedGitHubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task ArchiveRepository(long repoId, string repoName)
var repos = _memoryCache.Get<List<GitHubRepo>>(RepoCacheKey);
var item = repos.First(x => x.Id == repoId);

var archivedItem = new GitHubRepo(item.Id, item.Name, item.StarCount, true, item.ForkCount, item.OpenIssueCount, item.CreatedDate, item.UpdatedDate, item.Description, item.IsPrivate, item.PushedDate.ToNullable(), item.RepoUrl);
var archivedItem = new GitHubRepo(item.Id, item.Name, item.StarCount, true, item.ForkCount, item.OpenIssueCount, item.CreatedDate, item.UpdatedDate, item.Description, item.IsPrivate, item.PushedDate.ToNullable(), item.RepoUrl, item.WatcherCount);
repos.Remove(item);
repos.Add(archivedItem);

Expand Down
2 changes: 1 addition & 1 deletion src/Konmaripo.Web/Services/GitHubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task<List<GitHubRepo>> GetRepositoriesForOrganizationAsync()

var repos = await _githubClient.Repository.GetAllForOrg(orgName);

return repos.Select(x => new GitHubRepo(x.Id, x.Name, x.StargazersCount, x.Archived, x.ForksCount, x.OpenIssuesCount, x.CreatedAt, x.UpdatedAt, x.Description, x.Private, x.PushedAt, x.HtmlUrl)).ToList();
return repos.Select(x => new GitHubRepo(x.Id, x.Name, x.StargazersCount, x.Archived, x.ForksCount, x.OpenIssuesCount, x.CreatedAt, x.UpdatedAt, x.Description, x.Private, x.PushedAt, x.HtmlUrl, x.WatchersCount)).ToList();
}

public async Task<ExtendedRepoInformation> GetExtendedRepoInformationFor(long repoId)
Expand Down
3 changes: 2 additions & 1 deletion src/Konmaripo.Web/Views/Sunsetting/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<th scope="col"><!--Private icon--></th>
<th scope="col">Name</th>
<th scope="col" title="Stars"><i class="fas fa-star"></i></th>
<th scope="col" title="Watchers"><i class="fas fa-eye"></i></th>
<th scope="col" title="Forks"><i class="fas fa-code-branch"></i></th>
<th scope="col" title="Issues"><i class="fas fa-exclamation-circle"></i></th>
<th scope="col" title="Date the repository was created">Created</th>
Expand Down Expand Up @@ -49,7 +50,7 @@
}
else
{
<td>Never</td>
<td>Never</td>
}
</tr>
}
Expand Down
4 changes: 3 additions & 1 deletion src/Konmaripo.Web/Views/Sunsetting/Repo.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<th scope="col"><!--Private icon--></th>
<th scope="col">Name</th>
<th scope="col" title="Stars"><i class="fas fa-star"></i></th>
<th scope="col" title="Watchers"><i class="fas fa-eye"></i></th>
<th scope="col" title="Forks"><i class="fas fa-code-branch"></i></th>
<th scope="col" title="Issues"><i class="fas fa-exclamation-circle"></i></th>
<th scope="col" title="Date the repository was created">Created</th>
Expand All @@ -40,6 +41,7 @@
}
<th scope="row" title="@item.Description"><a href="@item.RepoUrl" target="_blank"><i class="fas fa-external-link-alt"></i></a> @item.Name</th>
<td>@item.StarCount</td>
<td>@item.WatcherCount</td>
<td>@item.ForkCount</td>
<td>@item.OpenIssueCount</td>
<td title="@item.CreatedDate">@item.CreatedDate.Humanize()</td>
Expand All @@ -64,7 +66,7 @@
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Number of Watchers</th>
<th scope="col">Watchers</th>
<th scope="col">Views in Past 14 days</th>
<th scope="col">Commit Activity in Last 4 Weeks</th>
</tr>
Expand Down