From 1db8ab0a92c1f96b3e0f3cc8e3a67c03b9bf2063 Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Mon, 20 Nov 2023 18:10:39 -0500 Subject: [PATCH] Added GitHubActionsLintAttribute --- .build/Build.CI.cs | 22 +----- .github/workflows/lint.yml | 14 +--- .../GitHubActionsLintAttribute.cs | 79 +++++++++++++++++++ .../GitHubActionsStepsAttribute.cs | 8 +- .../RocketSurgeonsGithubActionsJobBase.cs | 2 +- src/Nuke/PublicAPI.Shipped.txt | 1 + src/Nuke/PublicAPI.Unshipped.txt | 3 +- 7 files changed, 90 insertions(+), 39 deletions(-) create mode 100644 src/Nuke/GithubActions/GitHubActionsLintAttribute.cs diff --git a/.build/Build.CI.cs b/.build/Build.CI.cs index b1cd8e1ea..5d17ccb09 100644 --- a/.build/Build.CI.cs +++ b/.build/Build.CI.cs @@ -46,12 +46,11 @@ ExcludedTargets = new[] { nameof(ICanClean.Clean), nameof(ICanRestoreWithDotNetCore.DotnetToolRestore) }, Enhancements = new[] { nameof(CiMiddleware) } )] -[GitHubActionsSteps( +[GitHubActionsLint( "lint", GitHubActionsImage.UbuntuLatest, AutoGenerate = false, OnPullRequestBranches = new[] { "master", "main", "next" }, - InvokedTargets = new[] { nameof(ICanLintStagedFiles.LintStaged) }, Enhancements = new[] { nameof(LintStagedMiddleware) } )] [GitHubActionsSteps( @@ -120,27 +119,10 @@ public static RocketSurgeonGitHubActionsConfiguration CiMiddleware(RocketSurgeon public static RocketSurgeonGitHubActionsConfiguration LintStagedMiddleware(RocketSurgeonGitHubActionsConfiguration configuration) { - configuration.Permissions.Contents = GitHubActionsPermission.Write; configuration .Jobs.OfType() .First(z => z.Name.Equals("Build", StringComparison.OrdinalIgnoreCase)) - .UseDotNetSdks("6.0", "8.0") - .AddNuGetCache() - .ConfigureStep( - step => - { - step.FetchDepth = 0; - step.Repository = "${{ github.event.pull_request.head.repo.full_name }}"; - step.Ref = "${{ github.event.pull_request.head.ref }}"; - } - ) - .AddStep( - new UsingStep("Add & Commit") - { - Uses = "EndBug/add-and-commit@v9", - With = { ["message"] = "Automatically linting code", } - } - ); + .UseDotNetSdks("6.0", "8.0"); return configuration; } diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 2399de213..5a685d730 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -5,7 +5,7 @@ # # - To turn off auto-generation set: # -# [GitHubActionsSteps (AutoGenerate = false)] +# [GitHubActionsLint (AutoGenerate = false)] # # - To trigger manual generation invoke: # @@ -38,9 +38,7 @@ permissions: statuses: write jobs: - build: - env: - NUGET_PACKAGES: '${{ github.workspace }}/.nuget/packages' + lint: runs-on: ubuntu-latest steps: - name: Checkout @@ -49,14 +47,6 @@ jobs: repository: '${{ github.event.pull_request.head.repo.full_name }}' ref: '${{ github.event.pull_request.head.ref }}' clean: 'false' - fetch-depth: '0' - - name: NuGet Cache - uses: actions/cache@v2 - with: - path: '${{ github.workspace }}/.nuget/packages' - key: "${{ runner.os }}-nuget-${{ hashFiles('**/Directory.Packages.props') }}-${{ hashFiles('**/Directory.Packages.support.props') }}" - restore-keys: | - ${{ runner.os }}-nuget- - name: 🔨 Use .NET Core 6.0 SDK uses: actions/setup-dotnet@v1 with: diff --git a/src/Nuke/GithubActions/GitHubActionsLintAttribute.cs b/src/Nuke/GithubActions/GitHubActionsLintAttribute.cs new file mode 100644 index 000000000..fedeeebca --- /dev/null +++ b/src/Nuke/GithubActions/GitHubActionsLintAttribute.cs @@ -0,0 +1,79 @@ +using Nuke.Common.CI; +using Nuke.Common.CI.GitHubActions; +using Nuke.Common.Execution; + +namespace Rocket.Surgery.Nuke.GithubActions; + +/// +/// An attribute to help adding the lint workflow +/// +[PublicAPI] +[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] +public class GitHubActionsLintAttribute : GitHubActionsStepsAttribute +{ + /// + /// The default constructor + /// + /// + /// + /// + public GitHubActionsLintAttribute( + string name, + GitHubActionsImage image, + params GitHubActionsImage[] images + ) : base(name, image, images) + { + InvokedTargets = new[] { nameof(ICanLintStagedFiles.LintStaged) }; + } + + /// + /// The default constructor + /// + /// + /// + /// + public GitHubActionsLintAttribute( + string name, + string image, + params string[] images + ) : base(name, image, images) + { + } + + /// + public override ConfigurationEntity GetConfiguration(IReadOnlyCollection relevantTargets) + { + var config = base.GetConfiguration(relevantTargets); + if (config is not RocketSurgeonGitHubActionsConfiguration configuration) + { + return config; + } + + var buildJob = + configuration + .Jobs.OfType() + .First(z => z.Name.Equals("Build", StringComparison.OrdinalIgnoreCase)); + + configuration.Permissions.Contents = GitHubActionsPermission.Write; + + buildJob + .ConfigureStep( + step => + { + step.Repository = "${{ github.event.pull_request.head.repo.full_name }}"; + step.Ref = "${{ github.event.pull_request.head.ref }}"; + } + ) + .AddStep( + new UsingStep("Add & Commit") + { + Uses = "EndBug/add-and-commit@v9", + With = { ["message"] = "Automatically linting code", } + } + ); + + buildJob.Name = "lint"; + + return configuration; + } +} diff --git a/src/Nuke/GithubActions/GitHubActionsStepsAttribute.cs b/src/Nuke/GithubActions/GitHubActionsStepsAttribute.cs index b5e1f1ac5..b75dd197b 100644 --- a/src/Nuke/GithubActions/GitHubActionsStepsAttribute.cs +++ b/src/Nuke/GithubActions/GitHubActionsStepsAttribute.cs @@ -139,9 +139,9 @@ public override ConfigurationEntity GetConfiguration(IReadOnlyCollection(variables) - // ReSharper disable once CoVariantArrayConversion + // ReSharper disable once CoVariantArrayConversion .Concat(environmentAttributes) .SelectMany( z => @@ -183,8 +183,8 @@ public override ConfigurationEntity GetConfiguration(IReadOnlyCollection(); foreach (var (execute, targets) in relevantTargets .Select( - x => (ExecutableTarget: x, - Targets: GetInvokedTargets(x, relevantTargets).ToArray()) + x => ( ExecutableTarget: x, + Targets: GetInvokedTargets(x, relevantTargets).ToArray() ) ) .ForEachLazy(x => lookupTable.Add(x.ExecutableTarget, x.Targets.ToArray())) ) diff --git a/src/Nuke/GithubActions/RocketSurgeonsGithubActionsJobBase.cs b/src/Nuke/GithubActions/RocketSurgeonsGithubActionsJobBase.cs index 93c1536dc..80f96977c 100644 --- a/src/Nuke/GithubActions/RocketSurgeonsGithubActionsJobBase.cs +++ b/src/Nuke/GithubActions/RocketSurgeonsGithubActionsJobBase.cs @@ -23,7 +23,7 @@ protected RocketSurgeonsGithubActionsJobBase(string name) /// /// The name of the job /// - public string Name { get; } + public string Name { get; set; } /// /// The dependencies of this job diff --git a/src/Nuke/PublicAPI.Shipped.txt b/src/Nuke/PublicAPI.Shipped.txt index 03e21d7ff..868ebe288 100644 --- a/src/Nuke/PublicAPI.Shipped.txt +++ b/src/Nuke/PublicAPI.Shipped.txt @@ -496,6 +496,7 @@ Rocket.Surgery.Nuke.ICanLint Rocket.Surgery.Nuke.ICanLint.Lint.get -> Nuke.Common.Target! Rocket.Surgery.Nuke.ICanLint.PostLint.get -> Nuke.Common.Target! Rocket.Surgery.Nuke.ICanLintStagedFiles.LintStaged.get -> Nuke.Common.Target! +Rocket.Surgery.Nuke.ICanLintStagedFiles.LintStaged.get -> Nuke.Common.Target! Rocket.Surgery.Nuke.ICanPrettier Rocket.Surgery.Nuke.ICanPrettier.Prettier.get -> Nuke.Common.Target! Rocket.Surgery.Nuke.ICanRegenerateBuildConfiguration diff --git a/src/Nuke/PublicAPI.Unshipped.txt b/src/Nuke/PublicAPI.Unshipped.txt index 1ca1aac69..815c92006 100644 --- a/src/Nuke/PublicAPI.Unshipped.txt +++ b/src/Nuke/PublicAPI.Unshipped.txt @@ -1,2 +1 @@ -#nullable enable -Rocket.Surgery.Nuke.ICanLintStagedFiles.LintStaged.get -> Nuke.Common.Target! \ No newline at end of file +#nullable enable \ No newline at end of file