Skip to content

Commit

Permalink
Read input template-variables
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Jul 18, 2021
1 parent 0077788 commit 1e8d6b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ inputs:
description: 'The path to the action configuration file'
default: '.github/pr-commenter.yml'
required: false
template-variables:
description: 'A string with a JSON that holds variables that will be used when rendering the Mustache templates'
required: false
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
17 changes: 16 additions & 1 deletion lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
async function run() {
const token = core.getInput('github-token', { required: true });
const configPath = core.getInput('config-file', { required: true });
const templateVariablesJSONString = core.getInput('template-variables', { required: false });

const prNumber = getPrNumber();
if (!prNumber) {
Expand All @@ -42,12 +43,26 @@ async function run() {
const snippetIds = getMatchingSnippetIds(changedFiles, commentConfig);
const previousComment = await getPreviousPRComment(client, prNumber);

let templateVariables = {};
if (templateVariablesJSONString) {
core.debug('Input template-variables was passed');
core.debug(templateVariablesJSONString);

try {
templateVariables = JSON.parse(templateVariablesJSONString);
} catch (error) {
core.warning('Failed to parse template-variables input as JSON. Continuing without template variables.');
}
} else {
core.debug('Input template-variables was not passed');
}

if (shouldDeletePreviousComment(previousComment, snippetIds, commentConfig)) {
core.info('removing previous comment');
await deleteComment(client, previousComment);
}

const commentBody = assembleCommentBody(snippetIds, commentConfig);
const commentBody = assembleCommentBody(snippetIds, commentConfig, templateVariables);

if (shouldEditPreviousComment(previousComment, snippetIds, commentConfig)) {
core.info('updating previous comment');
Expand Down
8 changes: 6 additions & 2 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('run', () => {
const fakeConfigPath = 'foo/config-file.yml';
const fakeConfig = 'comment:\n'
+ ' on-update: recreate\n'
+ ' header: Hello!\n'
+ ' header: Hello {{name}}!\n'
+ ' footer: Bye!\n'
+ ' snippets:\n'
+ ' - id: snippet1\n'
Expand All @@ -49,6 +49,10 @@ describe('run', () => {
return 'foo/config-file.yml';
}

if (argument === 'template-variables') {
return '{"name": "Bob"}';
}

return null;
});

Expand All @@ -66,7 +70,7 @@ describe('run', () => {
{ user: { type: 'Bot' }, created_at: '2020-01-01' },
];

const commentBody = `${'Hello!\n\n'
const commentBody = `${'Hello Bob!\n\n'
+ 'This is snippet 1\n\n'
+ 'This is snippet 3\n\n'
+ 'Bye!\n\n'}${
Expand Down

0 comments on commit 1e8d6b5

Please sign in to comment.