Initial version of PR body rendering

This commit is contained in:
Luc Perkins 2024-06-04 09:19:35 -07:00
parent 8c5e8043f8
commit 0829421b88
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
6 changed files with 8458 additions and 16 deletions

View file

@ -16,7 +16,7 @@ inputs:
* `{{ flake_dot_lock }}` is the path to the `flake.lock` file being updated * `{{ flake_dot_lock }}` is the path to the `flake.lock` file being updated
* `{{ flake_dot_lock_dir }}` is the `flake.lock` file's directory * `{{ flake_dot_lock_dir }}` is the `flake.lock` file's directory
If you set both this and `commit-msg`, the `commit-msg` setting is used. If you set both this and `commit-msg`, the `commit-msg` setting is used (it does not support templating).
required: false required: false
default: | default: |
flake.lock: Updated in {{ flake_dot_lock_dir }} flake.lock: Updated in {{ flake_dot_lock_dir }}
@ -48,7 +48,13 @@ inputs:
default: "flake.lock: Update" default: "flake.lock: Update"
pr-body-template: pr-body-template:
description: | description: |
TODO The pull request body template to use. You can use these variables in your template:
* `{{ comma_separated_dirs }}` is the flake directories that were updated separated by comma
* `{{ space_separated_dirs }}` is the flake directories that were updated separated by space
* `{{ updated_dirs_list }}` is the flake directories that were updated as a Markdown list
If you set both this and `pr-body`, the `pr-body` setting is used (it does not support templating).
required: false required: false
default: | default: |
Just testing. Just testing.

8405
dist/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,5 @@
import { makeNixCommandArgs } from "./nix.js"; import { makeNixCommandArgs } from "./nix.js";
import { renderCommitMessage } from "./template.js"; import { renderCommitMessage, renderPullRequestBody } from "./template.js";
import * as actionsCore from "@actions/core"; import * as actionsCore from "@actions/core";
import * as actionsExec from "@actions/exec"; import * as actionsExec from "@actions/exec";
import { DetSysAction, inputs } from "detsys-ts"; import { DetSysAction, inputs } from "detsys-ts";
@ -14,6 +14,8 @@ const EVENT_EXECUTION_FAILURE = "execution_failure";
class UpdateFlakeLockAction extends DetSysAction { class UpdateFlakeLockAction extends DetSysAction {
private commitMessage: string; private commitMessage: string;
private commitMessageTemplate: string; private commitMessageTemplate: string;
private prBody: string;
private prBodyTemplate: string;
private nixOptions: string[]; private nixOptions: string[];
private flakeInputs: string[]; private flakeInputs: string[];
private pathToFlakeDir: string | null; private pathToFlakeDir: string | null;
@ -29,6 +31,8 @@ class UpdateFlakeLockAction extends DetSysAction {
this.commitMessage = inputs.getString("commit-msg"); this.commitMessage = inputs.getString("commit-msg");
this.commitMessageTemplate = inputs.getString("commit-msg-template"); this.commitMessageTemplate = inputs.getString("commit-msg-template");
this.prBody = inputs.getString("pr-body");
this.prBodyTemplate = inputs.getString("pr-body-template");
this.flakeInputs = inputs.getArrayOfStrings("inputs", "space"); this.flakeInputs = inputs.getArrayOfStrings("inputs", "space");
this.nixOptions = inputs.getArrayOfStrings("nix-options", "space"); this.nixOptions = inputs.getArrayOfStrings("nix-options", "space");
this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir"); this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
@ -48,7 +52,12 @@ class UpdateFlakeLockAction extends DetSysAction {
await this.updateFlakeInDirectory(directory); await this.updateFlakeInDirectory(directory);
} }
actionsCore.setOutput(PR_BODY_OUTPUT_KEY, "THIS IS JUST A TEST"); const prBody =
this.prBody !== ""
? this.prBody
: renderPullRequestBody(this.prBodyTemplate, this.flakeDirs);
actionsCore.setOutput(PR_BODY_OUTPUT_KEY, prBody);
} }
// No post phase // No post phase

View file

@ -46,13 +46,30 @@ describe("templating", () => {
test("pull request body", () => { test("pull request body", () => {
type TestCase = { type TestCase = {
template: string; template: string;
dirs: string[];
expected: string; expected: string;
}; };
const testCases: TestCase[] = []; const testCases: TestCase[] = [
{
template: "Updated inputs: {{ comma_separated_dirs }}",
dirs: ["."],
expected: "Updated inputs: .",
},
{
template: "Updated inputs: {{ space_separated_dirs }}",
dirs: ["subflake", "subflake2"],
expected: "Updated inputs: subflake subflake2",
},
{
template: "Updated inputs:\n{{ updated_dirs_list }}",
dirs: ["flake1", "flake2"],
expected: `Updated inputs:\n* flake1\n* flake2`,
},
];
testCases.forEach(({ template, expected }) => { testCases.forEach(({ template, dirs, expected }) => {
expect(renderPullRequestBody(template)).toEqual(expected); expect(renderPullRequestBody(template, dirs)).toEqual(expected);
}); });
}); });
}); });

View file

@ -1,8 +1,23 @@
import Handlebars from "handlebars"; import Handlebars from "handlebars";
export function renderPullRequestBody(template: string): string { export function renderPullRequestBody(
template: string,
dirs: string[],
): string {
const commaSeparated = dirs.join(", ");
const spaceSeparated = dirs.join(" ");
const dirsList = dirs.map((d: string) => `* ${d}`).join("\n");
const tpl = Handlebars.compile(template); const tpl = Handlebars.compile(template);
return tpl({});
return tpl({
// eslint-disable-next-line camelcase
comma_separated_dirs: commaSeparated,
// eslint-disable-next-line camelcase
space_separated_dirs: spaceSeparated,
// eslint-disable-next-line camelcase
updated_dirs_list: dirsList,
});
} }
export function renderCommitMessage( export function renderCommitMessage(
@ -11,7 +26,9 @@ export function renderCommitMessage(
flakeDotLock: string, flakeDotLock: string,
): string { ): string {
return render(template, { return render(template, {
// eslint-disable-next-line camelcase
flake_dot_lock_dir: flakeDotLockDir, flake_dot_lock_dir: flakeDotLockDir,
// eslint-disable-next-line camelcase
flake_dot_lock: flakeDotLock, flake_dot_lock: flakeDotLock,
}); });
} }