Skip to content

Commit

Permalink
add logic to escape pull request title when rendering JSON templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrivest committed Oct 9, 2024
1 parent d40317d commit 288ef90
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions prdeploy-webhooks/src/services/template-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Handlebars.registerHelper('jsonEncode', (text: string) => {
return JSON.stringify(text);
});

Handlebars.registerHelper('jsonEncodeUnquoted', (text: string) => {
return JSON.stringify(text).slice(1, -1);
});

Handlebars.registerHelper('color', hexValue => (hexValue ? hexValue.replace(/^#/, '') : ''));

export type TemplateNames =
Expand Down
2 changes: 1 addition & 1 deletion prdeploy-webhooks/templates/deploy-completed.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<{{pull.html_url}}|#{{pull.number}}> {{{pull.title}}}"
"text": "<{{pull.html_url}}|#{{pull.number}}> {{{jsonEncodeUnquoted pull.title}}}"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion prdeploy-webhooks/templates/deploy-released.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<{{pull.html_url}}|#{{pull.number}}> {{{pull.title}}}"
"text": "<{{pull.html_url}}|#{{pull.number}}> {{{jsonEncodeUnquoted pull.title}}}"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<{{pull.html_url}}|#{{pull.number}}> {{{pull.title}}}"
"text": "<{{pull.html_url}}|#{{pull.number}}> {{{jsonEncodeUnquoted pull.title}}}"
}
}
]
Expand Down
8 changes: 7 additions & 1 deletion prdeploy-webhooks/test/services/template-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('renderQueueTable', () => {
expect(result).not.toBeFalsy();
expect(result).toMatch(`| Position | 1 | 2 | 3 |
|----------|---------|---------|---------|
| [dev queue](https://awssite/deployments/greggbjensen/prdeploy-example-repo?environment=dev) | [2987](https://github.com/greggbjensen/prdeploy-example-repo/pull/2987) | [2549](https://github.com/greggbjensen/prdeploy-example-repo/pull/2549) | [2391](https://github.com/greggbjensen/prdeploy-example-repo/pull/2391) |`);
| [dev queue](https://awssite/deployments/greggbjensen/prdeploy-example-repo/deployments?environment=dev) | [2987](https://github.com/greggbjensen/prdeploy-example-repo/pull/2987) | [2549](https://github.com/greggbjensen/prdeploy-example-repo/pull/2549) | [2391](https://github.com/greggbjensen/prdeploy-example-repo/pull/2391) |`);
});
});

Expand Down Expand Up @@ -71,13 +71,19 @@ Pull request with fix and feature.

it('renders JSON template with escaping', async () => {
const service = container.resolve(TemplateService);
const pull = {
html_url: 'https://github.com/greggbjensen/prdeploy-example-repo/pull/2987',
number: 2987,
title: 'This pull request title has some "special" characters'
};
const slackPullBody = `
This pull request body includes multiple
new lines and " these things...
Oh and another one"
`;

const result = await service.render('deploy-released.json', {
pull,
slackPullBody
});

Expand Down

0 comments on commit 288ef90

Please sign in to comment.