Enable supplying a commit message template

This commit is contained in:
Luc Perkins
2024-06-04 08:28:12 -07:00
parent d3aa136776
commit 09b0ac8cd3
6 changed files with 99 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import { makeNixCommandArgs } from "./nix.js";
import { renderCommitMessage } from "./template.js";
import * as actionsCore from "@actions/core";
import * as actionsExec from "@actions/exec";
import { DetSysAction, inputs } from "detsys-ts";
@ -12,6 +13,7 @@ const EVENT_EXECUTION_FAILURE = "execution_failure";
class UpdateFlakeLockAction extends DetSysAction {
private commitMessage: string;
private commitMessageTemplate: string;
private nixOptions: string[];
private flakeInputs: string[];
private pathToFlakeDir: string | null;
@ -26,6 +28,7 @@ class UpdateFlakeLockAction extends DetSysAction {
});
this.commitMessage = inputs.getString("commit-msg");
this.commitMessageTemplate = inputs.getString("commit-msg-template");
this.flakeInputs = inputs.getArrayOfStrings("inputs", "space");
this.nixOptions = inputs.getArrayOfStrings("nix-options", "space");
this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
@ -57,6 +60,12 @@ class UpdateFlakeLockAction extends DetSysAction {
actionsCore.debug(`Running flake lock update in directory \`${flakeDir}\``);
const flakeDotLock = `${flakeDir}/flake.lock`;
const commitMessage =
this.commitMessage !== ""
? this.commitMessage
: renderCommitMessage(this.commitMessageTemplate, flakeDotLock);
// Nix command of this form:
// nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}
// Example commands:
@ -65,7 +74,7 @@ class UpdateFlakeLockAction extends DetSysAction {
const nixCommandArgs: string[] = makeNixCommandArgs(
this.nixOptions,
this.flakeInputs,
this.commitMessage,
commitMessage,
);
actionsCore.debug(

31
src/template.test.ts Normal file
View File

@ -0,0 +1,31 @@
import { renderCommitMessage } from "./template.js";
import { describe, expect, test } from "vitest";
describe("templating", () => {
test("commit message", () => {
type TestCase = {
template: string;
flakeDotLock: string;
expected: string;
};
const testCases: TestCase[] = [
{
template: "Updating lockfile at {{ flake_dot_lock }}",
flakeDotLock: "./flake.lock",
expected: "Updating lockfile at ./flake.lock",
},
{
template:
"Here I go doing some updating of my pristine flake.lock at {{ flake_dot_lock }}",
flakeDotLock: "subflake/flake.lock",
expected:
"Here I go doing some updating of my pristine flake.lock at subflake/flake.lock",
},
];
testCases.forEach(({ template, flakeDotLock, expected }) => {
expect(renderCommitMessage(template, flakeDotLock)).toEqual(expected);
});
});
});

13
src/template.ts Normal file
View File

@ -0,0 +1,13 @@
import Handlebars from "handlebars";
export function renderCommitMessage(
template: string,
flakeDotLock: string,
): string {
return render(template, { flake_dot_lock: flakeDotLock });
}
function render(template: string, inputs: Record<string, string>): string {
const tpl = Handlebars.compile(template);
return tpl(inputs);
}