-
Notifications
You must be signed in to change notification settings - Fork 73
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
return html; | ||
} | ||
|
||
export async function handlePullRequestMessage( | ||
config: Config, | ||
projectName: string, | ||
|
@@ -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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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` | ||
|
@@ -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.' | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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.