Skip to content

Commit 663636b

Browse files
authored
feat: add support for additional placeholders (#3)
1 parent 7f7dd81 commit 663636b

File tree

9 files changed

+98
-1
lines changed

9 files changed

+98
-1
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ jobs:
112112
with:
113113
dryRun: true
114114
slackWebhook: "invalid-slack-webhook"
115+
additionalPlaceholders: |
116+
non-production=https://google.com
117+
message: ":rocket: A new [[@linkify(it.links.commitSha, 'version') /]] of [[@linkify(it.links.repository, it.repository) /]] has been deployed to [[@linkify(it['non-production'], 'non-production') /]]!"
115118

116119
release:
117120
name: 🚀 Release

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,30 @@ We use [squirrelly](https://squirrelly.js.org/) as the templating engine with `[
4444
- `it.repository`: The shorthand name for the repo (e.g., `singlestone/commit-deploy-slack-action`)
4545
- `it.links.commitSha`: Link to the commit that triggered this action.
4646
- `it.links.repository`: Link to the repository that triggered this action.
47+
48+
### Additional placeholders
49+
50+
You can provide your own placeholders you can use with the message.
51+
52+
```yaml
53+
name: Release
54+
55+
on:
56+
push:
57+
branches:
58+
- main
59+
60+
concurrency: ${{ github.workflow }}-${{ github.ref }}
61+
62+
jobs:
63+
release:
64+
name: Release
65+
runs-on: ubuntu-latest
66+
steps:
67+
- name: Send a Slack notification
68+
with:
69+
additionalPlaceholders: |
70+
non-production=https://google.com
71+
message: ":rocket: A new [[@linkify(it.links.commitSha, 'version') /]] of [[@linkify(it.links.repository, it.repository) /]] has been deployed to [[@linkify(it['non-production'], 'non-production') /]]!"
72+
slackWebhook: ${{ secrets.SLACK_WEBHOOK }}
73+
```

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ inputs:
1717
slackWebhook:
1818
description: "Slack webhook to which notifications will be sent."
1919
required: true
20+
additionalPlaceholders:
21+
description: "Additional placeholders to make available for message use. Should be a multi-line string with keys separated from values by a =."
22+
required: false
2023
runs:
2124
using: "node16"
2225
main: "dist/index.js"

src/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ it("creates a templated Slack message", () => {
2323
JSON.parse(
2424
asSlackMessage(
2525
templateMessage({
26+
additionalPlaceholders: {},
2627
configMessage:
2728
":rocket: A new [[@linkify(it.links.commitSha, 'version') /]] of [[@linkify(it.links.repository, it.repository) /]] has been deployed!",
2829
github: fakeGitHubContext,

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as github from "@actions/github";
33

44
import { asSlackMessage } from "./as-slack-message.js";
55
import { notifySlackWebhook } from "./notify.js";
6+
import { reduceAdditionalPlaceholders } from "./reducers.js";
67
import { register } from "./sqrl/register.js";
78
import { templateMessage } from "./template-message.js";
89

@@ -13,8 +14,15 @@ import { templateMessage } from "./template-message.js";
1314
const webhook = core.getInput("slackWebhook", { required: true });
1415
const configMessage = core.getInput("message");
1516
const linkRoot = core.getInput("linkRoot") || "https://github.com";
17+
const additionalPlaceholdersAsArray = core.getMultilineInput(
18+
"additionalPlaceholders",
19+
);
20+
const additionalPlaceholders = reduceAdditionalPlaceholders(
21+
additionalPlaceholdersAsArray,
22+
);
1623

1724
const templatedMessage = templateMessage({
25+
additionalPlaceholders,
1826
configMessage,
1927
github: github.context,
2028
linkRoot,
@@ -25,7 +33,7 @@ import { templateMessage } from "./template-message.js";
2533
await notifySlackWebhook(webhook, message.buildToJSON());
2634
} else {
2735
console.log("Dry run enabled, printing message instead!");
28-
console.log(message);
36+
console.log(message.buildToJSON());
2937
}
3038
})().catch((e) => {
3139
console.error(e);

src/reducers.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, it } from "vitest";
2+
3+
import { reduceAdditionalPlaceholders } from "./reducers.js";
4+
5+
it("reduces a valid input", () => {
6+
const input = ["example=example", "test=test", "equals=="];
7+
8+
expect(reduceAdditionalPlaceholders(input)).toEqual({
9+
example: "example",
10+
test: "test",
11+
equals: "=",
12+
});
13+
});
14+
15+
it("ignores invalid inputs", () => {
16+
const input = ["example=example", "test=test", "equals==", "=something", ""];
17+
18+
expect(reduceAdditionalPlaceholders(input)).toEqual({
19+
example: "example",
20+
test: "test",
21+
equals: "=",
22+
});
23+
});

src/reducers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const reduceAdditionalPlaceholders = (multilineInput: string[]) =>
2+
multilineInput.reduce<Record<string, string>>((acc, input) => {
3+
const indexOfFirstEquals = input.indexOf("=");
4+
if (indexOfFirstEquals < 0) {
5+
return acc;
6+
}
7+
const key = input.substring(0, indexOfFirstEquals);
8+
const value = input.substring(indexOfFirstEquals + 1);
9+
if (!key) {
10+
return acc;
11+
}
12+
return { ...acc, [key]: value };
13+
}, {});

src/template-message.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ afterEach(() => {
1919
it("templates a message", () => {
2020
expect(
2121
templateMessage({
22+
additionalPlaceholders: {},
2223
configMessage:
2324
":rocket: A new [[@linkify(it.links.commitSha, 'version') /]] of [[@linkify(it.links.repository, it.repository) /]] has been deployed!",
2425
github: fakeGitHubContext,
@@ -28,3 +29,19 @@ it("templates a message", () => {
2829
":rocket: A new <https://github.com/singlestone/commit-deploy-slack-action/commit/test|version> of <https://github.com/singlestone/commit-deploy-slack-action|singlestone/commit-deploy-slack-action> has been deployed!",
2930
);
3031
});
32+
33+
it("supports additional placeholders", () => {
34+
expect(
35+
templateMessage({
36+
additionalPlaceholders: {
37+
"non-production": "https://google.com",
38+
},
39+
configMessage:
40+
":rocket: A new [[@linkify(it.links.commitSha, 'version') /]] of [[@linkify(it.links.repository, it.repository) /]] has been deployed to [[@linkify(it['non-production'], 'non-production') /]]!",
41+
github: fakeGitHubContext,
42+
linkRoot: "https://github.com",
43+
}),
44+
).toEqual(
45+
":rocket: A new <https://github.com/singlestone/commit-deploy-slack-action/commit/test|version> of <https://github.com/singlestone/commit-deploy-slack-action|singlestone/commit-deploy-slack-action> has been deployed to <https://google.com|non-production>!",
46+
);
47+
});

src/template-message.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { context } from "@actions/github";
22
import { render } from "squirrelly";
33

44
interface TemplateMessageParams {
5+
additionalPlaceholders: Record<string, string>;
56
configMessage: string;
67
github: typeof context;
78
linkRoot: string;
@@ -21,6 +22,7 @@ export const templateMessage = (params: TemplateMessageParams): string => {
2122
github: params.github,
2223
links,
2324
repository,
25+
...params.additionalPlaceholders,
2426
},
2527
{
2628
tags: ["[[", "]]"],

0 commit comments

Comments
 (0)