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

feat(source): added a configuration source #94

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LABEL repository="https://github.com/KinsonDigital/PackageMonster"
LABEL homepage="https://github.com/KinsonDigital/PackageMonster"

# Label as GitHub action
LABEL com.github.actions.name="Got Nuget"
LABEL com.github.actions.name="Got Package"

# Relayer the .NET SDK, anew with the build output
FROM mcr.microsoft.com/dotnet/sdk:7.0
Expand Down
36 changes: 33 additions & 3 deletions PackageMonster/ActionInputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (c) KinsonDigital. All rights reserved.
// </copyright>

using PackageMonster.Services;

namespace PackageMonster;

/// <summary>
Expand All @@ -19,24 +21,52 @@ public class ActionInputs
public string PackageName { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the NuGet package version to check.
/// Gets or sets the package version to check.
/// </summary>
/// <remarks>
/// Version search is not case-sensitive.
/// </remarks>
[Option(
"version",
Required = true,
HelpText = "The version of the NuGet package to check. This is not case-sensitive.")]
HelpText = "The version of the package to check. This is not case-sensitive.")]
public string Version { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the repository.
/// </summary>
[Option(
"source",
Required = false,
HelpText = "The source repository to check. Defaults to `nuget`.")]
public string Source { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the json path to extract the versions.
/// </summary>
[Option(
"json-path",
Required = false,
HelpText = "The json path to the versions.")]
public string VersionsJsonPath { get; set; } = string.Empty;

/// <summary>
/// Gets or sets a value indicating whether or not the action will fail if the package was not found.
/// </summary>
[Option(
"fail-when-not-found",
Required = false,
Default = false,
HelpText = "If true, will fail the workflow if the NuGet package of the requested version does not exist.")]
HelpText = "If true, will fail the workflow if the package of the requested version does not exist.")]
public bool? FailWhenNotFound { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not the action will fail if the package was found.
/// </summary>
[Option(
"fail-when-found",
Required = false,
Default = false,
HelpText = "If true, will fail the workflow if the package of the requested version does exist.")]
public bool? FailWhenFound { get; set; }
}
34 changes: 0 additions & 34 deletions PackageMonster/Exceptions/NugetNotFoundException.cs

This file was deleted.

30 changes: 30 additions & 0 deletions PackageMonster/Exceptions/PackageFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace PackageMonster.Exceptions;

/// <summary>
/// Occurs when a package is found.
/// </summary>
public class PackageFoundException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="PackageFoundException"/> class.
/// </summary>
public PackageFoundException()
: base("The package was not found.") => HResult = 60;

/// <summary>
/// Initializes a new instance of the <see cref="PackageFoundException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public PackageFoundException(string message)
: base(message) => HResult = 60;

/// <summary>
/// Initializes a new instance of the <see cref="PackageFoundException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="innerException">
/// The <see cref="Exception"/> instance that caused the current exception.
/// </param>
public PackageFoundException(string message, Exception innerException)
: base(message, innerException) => HResult = 60;
}
34 changes: 34 additions & 0 deletions PackageMonster/Exceptions/PackageNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="PackageNotFoundException.cs" company="KinsonDigital">
// Copyright (c) KinsonDigital. All rights reserved.
// </copyright>

namespace PackageMonster.Exceptions;

/// <summary>
/// Occurs when a package is not found.
/// </summary>
public class PackageNotFoundException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="PackageNotFoundException"/> class.
/// </summary>
public PackageNotFoundException()
: base("The package was not found.") => HResult = 60;

/// <summary>
/// Initializes a new instance of the <see cref="PackageNotFoundException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public PackageNotFoundException(string message)
: base(message) => HResult = 60;

