Skip to content

Commit

Permalink
Update nuget packages and include the url a license is downloaded fro…
Browse files Browse the repository at this point in the history
…m in the exception thrown when that fails (#99)
  • Loading branch information
sensslen authored Nov 1, 2024
1 parent 103d524 commit 044241a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/NuGetUtility/LicenseValidator/LicenseDownloadException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace NuGetUtility.LicenseValidator
{
public class LicenseDownloadException : Exception
{
public LicenseDownloadException(Exception inner, string context, PackageIdentity packageInfo)
public LicenseDownloadException(Exception inner, string context, Uri url, PackageIdentity packageInfo)
:
base(
$"Failed to download license for package {packageInfo.Id} ({packageInfo.Version}).\nContext: {context}",
$"Failed to download license for package {packageInfo.Id} ({packageInfo.Version}) from url: {url}.\nContext: {context}",
inner)
{ }
}
Expand Down
14 changes: 7 additions & 7 deletions src/NuGetUtility/LicenseValidator/LicenseValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private bool IsIgnoredPackage(PackageIdentity identity)
return Array.Exists(_ignoredPackages, ignored => identity.Id.Like(ignored));
}

private void AddOrUpdateLicense(
private static void AddOrUpdateLicense(
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result,
IPackageMetadata info,
LicenseInformationOrigin origin,
Expand All @@ -90,7 +90,7 @@ private void AddOrUpdateLicense(
(key, oldValue) => UpdateResult(oldValue, newValue));
}

private void AddOrUpdateLicense(
private static void AddOrUpdateLicense(
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result,
IPackageMetadata info,
LicenseInformationOrigin origin,
Expand All @@ -110,7 +110,7 @@ private void AddOrUpdateLicense(
(key, oldValue) => UpdateResult(oldValue, newValue));
}

private LicenseValidationResult UpdateResult(LicenseValidationResult oldValue,
private static LicenseValidationResult UpdateResult(LicenseValidationResult oldValue,
LicenseValidationResult newValue)
{
oldValue.ValidationErrors.AddRange(newValue.ValidationErrors);
Expand Down Expand Up @@ -244,7 +244,7 @@ await _fileDownloader.DownloadFile(licenseUrl,
}
catch (Exception e)
{
throw new LicenseDownloadException(e, context, identity);
throw new LicenseDownloadException(e, context, licenseUrl, identity);
}
}

Expand All @@ -259,17 +259,17 @@ private bool IsLicenseValid(string licenseId)
return _allowedLicenses.Any(allowedLicense => allowedLicense.Equals(licenseId));
}

private string GetLicenseNotAllowedMessage(string license)
private static string GetLicenseNotAllowedMessage(string license)
{
return $"License \"{license}\" not found in list of supported licenses";
}

private Uri GetLicenseUrl(string spdxIdentifier)
private static Uri GetLicenseUrl(string spdxIdentifier)
{
return new Uri($"https://licenses.nuget.org/({spdxIdentifier})");
}

private LicenseInformationOrigin ToLicenseOrigin(LicenseType type) => type switch
private static LicenseInformationOrigin ToLicenseOrigin(LicenseType type) => type switch
{
LicenseType.Overwrite => LicenseInformationOrigin.Overwrite,
LicenseType.Expression => LicenseInformationOrigin.Expression,
Expand Down
1 change: 0 additions & 1 deletion src/NuGetUtility/LicenseValidator/UrlToLicenseMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public static class UrlToLicenseMapping
new KeyValuePair<Uri, string>(new Uri("http://go.microsoft.com/fwlink/?linkid=833178"), Mit),
new KeyValuePair<Uri, string>(new Uri("http://www.gnu.org/licenses/old-licenses/gpl-2.0.html"), Gpl20),
new KeyValuePair<Uri, string>(new Uri("https://raw.githubusercontent.com/AArnott/Validation/8377954d86/LICENSE.txt"), MsPl),
new KeyValuePair<Uri, string>(new Uri(" http://opensource.org/licenses/mit-license.php"), Mit),
new KeyValuePair<Uri, string>(new Uri("https://raw.githubusercontent.com/bchavez/Bogus/master/LICENSE"), MitAndBsd3Clause),
new KeyValuePair<Uri, string>(new Uri("https://github.com/Microsoft/dotnet/blob/master/LICENSE"), Mit)
}
Expand Down
41 changes: 15 additions & 26 deletions src/NuGetUtility/Wrapper/HttpClientWrapper/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task DownloadFile(Uri url, string fileNameStem, CancellationToken t
{
return;
}
await Task.Delay((int)Math.Pow(EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS, i + 1), token);
await Task.Delay(EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS * ((int)Math.Pow(2, i)), token);
}
}
finally
Expand All @@ -39,38 +39,20 @@ public async Task DownloadFile(Uri url, string fileNameStem, CancellationToken t
}
}

#if NETFRAMEWORK
private async Task<bool> TryDownload(string fileNameStem, Uri url, CancellationToken _)
#pragma warning disable S1172
private async Task<bool> TryDownload(string fileNameStem, Uri url, CancellationToken token)
#pragma warning restore S1172
{
var request = new HttpRequestMessage(HttpMethod.Get, url);

#if NETFRAMEWORK
HttpResponseMessage response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
// System.Net.HttpStatusCode.TooManyRequests does not exist in .net472
if (response.StatusCode == (System.Net.HttpStatusCode)429)
{
return false;
}
response.EnsureSuccessStatusCode();

string extension = "html";
if (response.Content.Headers.ContentType.MediaType == "text/plain")
{
extension = "txt";
}
string fileName = fileNameStem + "." + extension;
using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName));
using Stream downloadStream = await response.Content.ReadAsStreamAsync();

await downloadStream.CopyToAsync(file);
return true;
}
#else
private async Task<bool> TryDownload(string fileNameStem, Uri url, CancellationToken token)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);

HttpResponseMessage response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
#endif
{
return false;
}
Expand All @@ -81,13 +63,20 @@ private async Task<bool> TryDownload(string fileNameStem, Uri url, CancellationT
{
extension = "txt";
}
string fileName = fileNameStem + "." + extension;
string fileName = $"{fileNameStem}.{extension}";
#if NETFRAMEWORK
using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName));
#else
await using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName));
#endif
using Stream downloadStream = await response.Content.ReadAsStreamAsync();

#if NETFRAMEWORK
await downloadStream.CopyToAsync(file);
#else
await downloadStream.CopyToAsync(file, token);
#endif
return true;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ public void ValidatingLicensesWithUrlInformation_Should_ThrowLicenseDownloadInfo
Assert.ThrowsAsync<LicenseDownloadException>(() => _uut.Validate(CreateInput(package, _context), _token.Token));
Assert.IsInstanceOf<Exception>(exception!.InnerException);
Assert.AreEqual(
$"Failed to download license for package {packageId} ({packageVersion}).\nContext: {_context}",
$"Failed to download license for package {packageId} ({packageVersion}) from url: {urlMatch.Key}.\nContext: {_context}",
exception.Message);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/NuGetUtility.Test/NuGetUtility.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
<PackageReference Include="AutoFixture.NUnit3" Version="4.18.1" />
<PackageReference Include="Bogus" Version="35.6.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Mono.Cecil" Version="0.11.6" />
<PackageReference Include="NetArchTest.Rules" Version="1.3.2" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
</ItemGroup>

</Project>

0 comments on commit 044241a

Please sign in to comment.