Skip to content

Commit ab3f13a

Browse files
committed
fix: parse provided payloads before replacing templated variables
1 parent f234e85 commit ab3f13a

2 files changed

Lines changed: 120 additions & 20 deletions

File tree

src/content.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export default class Content {
3737
this.values = github.context;
3838
break;
3939
}
40+
if (config.inputs.payloadTemplated) {
41+
this.values = this.templatize(this.values);
42+
}
4043
if (config.inputs.payloadDelimiter) {
4144
this.values = flatten(this.values, {
4245
delimiter: config.inputs.payloadDelimiter,
@@ -63,9 +66,8 @@ export default class Content {
6366
);
6467
}
6568
try {
66-
const input = this.templatize(config, config.inputs.payload);
6769
const content = /** @type {Content} */ (
68-
yaml.load(input, {
70+
yaml.load(config.inputs.payload, {
6971
schema: yaml.JSON_SCHEMA,
7072
})
7173
);
@@ -119,18 +121,17 @@ export default class Content {
119121
path.resolve(config.inputs.payloadFilePath),
120122
"utf-8",
121123
);
122-
const content = this.templatize(config, input);
123124
if (
124125
config.inputs.payloadFilePath.endsWith("yaml") ||
125126
config.inputs.payloadFilePath.endsWith("yml")
126127
) {
127-
const load = yaml.load(content, {
128+
const load = yaml.load(input, {
128129
schema: yaml.JSON_SCHEMA,
129130
});
130131
return /** @type {Content} */ (load);
131132
}
132133
if (config.inputs.payloadFilePath.endsWith("json")) {
133-
return JSON.parse(content);
134+
return JSON.parse(input);
134135
}
135136
throw new SlackError(
136137
config.core,
@@ -148,20 +149,32 @@ export default class Content {
148149
}
149150

150151
/**
151-
* Replace templated variables in the provided content if requested.
152-
* @param {Config} config
153-
* @param {string} input - The initial value of the content.
154-
* @returns {string} Content with templatized variables replaced.
152+
* Replace templated variables in the provided content as requested.
153+
* @param {unknown} input - The initial value of the content.
154+
* @returns {unknown} Content with templatized variables replaced.
155155
*/
156-
templatize(config, input) {
157-
if (!config.inputs.payloadTemplated) {
158-
return input;
156+
templatize(input) {
157+
if (Array.isArray(input)) {
158+
return input.map((v) => this.templatize(v));
159+
}
160+
if (input && typeof input === "object") {
161+
/**
162+
* @type {any}
163+
*/
164+
const out = {};
165+
for (const [k, v] of Object.entries(input)) {
166+
out[k] = this.templatize(v);
167+
}
168+
return out;
169+
}
170+
if (typeof input === "string") {
171+
const template = input.replace(/\$\{\{/g, "{{"); // swap ${{ for {{
172+
const context = {
173+
env: process.env,
174+
github: github.context,
175+
};
176+
return markup.up(template, context);
159177
}
160-
const template = input.replace(/\$\{\{/g, "{{"); // swap ${{ for {{
161-
const context = {
162-
env: process.env,
163-
github: github.context,
164-
};
165-
return markup.up(template, context);
178+
return input;
166179
}
167180
}

test/content.spec.js

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,53 @@ describe("content", () => {
8787
it("templatizes variables with matching variables", async () => {
8888
mocks.core.getInput
8989
.withArgs("payload")
90-
.returns("message: Served ${{ env.NUMBER }} from ${{ github.apiUrl }}");
90+
.returns(`
91+
channel: C0123456789
92+
reply_broadcast: false
93+
message: Served \${{ env.NUMBER }} from \${{ github.apiUrl }}
94+
blocks:
95+
- type: section
96+
text:
97+
type: mrkdwn
98+
text: "Served \${{ env.NUMBER }} on: \${{ env.DETAILS }}"
99+
- type: divider
100+
- type: section
101+
text:
102+
type: mrkdwn
103+
text: "> From \${{ github.apiUrl }}"
104+
`);
91105
mocks.core.getBooleanInput.withArgs("payload-templated").returns(true);
106+
process.env.DETAILS = `
107+
-fri
108+
-sat
109+
-sun`
92110
process.env.NUMBER = 12;
93111
const config = new Config(mocks.core);
112+
process.env.DETAILS = undefined;
94113
process.env.NUMBER = undefined;
95114
const expected = {
115+
channel: "C0123456789",
116+
reply_broadcast: false,
96117
message: "Served 12 from https://api.github.com",
118+
blocks: [
119+
{
120+
type: "section",
121+
text: {
122+
type: "mrkdwn",
123+
text: "Served 12 on: \n-fri\n-sat\n-sun"
124+
}
125+
},
126+
{
127+
type: "divider"
128+
},
129+
{
130+
type: "section",
131+
text: {
132+
type: "mrkdwn",
133+
text: "> From https://api.github.com"
134+
}
135+
}
136+
]
97137
};
98138
assert.deepEqual(config.content.values, expected);
99139
});
@@ -257,14 +297,61 @@ describe("content", () => {
257297
mocks.fs.readFileSync
258298
.withArgs(path.resolve("example.json"), "utf-8")
259299
.returns(`{
260-
"message": "Served $\{\{ env.NUMBER }} from $\{\{ github.apiUrl }}"
300+
"channel": "C0123456789",
301+
"reply_broadcast": false,
302+
"message": "Served \${{ env.NUMBER }} from \${{ github.apiUrl }}",
303+
"blocks": [
304+
{
305+
"type": "section",
306+
"text": {
307+
"type": "mrkdwn",
308+
"text": "Served \${{ env.NUMBER }} on: \${{ env.DETAILS }}"
309+
}
310+
},
311+
{
312+
"type": "divider"
313+
},
314+
{
315+
"type": "section",
316+
"text": {
317+
"type": "mrkdwn",
318+
"text": "> From \${{ github.apiUrl }}"
319+
}
320+
}
321+
]
261322
}`);
262323
mocks.core.getBooleanInput.withArgs("payload-templated").returns(true);
324+
process.env.DETAILS = `
325+
-fri
326+
-sat
327+
-sun`
263328
process.env.NUMBER = 12;
264329
const config = new Config(mocks.core);
330+
process.env.DETAILS = undefined;
265331
process.env.NUMBER = undefined;
266332
const expected = {
333+
channel: "C0123456789",
334+
reply_broadcast: false,
267335
message: "Served 12 from https://api.github.com",
336+
blocks: [
337+
{
338+
type: "section",
339+
text: {
340+
type: "mrkdwn",
341+
text: "Served 12 on: \n-fri\n-sat\n-sun"
342+
}
343+
},
344+
{
345+
type: "divider"
346+
},
347+
{
348+
type: "section",
349+
text: {
350+
type: "mrkdwn",
351+
text: "> From https://api.github.com"
352+
}
353+
}
354+
]
268355
};
269356
assert.deepEqual(config.content.values, expected);
270357
});

0 commit comments

Comments
 (0)