/// <summary>
/// Initializes a new instance of the <see cref="PackageNotFoundException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="innerException">
/// The <see cref="Exception"/> instance that caused the current exception.
/// </param>
public PackageNotFoundException(string message, Exception innerException)
: base(message, innerException) => HResult = 60;
}
37 changes: 27 additions & 10 deletions PackageMonster/GitHubAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ namespace PackageMonster;
public sealed class GitHubAction : IGitHubAction
{
private readonly IGitHubConsoleService gitHubConsoleService;
private readonly INugetDataService nugetDataService;
private readonly IDataService dataService;
private readonly IActionOutputService actionOutputService;
private bool isDisposed;

/// <summary>
/// Initializes a new instance of the <see cref="GitHubAction"/> class.
/// </summary>
/// <param name="gitHubConsoleService">Writes to the console.</param>
/// <param name="nugetDataService">Provides access to NuGet data.</param>
/// <param name="dataService">Provides access to data.</param>
/// <param name="actionOutputService">Sets the output data of the action.</param>
public GitHubAction(
IGitHubConsoleService gitHubConsoleService,
INugetDataService nugetDataService,
IDataService dataService,
IActionOutputService actionOutputService)
{
this.gitHubConsoleService = gitHubConsoleService;
this.nugetDataService = nugetDataService;
this.dataService = dataService;
this.actionOutputService = actionOutputService;
}

Expand All @@ -38,32 +38,49 @@ public async Task Run(ActionInputs inputs, Action onCompleted, Action<Exception>

try
{
if (string.IsNullOrWhiteSpace(inputs.PackageName))
{
throw new ArgumentException("Package name is empty!");
}

if (string.IsNullOrWhiteSpace(inputs.Version))
{
throw new ArgumentException("Version is empty!");
}

this.gitHubConsoleService.Write($"Searching for package '{inputs.PackageName} v{inputs.Version}' . . . ");
var versions = await this.nugetDataService.GetNugetVersions(inputs.PackageName);
var versions = await this.dataService.GetVersions(inputs.PackageName, inputs.Source, inputs.VersionsJsonPath);

var versionFound = versions
.Any(version =>
string.Equals(version, inputs.Version, StringComparison.CurrentCultureIgnoreCase));

var searchEndMsg = versionFound ? "package found!!" : "package not found!!";
var searchEndMsg = versionFound ? "package found!" : "package not found!";

this.gitHubConsoleService.WriteLine(searchEndMsg);
this.gitHubConsoleService.BlankLine();

this.actionOutputService.SetOutputValue("nuget-exists", versionFound.ToString().ToLower());
this.actionOutputService.SetOutputValue("package-exists", versionFound.ToString().ToLower());

var emoji = inputs.FailWhenNotFound is false
? "✅"
: string.Empty;

var foundResultMsg = $"{emoji}The NuGet package '{inputs.PackageName}'";
var foundResultMsg = $"{emoji}The package '{inputs.PackageName}'";
foundResultMsg += $" with the version 'v{inputs.Version}' was{(versionFound ? string.Empty : " not")} found.";

if (versionFound is false)
{
if (inputs.FailWhenNotFound is true)
{
throw new NugetNotFoundException(foundResultMsg);
throw new PackageNotFoundException(foundResultMsg);
}
}
else
{
if (inputs.FailWhenFound is true)
{
throw new PackageFoundException(foundResultMsg);
}
}

Expand All @@ -87,7 +104,7 @@ public void Dispose()
return;
}

this.nugetDataService.Dispose();
this.dataService.Dispose();

this.isDisposed = true;
}
Expand Down
19 changes: 0 additions & 19 deletions PackageMonster/Models/NugetVersionsModel.cs

This file was deleted.

5 changes: 3 additions & 2 deletions PackageMonster/PackageMonster.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
<Nullable>enable</Nullable>

<!--Update this for production and preview releases-->
<Version>1.0.0-preview.2</Version>
<Version>1.0.0-preview.3</Version>

<!--Update this for production and preview releases-->
<FileVersion>1.0.0-preview.2</FileVersion>

<Authors>Calvin Wilkinson</Authors>
<Company>Kinson Digital</Company>
<Product>PackageMonster</Product>
<Description>Custom GitHub action used to check if nuget.org contains a NuGet package of a particular version.</Description>
<Description>Custom GitHub action used to check if a package repository, like nuget.org, contains a package of a particular version.</Description>
<Copyright>Copyright ©2023 Kinson Digital</Copyright>
</PropertyGroup>

Expand All @@ -27,6 +27,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
Expand Down
6 changes: 5 additions & 1 deletion PackageMonster/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// </copyright>

using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using PackageMonster.Services;

namespace PackageMonster;
Expand All @@ -27,9 +28,12 @@ public static async Task Main(string[] args)
{
services.AddSingleton<IAppService, AppService>();
services.AddSingleton<IGitHubConsoleService, GitHubConsoleService>();
services.AddSingleton<IEnvVarService, EnvVarService>();
services.AddSingleton<IActionOutputService, ActionOutputService>();
services.AddSingleton<IFile, FileWrapper>();
services.AddSingleton<IFileSystem, FileSystem>();
services.AddSingleton<IArgParsingService<ActionInputs>, ArgParsingService>();
services.AddSingleton<INugetDataService, NugetDataService>();
services.AddSingleton<IDataService, DataService>();
services.AddSingleton<IGitHubAction, GitHubAction>();
}).Build();

Expand Down
7 changes: 7 additions & 0 deletions PackageMonster/Repositories/CustomPackageRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PackageMonster.Repositories;

internal class CustomPackageRepository : IPackageRepository
{
public string Url { get; set; }
public string JsonPath { get; set; }
}
7 changes: 7 additions & 0 deletions PackageMonster/Repositories/IPackageRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PackageMonster.Repositories;

internal interface IPackageRepository
{
string Url { get; }
string JsonPath { get; }
}
7 changes: 7 additions & 0 deletions PackageMonster/Repositories/NpmPackageRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PackageMonster.Repositories;

internal class NpmPackageRepository : IPackageRepository
{
public string Url => "https://registry.npmjs.org/PACKAGE-NAME";
public string JsonPath => "$.versions.*.version";
}
12 changes: 12 additions & 0 deletions PackageMonster/Repositories/NugetPackageRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace PackageMonster.Repositories;

/* Resources:
* These links refer to the documentation for the Nuget API
* 1. Package Content: https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource
* 2. Server API: https://docs.microsoft.com/en-us/nuget/api/overview
*/
internal class NugetPackageRepository : IPackageRepository
{
public string Url => "https://api.nuget.org/v3-flatcontainer/PACKAGE-NAME/index.json";
public string JsonPath => "$.versions[*]";
}
Loading