Skip to content

Commit

Permalink
Added GitHubActionsLintAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Nov 20, 2023
1 parent aac8330 commit 1db8ab0
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 39 deletions.
22 changes: 2 additions & 20 deletions .build/Build.CI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -120,27 +119,10 @@ public static RocketSurgeonGitHubActionsConfiguration CiMiddleware(RocketSurgeon

public static RocketSurgeonGitHubActionsConfiguration LintStagedMiddleware(RocketSurgeonGitHubActionsConfiguration configuration)
{
configuration.Permissions.Contents = GitHubActionsPermission.Write;
configuration
.Jobs.OfType<RocketSurgeonsGithubActionsJob>()
.First(z => z.Name.Equals("Build", StringComparison.OrdinalIgnoreCase))
.UseDotNetSdks("6.0", "8.0")
.AddNuGetCache()
.ConfigureStep<CheckoutStep>(
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;
}
Expand Down
14 changes: 2 additions & 12 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# - To turn off auto-generation set:
#
# [GitHubActionsSteps (AutoGenerate = false)]
# [GitHubActionsLint (AutoGenerate = false)]
#
# - To trigger manual generation invoke:
#
Expand Down Expand Up @@ -38,9 +38,7 @@ permissions:
statuses: write

jobs:
build:
env:
NUGET_PACKAGES: '${{ github.workspace }}/.nuget/packages'
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -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:
Expand Down
79 changes: 79 additions & 0 deletions src/Nuke/GithubActions/GitHubActionsLintAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Nuke.Common.CI;
using Nuke.Common.CI.GitHubActions;
using Nuke.Common.Execution;

namespace Rocket.Surgery.Nuke.GithubActions;

/// <summary>
/// An attribute to help adding the lint workflow
/// </summary>
[PublicAPI]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class GitHubActionsLintAttribute : GitHubActionsStepsAttribute
{
/// <summary>
/// The default constructor
/// </summary>
/// <param name="name"></param>
/// <param name="image"></param>
/// <param name="images"></param>
public GitHubActionsLintAttribute(
string name,
GitHubActionsImage image,
params GitHubActionsImage[] images
) : base(name, image, images)
{
InvokedTargets = new[] { nameof(ICanLintStagedFiles.LintStaged) };
}

/// <summary>
/// The default constructor
/// </summary>
/// <param name="name"></param>
/// <param name="image"></param>
/// <param name="images"></param>
public GitHubActionsLintAttribute(
string name,
string image,
params string[] images
) : base(name, image, images)
{
}

/// <inheritdoc />
public override ConfigurationEntity GetConfiguration(IReadOnlyCollection<ExecutableTarget> relevantTargets)
{
var config = base.GetConfiguration(relevantTargets);
if (config is not RocketSurgeonGitHubActionsConfiguration configuration)
{
return config;
}

var buildJob =
configuration
.Jobs.OfType<RocketSurgeonsGithubActionsJob>()
.First(z => z.Name.Equals("Build", StringComparison.OrdinalIgnoreCase));

configuration.Permissions.Contents = GitHubActionsPermission.Write;

buildJob
.ConfigureStep<CheckoutStep>(
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;
}
}
8 changes: 4 additions & 4 deletions src/Nuke/GithubActions/GitHubActionsStepsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public override ConfigurationEntity GetConfiguration(IReadOnlyCollection<Executa

var environmentVariables =
GetAllSecrets(secrets)
// ReSharper disable once CoVariantArrayConversion
// ReSharper disable once CoVariantArrayConversion
.Concat<ITriggerValue>(variables)
// ReSharper disable once CoVariantArrayConversion
// ReSharper disable once CoVariantArrayConversion
.Concat(environmentAttributes)
.SelectMany(
z =>
Expand Down Expand Up @@ -183,8 +183,8 @@ public override ConfigurationEntity GetConfiguration(IReadOnlyCollection<Executa
var lookupTable = new LookupTable<ExecutableTarget, ExecutableTarget[]>();
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()))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected RocketSurgeonsGithubActionsJobBase(string name)
/// <summary>
/// The name of the job
/// </summary>
public string Name { get; }
public string Name { get; set; }

/// <summary>
/// The dependencies of this job
Expand Down
1 change: 1 addition & 0 deletions src/Nuke/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/Nuke/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#nullable enable
Rocket.Surgery.Nuke.ICanLintStagedFiles.LintStaged.get -> Nuke.Common.Target!
#nullable enable

0 comments on commit 1db8ab0

Please sign in to comment.