Skip to content

Commit

Permalink
Added trigger values to the attributes, and concurrency / default con… (
Browse files Browse the repository at this point in the history
#1162)

* Added trigger values to the attributes, and concurrency / default configurations

* Updated default concurrency for lint
  • Loading branch information
david-driscoll committed Mar 25, 2024
1 parent 39726ba commit 0c9b669
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ permissions:
repository-projects: none
security-events: none
statuses: write
concurrency:
group: 'lint-${{ github.event.pull_request.number }}'
cancel-in-progress: true

jobs:
lint:
Expand Down
6 changes: 3 additions & 3 deletions src/Nuke/GithubActions/CustomFileWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public static void WriteKeyValues(this CustomFileWriter writer, string key, IDic
}
}

private static void WriteValue(CustomFileWriter writer, KeyValuePair<string, string> kvp)
internal static void WriteValue(this CustomFileWriter writer, KeyValuePair<string, string> kvp)
{
var (key, value) = kvp;
( var key, var value ) = kvp;
if (value.StartsWith('>') || value.StartsWith('|'))
{
var values = value.Split('\n');
Expand All @@ -52,4 +52,4 @@ private static void WriteValue(CustomFileWriter writer, KeyValuePair<string, str
writer.WriteLine($"{key}: '{value}'");
}
}
}
}
31 changes: 21 additions & 10 deletions src/Nuke/GithubActions/GitHubActionsInputAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ namespace Rocket.Surgery.Nuke.GithubActions;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class GitHubActionsInputAttribute : TriggerValueAttribute
{
/// <summary>
/// The constructor for the <see cref="GitHubActionsInputAttribute" />
/// </summary>
/// <param name="name"></param>
public GitHubActionsInputAttribute(string name) : base(name) { }

/// <summary>
/// The type of the input
/// </summary>
Expand All @@ -16,20 +22,25 @@ public sealed class GitHubActionsInputAttribute : TriggerValueAttribute
/// </summary>
public bool? Required { get; set; }

/// <summary>
/// The constructor for the <see cref="GitHubActionsInputAttribute" />
/// </summary>
/// <param name="name"></param>
public GitHubActionsInputAttribute(string name) : base(name)
{
}

/// <summary>
/// Convert the attribute into an input
/// </summary>
/// <returns></returns>
public GitHubActionsInput ToInput()
{
return new GitHubActionsInput(Name, Type, Default, Description, Required, Alias);
return new(
Name,
Type,
Default,
Description,
Required,
Alias
);
}

/// <inheritdoc />
public override ITriggerValue ToTriggerValue()
{
return ToInput();
}
}
}
21 changes: 19 additions & 2 deletions src/Nuke/GithubActions/GitHubActionsLintAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using Nuke.Common.CI;
using Nuke.Common.CI.GitHubActions;
using Nuke.Common.Execution;
Expand Down Expand Up @@ -66,11 +67,27 @@ public override ConfigurationEntity GetConfiguration(IReadOnlyCollection<Executa

const string commitMessage = "Automatically linting code";

var secretPath = Build
.GetType()
.GetCustomAttributes()
.OfType<TriggerValueAttribute>()
.Where(z => z.Name == TokenSecret)
.Select(z => z.ToTriggerValue())
.Select(value => string.IsNullOrWhiteSpace(value.Prefix) ? value.Name : $"{value.Prefix}.{value.Name}")
.FirstOrDefault()
?? $$$"""secrets.{{{TokenSecret}}}""";

configuration.Concurrency = new()
{
CancelInProgress = true,
Group = "lint-${{ github.event.pull_request.number }}",
};

buildJob
.ConfigureStep<CheckoutStep>(
step =>
{
step.Token ??= $$$"""${{ secrets.{{{TokenSecret}}} }}""";
step.Token ??= $$$"""${{ {{{secretPath}}} }}""";
step.FetchDepth = 0;
step.Repository = "${{ github.event.pull_request.head.repo.full_name }}";
step.Ref = "${{ github.event.pull_request.head.ref }}";
Expand All @@ -91,7 +108,7 @@ public override ConfigurationEntity GetConfiguration(IReadOnlyCollection<Executa
["repo"] = "${{ github.repository }}",
["branch"] = "${{ github.event.pull_request.head.ref }}",
},
Environment = { ["GITHUB_TOKEN"] = $$$"""${{ secrets.{{{TokenSecret}}} }}""", },
Environment = { ["GITHUB_TOKEN"] = $$$"""${{ {{{secretPath}}} }}""", },
}
);

Expand Down
22 changes: 13 additions & 9 deletions src/Nuke/GithubActions/GitHubActionsSecretAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ namespace Rocket.Surgery.Nuke.GithubActions;
public sealed class GitHubActionsSecretAttribute : TriggerValueAttribute
{
/// <summary>
/// Is the secret required
/// The constructor for the <see cref="GitHubActionsSecretAttribute" />
/// </summary>
public bool? Required { get; set; }
/// <param name="name"></param>
public GitHubActionsSecretAttribute(string name) : base(name) { }

/// <summary>
/// The constructor for the <see cref="GitHubActionsSecretAttribute" />
/// Is the secret required
/// </summary>
/// <param name="name"></param>
public GitHubActionsSecretAttribute(string name) : base(name)
{
}
public bool? Required { get; set; }

/// <summary>
/// Convert to a secret
/// </summary>
/// <returns></returns>
public GitHubActionsSecret ToSecret()
{
return new GitHubActionsSecret(Name, Description, Required, Alias);
return new(Name, Description, Required, Alias);
}

/// <inheritdoc />
public override ITriggerValue ToTriggerValue()
{
return ToSecret();
}
}
}
22 changes: 13 additions & 9 deletions src/Nuke/GithubActions/GitHubActionsVariableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ namespace Rocket.Surgery.Nuke.GithubActions;
public sealed class GitHubActionsVariableAttribute : TriggerValueAttribute
{
/// <summary>
/// Is the variable required
/// The constructor for the <see cref="GitHubActionsVariableAttribute" />
/// </summary>
public bool? Required { get; set; }
/// <param name="name"></param>
public GitHubActionsVariableAttribute(string name) : base(name) { }

/// <summary>
/// The constructor for the <see cref="GitHubActionsVariableAttribute" />
/// Is the variable required
/// </summary>
/// <param name="name"></param>
public GitHubActionsVariableAttribute(string name) : base(name)
{
}
public bool? Required { get; set; }

/// <summary>
/// Convert to a variable
/// </summary>
/// <returns></returns>
public GitHubActionsVariable ToVariable()
{
return new GitHubActionsVariable(Name, Description, Alias);
return new(Name, Description, Alias);
}

/// <inheritdoc />
public override ITriggerValue ToTriggerValue()
{
return ToVariable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ public OnePasswordConnectServerSecret ToSecret()
ConnectToken ?? "OP_CONNECT_TOKEN"
);
}

/// <inheritdoc />
public override ITriggerValue ToTriggerValue()
{
return ToSecret();
}
}
6 changes: 6 additions & 0 deletions src/Nuke/GithubActions/OnePasswordSecretAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ internal ITriggerValue ToSecret()
Secret ?? "OP_SERVICE_ACCOUNT_TOKEN"
);
}

/// <inheritdoc />
public override ITriggerValue ToTriggerValue()
{
return ToSecret();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ public OnePasswordServiceAccountSecret ToSecret()
Secret ?? "OP_SERVICE_ACCOUNT_TOKEN"
);
}

/// <inheritdoc />
public override ITriggerValue ToTriggerValue()
{
return ToSecret();
}
}
27 changes: 24 additions & 3 deletions src/Nuke/GithubActions/RocketSurgeonGitHubActionsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,22 @@ public class RocketSurgeonGitHubActionsConfiguration : ConfigurationEntity
public List<RocketSurgeonsGithubActionsJobBase> Jobs { get; set; } = new();

/// <summary>
/// The dependencies of this job
/// The dependencies of this workflow
/// </summary>
public Dictionary<string, string> Environment { get; set; } = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// The permissions of this workflow
/// The concurrency of the workflow
/// </summary>
public RocketSurgeonsGithubActionsConcurrency? Concurrency { get; set; }

