Skip to content

Commit 011771d

Browse files
Exclude the source repo as dependent repo (#10347)
* Cache top 6 popluar repos to exclude the repo itself if any * Add ComparableGitHubRepository to DisplayPackageViewModel * Compare Repos Id with ComparableGitHubRepository * Apply code suggestions * Fix total and top counts when excluding --------- Co-authored-by: Ruijie Shi <[email protected]> Co-authored-by: Joel Verhagen <[email protected]>
1 parent 77aef6f commit 011771d

8 files changed

Lines changed: 113 additions & 12 deletions

File tree

src/NuGetGallery.Core/GitHub/NuGetPackageGitHubInformation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -9,7 +9,7 @@ namespace NuGetGallery
99
{
1010
public class NuGetPackageGitHubInformation
1111
{
12-
public const int ReposPerPackage = 20;
12+
public const int ReposPerPackage = 21;
1313

1414
public readonly static NuGetPackageGitHubInformation Empty = new NuGetPackageGitHubInformation(new List<RepositoryInformation>());
1515

src/NuGetGallery/Controllers/PackagesController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,8 @@ public virtual async Task<ActionResult> DisplayPackage(string id, string version
10031003

10041004
if (model.IsGitHubUsageEnabled = _featureFlagService.IsGitHubUsageEnabled(currentUser))
10051005
{
1006-
model.GitHubDependenciesInformation = _contentObjectService.GitHubUsageConfiguration.GetPackageInformation(id);
1006+
var gitHubUsage = _contentObjectService.GitHubUsageConfiguration.GetPackageInformation(id);
1007+
model.GitHubDependenciesInformation = new GitHubUsageViewModel(model.ComparableGitHubRepository, gitHubUsage);
10071008
}
10081009

10091010
// If the normalized version is actually a SemVer but does not match the resolved package version, show a

src/NuGetGallery/Helpers/ViewModelExtensions/DisplayPackageViewModelFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -168,6 +168,8 @@ private DisplayPackageViewModel SetupCommon(
168168
viewModel.ProjectUrl = projectUrl;
169169
}
170170

171+
viewModel.InitializeComparableGitHubRepository();
172+
171173
var fugetUrl = $"https://www.fuget.org/packages/{package.Id}/{package.NormalizedVersion}";
172174
if (PackageHelper.TryPrepareUrlForRendering(fugetUrl, out string fugetReadyUrl))
173175
{

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@
784784
<Compile Include="ViewModels\DeleteOrganizationViewModel.cs" />
785785
<Compile Include="ViewModels\DeleteUserViewModel.cs" />
786786
<Compile Include="ViewModels\DisplayLicenseViewModel.cs" />
787+
<Compile Include="ViewModels\GitHubUsageViewModel.cs" />
787788
<Compile Include="ViewModels\ListCertificateItemViewModel.cs" />
788789
<Compile Include="ViewModels\GalleryHomeViewModel.cs" />
789790
<Compile Include="ViewModels\HandleOrganizationMembershipRequestModel.cs" />

src/NuGetGallery/ViewModels/DisplayPackageViewModel.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -46,7 +46,7 @@ public class DisplayPackageViewModel : ListPackageItemViewModel
4646
public bool IsRecentPackagesNoIndexEnabled { get; set; }
4747
public bool IsMarkdigMdSyntaxHighlightEnabled { get; set; }
4848
public bool CanDisplayReadmeWarning { get; set; }
49-
public NuGetPackageGitHubInformation GitHubDependenciesInformation { get; set; }
49+
public GitHubUsageViewModel GitHubDependenciesInformation { get; set; }
5050
public bool HasEmbeddedIcon { get; set; }
5151
public bool HasEmbeddedReadmeFile { get; set; }
5252
public PackageDependents PackageDependents { get; set; }
@@ -110,6 +110,8 @@ public bool HasNewerRelease
110110
public bool IsComputeTargetFrameworkEnabled { get; set; }
111111
public PackageFrameworkCompatibility PackageFrameworkCompatibility { get; set; }
112112

113+
public string ComparableGitHubRepository { get; private set; }
114+
113115
public void InitializeRepositoryMetadata(string repositoryUrl, string repositoryType)
114116
{
115117
RepositoryType = RepositoryKind.Unknown;
@@ -138,6 +140,36 @@ public void InitializeRepositoryMetadata(string repositoryUrl, string repository
138140
}
139141
}
140142

143+
public void InitializeComparableGitHubRepository()
144+
{
145+
const string githubCom = "github.com/";
146+
string comparableUrl = string.Empty;
147+
148+
if (RepositoryUrl != null && RepositoryUrl.Contains(githubCom))
149+
{
150+
comparableUrl = RepositoryUrl;
151+
}
152+
else if (ProjectUrl != null && ProjectUrl.Contains(githubCom))
153+
{
154+
comparableUrl = ProjectUrl;
155+
}
156+
157+
comparableUrl = comparableUrl.ToLowerInvariant();
158+
comparableUrl = comparableUrl.EndsWith("/") ? comparableUrl.Substring(0, comparableUrl.Length - 1) : comparableUrl;
159+
comparableUrl = comparableUrl.EndsWith(".git") ? comparableUrl.Substring(0, comparableUrl.Length - ".git".Length) : comparableUrl;
160+
161+
if (comparableUrl.StartsWith("git://"))
162+
{
163+
comparableUrl = comparableUrl.Replace($"git://{githubCom}", "");
164+
}
165+
else if (comparableUrl.StartsWith("https://"))
166+
{
167+
comparableUrl = comparableUrl.Replace($"https://{githubCom}", "");
168+
}
169+
170+
ComparableGitHubRepository = comparableUrl;
171+
}
172+
141173
public bool CanDisplayNuGetPackageExplorerLink()
142174
{
143175
return IsNuGetPackageExplorerLinkEnabled && !string.IsNullOrEmpty(NuGetPackageExplorerUrl) && Available;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace NuGetGallery
9+
{
10+
public class GitHubUsageViewModel
11+
{
12+
public GitHubUsageViewModel(string comparableGitHubRepository, NuGetPackageGitHubInformation gitHubUsage)
13+
{
14+
TotalRepos = gitHubUsage.TotalRepos;
15+
16+
// filter out repositories that are not the same as the package itself
17+
var repos = new List<RepositoryInformation>();
18+
foreach (var repo in gitHubUsage.Repos)
19+
{
20+
if (repo.Id.Equals(comparableGitHubRepository, StringComparison.OrdinalIgnoreCase))
21+
{
22+
TotalRepos--;
23+
continue;
24+
}
25+
26+
repos.Add(repo);
27+
28+
if (repos.Count == NuGetPackageGitHubInformation.ReposPerPackage - 1)
29+
{
30+
break;
31+
}
32+
}
33+
34+
Repos = repos;
35+
}
36+
37+
public int TotalRepos { get; }
38+
public IReadOnlyList<RepositoryInformation> Repos { get; }
39+
}
40+
}

src/NuGetGallery/Views/Packages/DisplayPackage.cshtml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -921,19 +921,20 @@
921921
</tr>
922922
</thead>
923923
<tbody class="no-border">
924-
@foreach (var item in Model.GitHubDependenciesInformation.Repos.Select((elem, i) => new { Value = elem, Idx = i }))
924+
@for (var i = 0; i < Model.GitHubDependenciesInformation.Repos.Count; i++)
925925
{
926+
var repo = Model.GitHubDependenciesInformation.Repos[i];
926927
<tr>
927928
<td class="used-by-desc-column">
928-
<a data-index-number="@item.Idx" class="text-left gh-link" href="@item.Value.Url" target="_blank">
929-
@(item.Value.Id)
929+
<a data-index-number="@i" class="text-left gh-link" href="@repo.Url" target="_blank">
930+
@(repo.Id)
930931
</a>
931932
<div class="row used-by-desc">
932-
<span>@(item.Value.Description)</span>
933+
<span>@(repo.Description)</span>
933934
</div>
934935
</td>
935936
<td>
936-
<i class="ms-Icon ms-Icon--FavoriteStarFill gh-star" aria-hidden="true"></i> <label class="used-by-count">@(item.Value.Stars.ToKiloFormat())</label>
937+
<i class="ms-Icon ms-Icon--FavoriteStarFill gh-star" aria-hidden="true"></i> <label class="used-by-count">@(repo.Stars.ToKiloFormat())</label>
937938
</td>
938939
</tr>
939940
}

tests/NuGetGallery.Facts/ViewModels/DisplayPackageViewModelFacts.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -131,6 +131,30 @@ public void ItDeterminesRepositoryKind(string repoUrl, string repoType, Reposito
131131
Assert.Equal(expectedUrl, model.RepositoryUrl);
132132
}
133133

134+
[Theory]
135+
[InlineData("https://github.com/dotnet/sqlclient", "git", "dotnet/SqlClient")]
136+
[InlineData("https://github.com/jbogard/MediatR.git", "git", "jbogard/MediatR")]
137+
[InlineData("git://github.com/AppMetrics/AppMetrics", null, "AppMetrics/AppMetrics")]
138+
[InlineData("https://github.com/Azure/durabletask/", "git", "Azure/durabletask")]
139+
[InlineData("https://visualstudio.com", "tfs", "")]
140+
public void ItDeterminesComparableGitHubRepository(string repoUrl, string repoType, string ComparableGitHubRepository)
141+
{
142+
var package = new Package
143+
{
144+
Version = "1.0.0",
145+
RepositoryUrl = repoUrl,
146+
RepositoryType = repoType,
147+
PackageRegistration = new PackageRegistration
148+
{
149+
Owners = Enumerable.Empty<User>().ToList(),
150+
Packages = Enumerable.Empty<Package>().ToList()
151+
}
152+
};
153+
154+
var model = CreateDisplayPackageViewModel(package, currentUser: null, packageKeyToDeprecation: null, readmeHtml: null);
155+
Assert.Equal(ComparableGitHubRepository, model.ComparableGitHubRepository, StringComparer.OrdinalIgnoreCase);
156+
}
157+
134158
[Theory]
135159
[InlineData(null, null)]
136160
[InlineData("", null)]

0 commit comments

Comments
 (0)