Skip to content

Commit

Permalink
fix: bot comment cannot render properly
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Jun 5, 2024
1 parent a8036eb commit 16e0b08
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 56 deletions.
3 changes: 1 addition & 2 deletions src/github/commands/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const COMMENTS_START = '<!--';
const COMMENTS_END = '-->';
import { COMMENTS_END, COMMENTS_START } from '../gfm';

const parsePayload = (body: string) => {
const lines = body.split('\n');
Expand Down
41 changes: 23 additions & 18 deletions src/github/gfm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Link, Text } from 'mdast-util-from-markdown/lib/index';

import { makeMarkdown, parseMarkdown, walk } from './renderer/make-mark';

export const COMMENTS_START = '<!--';
export const COMMENTS_END = '-->';

interface ReplaceOptions {
owner: string;
repo: string;
Expand Down Expand Up @@ -76,29 +79,31 @@ export function replaceGitHubUrlToMarkdown(
}
});

text = makeMarkdown(tree);

return text;
return makeMarkdown(tree);
}

export function replaceGitHubText(text: string) {
if (!text.includes('<img')) {
return text;
// html 语法转为 markdown 语法
if (text.includes('<img')) {
let tmp = text;
let regexResult: RegExpExecArray | null = null;
do {
// https://stackoverflow.com/questions/1028362/how-do-i-extract-html-img-sources-with-a-regular-expression
const imgRegex = /<img\s.*?src=(?:'|\")([^'\">]+)(?:'|\").*?\/?>/gm;
regexResult = imgRegex.exec(tmp);
if (regexResult) {
const newMsg = `![](${regexResult[1]})`;
tmp = tmp.replaceAll(regexResult[0], newMsg);
}
} while (regexResult);
return tmp;
}

let tmp = text;
let regexResult: RegExpExecArray | null = null;
do {
// https://stackoverflow.com/questions/1028362/how-do-i-extract-html-img-sources-with-a-regular-expression
const imgRegex = /<img\s.*?src=(?:'|\")([^'\">]+)(?:'|\").*?\/?>/gm;
regexResult = imgRegex.exec(tmp);
if (regexResult) {
const newMsg = `![](${regexResult[1]})`;
tmp = tmp.replaceAll(regexResult[0], newMsg);
}
} while (regexResult);

return tmp;
// 移除 markdown 注释
if (text.includes(COMMENTS_START)) {
return text.replace(/(<!--[\s\S]*?-->)/g, '');
}
return text;
}

function getUrl(str: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/github/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
PullRequestRefInfo,
} from '@/github/templates/components';
import { StringBuilder } from '@/utils/string-builder';
import { IIssueDetail, IPrDetail } from '@opensumi/octo-service/src/types';
import type { IIssueDetail, IPrDetail } from '@opensumi/octo-service/lib/types';

export { render } from './template-engine';

Expand All @@ -17,7 +17,7 @@ export function renderPrOrIssue(data: IIssueDetail | IPrDetail) {
builder.add(PullRequestRefInfo(data.pr));
}

builder.addDivider(undefined, true);
builder.addDivider();

if (data.type === 'pr') {
builder.add(data.pr.body || '');
Expand Down
7 changes: 3 additions & 4 deletions src/github/templates/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,20 @@ export async function handleCommitComment(
restText = splitted.slice(1).join('\n');
}

const builder = new StringBuilder(`> #### [${title}]({{comment.html_url}})`);
const builder = new StringBuilder(`#### [${title}]({{comment.html_url}})`);

if (restText) {
builder.add(Reference(restText));
}
builder.add(`>`);
builder.add('{{comment.body|ref}}');

return Template(
{
payload,
event: 'commit comment',
action: 'created',
title: `{{sender | link}} {{action}} comment on [${commitRefInfo}]({{comment.html_url}})`,
body: builder.build(),
target: builder.build(),
body: '{{comment.body}}',
},
ctx,
);
Expand Down
2 changes: 1 addition & 1 deletion src/github/templates/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export const Template: TextTpl = (data, ctx) => {
}

if (bodyText) {
textBuilder.addDivider('', true);
textBuilder.addDivider();

if (contentLimit && contentLimit > 0) {
bodyText = limitTextByPosition(bodyText, contentLimit);
Expand Down
12 changes: 6 additions & 6 deletions src/utils/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ export class StringBuilder {
}
}

add(str: string, addExtraLine = false) {
addExtraLine && this.addLineIfNecessary();
add(str: string) {
this.array.push(str);
addExtraLine && this.addLineIfNecessary();
}
addDivider(prefix = '', addExtraLine = false) {
this.add(prefix + '***', addExtraLine);
addDivider() {
this.appendEmptyLine();
this.add('---');
this.appendEmptyLine();
}
/**
* if there are content in the last line, then add a new line
*/
addLineIfNecessary() {
appendEmptyLine() {
const data = this.array[this.array.length - 1];
if (data) {
this.array.push('');
Expand Down
4 changes: 2 additions & 2 deletions test/github/__snapshots__/render.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`render can render issue 1`] = `
{
"text": "#### [#2045 [BUG] 插件 context.globalStoragePath 拿到的和 VSCode 不一样](https://github.com/opensumi/core/issues/2045)
***
---
vscode 拿到的是有插件名称的子目录的:
Expand Down Expand Up @@ -32,7 +32,7 @@ exports[`render can render pr 1`] = `
"text": "#### [#2060 fix: electron cannot work](https://github.com/opensumi/core/pull/2060)
main <- chore/electron-rebuild
***
---
### Types
Expand Down
16 changes: 8 additions & 8 deletions test/github/templates/__snapshots__/comment.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ exports[`github templates comment can handle commit_comment 1`] = `
"compactText": undefined,
"text": "#### [Codertocat](https://github.com/Codertocat) created comment on [commit@611372](https://github.com/Codertocat/Hello-World/commit/6113728f27ae82c7b1a177c8d03f9e96e0adf246#commitcomment-33548674)
***
#### [M: ](https://github.com/Codertocat/Hello-World/commit/6113728f27ae82c7b1a177c8d03f9e96e0adf246#commitcomment-33548674)
> #### [M: ](https://github.com/Codertocat/Hello-World/commit/6113728f27ae82c7b1a177c8d03f9e96e0adf246#commitcomment-33548674)
>
> This is a really good change! :+1:",
---
This is a really good change! :+1:",
"title": "Commit comment created",
}
`;
Expand All @@ -23,7 +23,7 @@ You are totally right! I'll get this fixed right away.",
#### [#1 Spelling error in the README file](https://github.com/Codertocat/Hello-World/issues/1)
***
---
You are totally right! I'll get this fixed right away.",
"title": "Issue comment created",
Expand All @@ -34,7 +34,7 @@ exports[`github templates comment can handle issue comment created 1`] = `
{
"compactText": "[opensumi\\[bot\\]](https://github.com/apps/opensumi) created [comment](https://github.com/opensumi/core/pull/3703):
<!-- versionInfo: PR Next | 3.0.2-next-1716281731.0 -->
🎉 PR Next publish successful!
\`\`\`
Expand All @@ -44,9 +44,9 @@ exports[`github templates comment can handle issue comment created 1`] = `
#### [#3703 feat: support monaco editor worker](https://github.com/opensumi/core/pull/3703)
***
---
<!-- versionInfo: PR Next | 3.0.2-next-1716281731.0 -->
🎉 PR Next publish successful!
\`\`\`
Expand Down
4 changes: 2 additions & 2 deletions test/github/templates/__snapshots__/prOrIssue.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`github templates pr or issue can handle issue 1`] = `
#### [#2960 [BUG] Problem 面板闪烁](https://github.com/opensumi/core/issues/2960)
> Assignees: [bytemain](https://github.com/bytemain)
***
---
打开 settings.json,打开问题面板,操作的时候发现有闪烁现象。
Expand Down Expand Up @@ -76,7 +76,7 @@ exports[`github templates pr or issue can handle pull_request_opened 1`] = `
#### [#2 Update the README with new information.](https://github.com/Codertocat/Hello-World/pull/2)
> master <- changes
***
---
This is a pretty simple change that we need to pull into master.",
"title": "Pull request#2 opened",
Expand Down
12 changes: 4 additions & 8 deletions test/github/templates/__snapshots__/release.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ exports[`release related can handle release event 1`] = `
"compactText": undefined,
"text": "#### [opensumi](https://github.com/opensumi) published [v0.0.0](https://github.com/opensumi/core/releases/tag/v0.0.0)
***
---
Tag: v0.0.0
> <!-- Release notes generated using configuration in .github/release.yml at v2.21.2 -->
>
> ## What's Changed
>
> * fix(snippets): register code snippets timing by @Aaaaash in [#1920](https://github.com/opensumi/core/pull/1920)
Expand All @@ -34,7 +32,7 @@ exports[`release related transform url correctly 1`] = `
"compactText": undefined,
"text": "#### [github](https://github.com/github) published [1.1.0](https://github.com/ant-design/ant-design-mini/releases/tag/1.1.0)
***
---
Tag: 1.1.0
Expand All @@ -44,7 +42,7 @@ Tag: 1.1.0
>
> * feat(Container): 无titleheaderRight时隐藏header [\`#560\`](https://github.com/ant-design/ant-design-mini/pull/560)
> * chore(formItem): 删除formItem无用代码 [\`#562\`](https://github.com/ant-design/ant-design-mini/pull/562)
> * docs(collapse): 文档透出CollapseItem的disabled字段 [\`#561\`](https://github.com/ant-design/ant-design-mini/pull/561)<!-- Release notes generated using configuration in .github/release.yml at v2.21.2 -->
> * docs(collapse): 文档透出CollapseItem的disabled字段 [\`#561\`](https://github.com/ant-design/ant-design-mini/pull/561)
>
> ## What's Changed
>
Expand All @@ -69,12 +67,10 @@ exports[`release related will transform long url to short link 1`] = `
"compactText": undefined,
"text": "#### [opensumi](https://github.com/opensumi) published [v0.0.0](https://github.com/opensumi/core/releases/tag/v0.0.0)
***
---
Tag: v0.0.0
> <!-- Release notes generated using configuration in .github/release.yml at v2.21.2 -->
>
> ## What's Changed
>
> * fix(snippets): register code snippets timing by @Aaaaash in [#1920](https://github.com/opensumi/core/pull/1920)
Expand Down
2 changes: 1 addition & 1 deletion test/github/templates/__snapshots__/review.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Maybe you should use more emoji on this line.",
#### [#2 Update the README with new information.](https://github.com/Codertocat/Hello-World/pull/2)
***
---
Maybe you should use more emoji on this line.",
"title": "Review comment created",
Expand Down
4 changes: 2 additions & 2 deletions test/queue/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`queue can composite discussion 1`] = `
#### [#90 Welcome to discussions!](https://github.com/octo-org/octo-repo/discussions/90)
***
---
We're glad to have you here!
Expand Down Expand Up @@ -62,7 +62,7 @@ Maybe you should use more emojji on this line.",
#### [#2 Update the README with new information.](https://github.com/Codertocat/Hello-World/pull/2)
***
---
Maybe you should use more emojji on this line.",
"title": "[Hello-World] Review comment created",
Expand Down

0 comments on commit 16e0b08

Please sign in to comment.