/// <summary>
/// The defaults of the job
/// </summary>
public RocketSurgeonsGithubActionsDefaults? Defaults { get; set; }

/// <summary>
/// The permissions of this workflow
/// </summary>
public GitHubActionsPermissions Permissions { get; set; } = new();

Expand All @@ -66,6 +76,17 @@ public override void Write(CustomFileWriter writer)
Permissions.Write(writer);

writer.WriteKeyValues("env", Environment);
if (Concurrency is { } concurrency)
{
writer.WriteLine("concurrency:");
concurrency.Write(writer);
}

if (Defaults is { } defaults)
{
writer.WriteLine("defaults:");
defaults.Write(writer);
}

writer.WriteLine();

Expand All @@ -75,4 +96,4 @@ public override void Write(CustomFileWriter writer)
Jobs.ForEach(x => x.Write(writer));
}
}
}
}
34 changes: 34 additions & 0 deletions src/Nuke/GithubActions/RocketSurgeonsGithubActionsConcurrency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Nuke.Common.CI;

namespace Rocket.Surgery.Nuke.GithubActions;

/// <summary>
/// The concurrency settings for a workflow or job
/// </summary>
public class RocketSurgeonsGithubActionsConcurrency : ConfigurationEntity
{
/// <summary>
/// The concurrency group
/// </summary>
public string? Group { get; set; }

/// <summary>
/// Cancel existing jobs
/// </summary>
public bool? CancelInProgress { get; set; }

/// <inheritdoc />
public override void Write(CustomFileWriter writer)
{
using var _ = writer.Indent();
if (!string.IsNullOrWhiteSpace(Group))
{
writer.WriteValue(new("group", Group));
}

if (CancelInProgress.HasValue)
{
writer.WriteLine($"cancel-in-progress: {CancelInProgress.Value.ToString().ToLowerInvariant()}");
}
}
}
23 changes: 23 additions & 0 deletions src/Nuke/GithubActions/RocketSurgeonsGithubActionsDefaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Nuke.Common.CI;

namespace Rocket.Surgery.Nuke.GithubActions;

/// <summary>
/// The job defaults
/// </summary>
public class RocketSurgeonsGithubActionsDefaults : ConfigurationEntity
{
/// <summary>
/// The defaults of run
/// </summary>
public RocketSurgeonsGithubActionsDefaultsRun? Run { get; set; }

/// <inheritdoc />
public override void Write(CustomFileWriter writer)
{
using var _ = writer.Indent();
if (Run is null) return;
writer.WriteLine("run:");
Run.Write(writer);
}
}
28 changes: 28 additions & 0 deletions src/Nuke/GithubActions/RocketSurgeonsGithubActionsDefaultsRun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Nuke.Common.CI;

namespace Rocket.Surgery.Nuke.GithubActions;

/// <summary>
/// The job run defaults
/// </summary>
public class RocketSurgeonsGithubActionsDefaultsRun : ConfigurationEntity
{
/// <summary>
/// The shell
/// </summary>
public GithubActionShell? Shell { get; set; }

/// <summary>
/// The working directory where the script is run
/// </summary>
public string? WorkingDirectory { get; set; }

public override void Write(CustomFileWriter writer)
{
using var _ = writer.Indent();
if (!string.IsNullOrWhiteSpace(WorkingDirectory))
writer.WriteLine($"working-directory: {WorkingDirectory}");
if (!string.IsNullOrWhiteSpace(Shell?.ToString()))
writer.WriteLine($"shell: {Shell}");
}
}
Loading

0 comments on commit 0c9b669

Please sign in to comment.