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

Conversation

bennettp123
Copy link
Contributor

@bennettp123 bennettp123 commented Feb 4, 2023

When pulumi/actions is used with {comment-on-pr: true, color: "always"}, it creates a PR comment with extra ANSI control characters.

Let's strip them before adding them to the PR comment.

Uses ansi-to-html to convert the output into HTML. The result is then wrapped in <pre></pre> tags before adding it to the PR comment.

I have no idea if this is the best way to do it, but I thought I'd try it and see what people think. 😀

Before:

image

After:

image

When called with `color: always`, the PR comment includes ANSI control
characters.

Let's strip them using ansi-to-html
@bennettp123 bennettp123 changed the title strip ansi control chars from PR comments strip ansi control chars from PR comments when called with {"color": "always"} Feb 14, 2023
Copy link
Contributor

@RobbieMcKinstry RobbieMcKinstry left a comment

Choose a reason for hiding this comment

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

On the whole this change LGTM. I do wonder if it makes sense to add pre tags in all cases. My two concerns are if this will affect users who have color disabled, and the performance of substringing when over max length (see inline comment).

Does GitHub Flavored Markdown always convert code blocks (```) to <pre> tags when it renders in the browser? i.e. is change equivalent when color: never?

Comment on lines +14 to +17
while (maxLength !== undefined && html.length > maxLength) {
message = message.substring(0, message.length - 1);
html = convert.toHtml(message);
}
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.

@@ -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

Comment on lines +70 to +72
const comment = comments.find(
(comment) =>
comment.body.startsWith(heading) && comment.body.includes(summary),
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants