update-flake-lock/src/index.ts

87 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-04-26 16:55:19 +02:00
import * as actionsCore from "@actions/core";
import * as actionsExec from "@actions/exec";
2024-04-22 00:42:23 +02:00
import { ActionOptions, IdsToolbox, inputs } from "detsys-ts";
2024-04-26 16:55:19 +02:00
const EVENT_EXECUTION_FAILURE = "execution_failure";
2024-04-22 00:42:23 +02:00
class UpdateFlakeLockAction {
idslib: IdsToolbox;
private nixOptions: string;
2024-04-22 00:50:32 +02:00
private targets: string[];
2024-04-22 00:42:23 +02:00
private commitMessage: string;
2024-04-26 16:55:19 +02:00
private pathToFlakeDir: string | null;
2024-04-22 00:42:23 +02:00
constructor() {
const options: ActionOptions = {
name: "update-flake-lock",
fetchStyle: "universal",
requireNix: "fail",
};
this.idslib = new IdsToolbox(options);
this.nixOptions = inputs.getString("nix-options");
2024-04-22 00:50:32 +02:00
this.targets = inputs.getString("inputs").split(" ");
2024-04-22 00:42:23 +02:00
this.commitMessage = inputs.getString("commit-msg");
2024-04-26 16:55:19 +02:00
this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
2024-04-22 00:42:23 +02:00
}
2024-04-22 00:50:32 +02:00
async update(): Promise<void> {
2024-04-26 16:55:19 +02:00
const nixOptions: string[] = this.nixOptions.split(",");
const inputFlags: string[] =
this.targets.length > 0
? this.targets.map((input) => `--update-input ${input}`)
: [];
if (this.pathToFlakeDir !== null) {
const returnCode = await actionsExec.exec("cd", [this.pathToFlakeDir]);
if (returnCode !== 0) {
this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {
returnCode,
});
actionsCore.setFailed(
`Error when trying to cd into flake directory ${this.pathToFlakeDir}. Make sure the check that the directory exists.`,
);
}
}
// Nix command of this form:
// nix ${nix options} flake lock ${input flags} --commit-lock-file --commit-lock-file-summary ${commit message}
// Example command:
// nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lock-file-summary
const nixCommandArgs: string[] = nixOptions
.concat(["flake", "lock"])
.concat(inputFlags.length > 0 ? inputFlags : [])
.concat([
"--commit-lock-file",
"--commit-lock-file-summary",
this.commitMessage,
]);
actionsCore.debug(`running nix command:\nnix ${nixCommandArgs.join(" ")}`);
const exitCode = await actionsExec.exec("nix", nixCommandArgs);
2024-04-22 00:50:32 +02:00
2024-04-26 16:55:19 +02:00
if (exitCode !== 0) {
this.idslib.recordEvent(EVENT_EXECUTION_FAILURE, {
exitCode,
});
actionsCore.setFailed(`non-zero exit code of ${exitCode} detected`);
} else {
actionsCore.info(`flake.lock file was successfully updated`);
}
2024-04-22 00:50:32 +02:00
}
2024-04-22 00:42:23 +02:00
}
function main(): void {
const updateFlakeLock = new UpdateFlakeLockAction();
updateFlakeLock.idslib.onMain(async () => {
await updateFlakeLock.update();
});
updateFlakeLock.idslib.execute();
}
2024-04-22 00:17:03 +02:00
main();