Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strip ansi control chars from PR comments when called with {"color": "always"} #859

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
([#803](https://github.com/pulumi/actions/issues/803))

- bug: Fix installation on Windows.
([#851](https://github.com/pulumi/actions/pull/851))
([#851](https://github.com/pulumi/actions/pull/851))

- fix: strip ansi control characters from pull request comment
([#859](https://github.com/pulumi/actions/pull/859))

## 4.0.0 (2023-19-01)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@actions/io": "^1.1.2",
"@actions/tool-cache": "^2.0.1",
"@pulumi/pulumi": "3.54.0",
"ansi-to-html": "^0.7.2",
"dedent": "^0.7.0",
"envalid": "^7.3.1",
"got": "^11.8.6",
Expand Down
10 changes: 5 additions & 5 deletions src/libs/__tests__/pr.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as gh from '@actions/github';
import gh from '@actions/github';
import { Config } from '../../config';
import { handlePullRequestMessage } from '../pr';

Expand Down Expand Up @@ -50,7 +50,7 @@ describe('pr.ts', () => {

await handlePullRequestMessage(defaultOptions, projectName, 'test');
expect(createComment).toHaveBeenCalledWith({
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n```\ntest\n```\n\n</details>',
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n<pre>\ntest\n</pre>\n\n</details>',
issue_number: 123,
});
});
Expand All @@ -74,7 +74,7 @@ describe('pr.ts', () => {

await handlePullRequestMessage(options, projectName, 'test');
expect(createComment).toHaveBeenCalledWith({
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n```\ntest\n```\n\n</details>',
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n<pre>\ntest\n</pre>\n\n</details>',
issue_number: 87,
});
});
Expand All @@ -101,7 +101,7 @@ describe('pr.ts', () => {

await handlePullRequestMessage(options, projectName, 'test');
expect(createComment).toHaveBeenCalledWith({
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n```\ntest\n```\n\n</details>',
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n<pre>\ntest\n</pre>\n\n</details>',
issue_number: 87,
});
});
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('pr.ts', () => {
await handlePullRequestMessage(options, projectName, 'test');
expect(updateComment).toHaveBeenCalledWith({
comment_id: 2,
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n```\ntest\n```\n\n</details>',
body: '#### :tropical_drink: `preview` on myFirstProject/staging\n\n<details>\n<summary>Pulumi report</summary>\n\n<pre>\ntest\n</pre>\n\n</details>',
});
});
});
27 changes: 21 additions & 6 deletions src/libs/pr.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import * as core from '@actions/core';
import { context, getOctokit } from '@actions/github';
import * as dedent from 'dedent';
import AnsiToHtml from 'ansi-to-html';
import dedent from 'dedent';
import invariant from 'ts-invariant';
import { Config } from '../config';

function ansiToHtml(
message: string,
{ maxLength }: { maxLength?: number } = { maxLength: undefined },
) {
const convert = new AnsiToHtml();
let html = convert.toHtml(message);
while (maxLength !== undefined && html.length > maxLength) {
message = message.substring(0, message.length - 1);
html = convert.toHtml(message);
}
Comment on lines +14 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain in a comment what this code is doing? It looks like it's truncating the message body according to the max length of the message.

However, it looks like it's only cutting out a single character at a time and attempting to convert to HTML again. I imagine that would be very slow if html.length >>>> maxLength.

return html;
}

export async function handlePullRequestMessage(
config: Config,
projectName: string,
Expand All @@ -20,7 +34,7 @@ export async function handlePullRequestMessage(

const summary = '<summary>Pulumi report</summary>';

const rawBody = output.substring(0, 64_000);
const rawBody = ansiToHtml(output, { maxLength: 64_000 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this PR touches this code, would you mind pulling this value into a constant so it's not a "magic number"? I'd appreciate it :D

// a line break between heading and rawBody is needed
// otherwise the backticks won't work as intended
const body = dedent`
Expand All @@ -29,9 +43,9 @@ export async function handlePullRequestMessage(
<details>
${summary}

\`\`\`
<pre>
${rawBody}
\`\`\`
</pre>
${
rawBody.length === 64_000
? '**Warn**: The output was too long and trimmed.'
Expand All @@ -53,8 +67,9 @@ export async function handlePullRequestMessage(
...repo,
issue_number: nr,
});
const comment = comments.find((comment) =>
comment.body.startsWith(heading) && comment.body.includes(summary),
const comment = comments.find(
(comment) =>
comment.body.startsWith(heading) && comment.body.includes(summary),
Comment on lines +70 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like its a purely aesthetic change, right? NBD, just checking.

);

// If comment exists, update it.
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "es2015",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"allowJs": false,
"importHelpers": true,
"alwaysStrict": true,
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,13 @@ ansi-styles@^6.0.0:
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.1.0.tgz#87313c102b8118abd57371afab34618bf7350ed3"
integrity sha512-VbqNsoz55SYGczauuup0MFUyXNQviSpFTj1RQtFzmQLk18qbVSpTFFGMT293rmDaQuKCT6InmbuEyUne4mTuxQ==

ansi-to-html@^0.7.2:
version "0.7.2"
resolved "https://registry.yarnpkg.com/ansi-to-html/-/ansi-to-html-0.7.2.tgz#a92c149e4184b571eb29a0135ca001a8e2d710cb"
integrity sha512-v6MqmEpNlxF+POuyhKkidusCHWWkaLcGRURzivcU3I9tv7k4JVhFcnukrM5Rlk2rUywdZuzYAZ+kbZqWCnfN3g==
dependencies:
entities "^2.2.0"

anymatch@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb"
Expand Down Expand Up @@ -2048,6 +2055,11 @@ enquirer@^2.3.5:
dependencies:
ansi-colors "^4.1.1"

entities@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==

envalid@^7.3.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/envalid/-/envalid-7.3.1.tgz#5bf6bbb4effab2d64a1991d8078b4ae38924f0d2"
Expand Down