Add JS-specific bits to Actions

This commit is contained in:
Luc Perkins 2024-04-26 14:19:53 -03:00
parent 539b7a6481
commit 239b4c9810
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
8 changed files with 1036 additions and 82 deletions

View file

@ -1,3 +1,4 @@
import { makeNixCommandArgs } from "./nix.js";
import * as actionsCore from "@actions/core";
import * as actionsExec from "@actions/exec";
import { ActionOptions, IdsToolbox, inputs } from "detsys-ts";
@ -7,6 +8,8 @@ const EVENT_EXECUTION_FAILURE = "execution_failure";
class UpdateFlakeLockAction {
idslib: IdsToolbox;
private commitMessage: string;
private nixOptions: string[];
private flakeInputs: string[];
private pathToFlakeDir: string | null;
constructor() {
@ -18,21 +21,14 @@ class UpdateFlakeLockAction {
this.idslib = new IdsToolbox(options);
this.commitMessage = inputs.getString("commit-msg");
this.flakeInputs = inputs.getCommaSeparatedArrayOfStrings("inputs", true);
this.nixOptions = inputs.getCommaSeparatedArrayOfStrings(
"nix-options",
true,
);
this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
}
get flakeInputs(): string[] {
const targets: string[] = [];
for (const input of inputs.getString("inputs").split(",")) {
targets.concat(["--update-input", input]);
}
return targets;
}
get nixOptions(): string[] {
return inputs.getString("nix-options").split(",");
}
async update(): Promise<void> {
if (this.pathToFlakeDir !== null) {
const returnCode = await actionsExec.exec("cd", [this.pathToFlakeDir]);
@ -51,16 +47,15 @@ class UpdateFlakeLockAction {
// Example commands:
// nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lock-file-summary "updated flake.lock"
// nix flake lock --commit-lock-file --commit-lock-file-summary "updated flake.lock"
const nixCommandArgs: string[] = this.nixOptions
.concat(["flake", "lock"])
.concat(this.flakeInputs)
.concat([
"--commit-lock-file",
"--commit-lock-file-summary",
this.commitMessage,
]);
const nixCommandArgs: string[] = makeNixCommandArgs(
this.nixOptions,
this.flakeInputs,
this.commitMessage,
);
actionsCore.debug(`running nix command:\nnix ${nixCommandArgs.join(" ")}`);
// Solely for debugging
const fullNixCommand = `nix ${nixCommandArgs.join(" ")}`;
actionsCore.debug(`running nix command:\n${fullNixCommand}`);
const exitCode = await actionsExec.exec("nix", nixCommandArgs);

74
src/nix.test.ts Normal file
View file

@ -0,0 +1,74 @@
import { makeNixCommandArgs } from "./nix.js";
import { expect, test } from "vitest";
type TestCase = {
inputs: {
nixOptions: string[];
flakeInputs: string[];
commitMessage: string;
};
expected: string[];
};
test("Nix command arguments", () => {
const testCases: TestCase[] = [
{
inputs: {
nixOptions: ["--log-format", "raw"],
flakeInputs: [],
commitMessage: "just testing",
},
expected: [
"--log-format",
"raw",
"flake",
"lock",
"--commit-lock-file",
"--commit-lock-file-summary",
'"just testing"',
],
},
{
inputs: {
nixOptions: [],
flakeInputs: ["nixpkgs", "rust-overlay"],
commitMessage: "just testing",
},
expected: [
"flake",
"lock",
"--update-input",
"nixpkgs",
"--update-input",
"rust-overlay",
"--commit-lock-file",
"--commit-lock-file-summary",
'"just testing"',
],
},
{
inputs: {
nixOptions: ["--debug"],
flakeInputs: [],
commitMessage: "just testing",
},
expected: [
"--debug",
"flake",
"lock",
"--commit-lock-file",
"--commit-lock-file-summary",
'"just testing"',
],
},
];
testCases.forEach(({ inputs, expected }) => {
const args = makeNixCommandArgs(
inputs.nixOptions,
inputs.flakeInputs,
inputs.commitMessage,
);
expect(args).toStrictEqual(expected);
});
});

20
src/nix.ts Normal file
View file

@ -0,0 +1,20 @@
// Build the Nix args out of inputs from the Actions environment
export function makeNixCommandArgs(
nixOptions: string[],
flakeInputs: string[],
commitMessage: string,
): string[] {
const flakeInputFlags = flakeInputs.flatMap((input) => [
"--update-input",
input,
]);
return nixOptions
.concat(["flake", "lock"])
.concat(flakeInputFlags)
.concat([
"--commit-lock-file",
"--commit-lock-file-summary",
`"${commitMessage}"`,
]);
}