Skip to content

Commit

Permalink
feat: optimize renderer effect
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Feb 28, 2024
1 parent 079863c commit d7f56d2
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/github/templates/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
StopHandleError,
limitLine,
renderPrOrIssueLink,
renderPrOrIssueTitleLink,
textTpl,
useRef,
} from './utils';
Expand Down Expand Up @@ -86,7 +87,7 @@ function renderComment(
payload,
action,
event: `${location} comment`,
target: renderPrOrIssueLink(data),
target: renderPrOrIssueTitleLink(data),
title: `{{sender | link:sender}} ${action} [comment](${comment.html_url}) on [${location}](${data.html_url})`,
body: renderCommentBody(payload.comment),
notRenderBody,
Expand Down
18 changes: 6 additions & 12 deletions src/github/templates/prOrIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
renderRequestedReviewersInfo,
textTpl,
prettyUnderlineWord,
useRef,
} from './utils';

export type Name = 'issues' | 'pull_request' | 'discussion';
Expand Down Expand Up @@ -71,27 +70,22 @@ function render(
}
}

if (shouldRenderBody && data.body) {
builder.addDivider('', true);
builder.add(renderPrOrIssueBody(data));
}

let textFirstLine = `{{sender | link:sender}} ${action} [${nameBlock}](${data.html_url})`;
let title = `{{sender | link:sender}} ${action} [${nameBlock}](${data.html_url})`;

if ((data as Issue).state_reason) {
textFirstLine += ` as ${prettyUnderlineWord(
(data as Issue).state_reason!,
)}`;
title += ` as ${prettyUnderlineWord((data as Issue).state_reason!)}`;
}

const text = textTpl(
{
payload,
event: `${nameBlock}#${data.number}`,
action,
title: textFirstLine,
body: builder.build(),
title,
target: builder.build(),
body: renderPrOrIssueBody(data),
contentLimit,
notRenderBody: !shouldRenderBody,
},
ctx,
);
Expand Down
8 changes: 6 additions & 2 deletions src/github/templates/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export async function handleReview(
did = review.state;
}

let doNotRenderBody = false;

if (review.state === 'changes_requested') {
titleActionText = 'requested changes';
did = 'requested';
Expand All @@ -33,6 +35,7 @@ export async function handleReview(
if (action === 'dismissed') {
did = 'dismissed';
something = 'review';
doNotRenderBody = true;
}

let textFirstLine = `{{sender|link}} [${did}]({{review.html_url}}) `;
Expand All @@ -45,10 +48,11 @@ export async function handleReview(
{
payload,
event: 'review',
target: '{{pull_request|link}}',
target: '#### {{pull_request|link}}',
action: titleActionText,
title: textFirstLine,
body: '{{review.body|ref}}',
body: '{{review.body}}',
notRenderBody: doNotRenderBody,
},
ctx,
);
Expand Down
2 changes: 1 addition & 1 deletion src/github/templates/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export const textTpl: TextTpl = (data, ctx, options) => {
}

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

if (contentLimit && contentLimit > 0) {
bodyText = limitTextByPosition(bodyText, contentLimit);
Expand Down
2 changes: 2 additions & 0 deletions test/github/templates/__snapshots__/comment.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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)
>
> This is a really good change! :+1:",
Expand Down
3 changes: 2 additions & 1 deletion test/github/templates/__snapshots__/prOrIssue.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exports[`github templates pr or issue can handle issue 1`] = `
{
"compactText": undefined,
"text": "#### [bytemain](https://github.com/bytemain) opened [issue](https://github.com/opensumi/core/issues/2960)
***
#### [#2960 [BUG] Problem 面板闪烁](https://github.com/opensumi/core/issues/2960)
Assignees: [bytemain](https://github.com/bytemain)
Expand Down Expand Up @@ -70,7 +69,9 @@ exports[`github templates pr or issue can handle pull_request_opened 1`] = `
"text": "#### [Codertocat](https://github.com/Codertocat) opened [pull request](https://github.com/Codertocat/Hello-World/pull/2)
#### [#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
6 changes: 6 additions & 0 deletions test/github/templates/__snapshots__/release.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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 -->
Expand All @@ -31,7 +33,9 @@ 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
> #### [1.1.0](https://github.com/ant-design/ant-design-mini/compare/1.0.1...1.1.0)
Expand Down Expand Up @@ -64,7 +68,9 @@ 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 -->
Expand Down
11 changes: 11 additions & 0 deletions test/github/templates/__snapshots__/review.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`github templates pr review can handle pull_request_review_2_1_dismissed_dismissed 1`] = `
{
"compactText": undefined,
"text": "#### [Codertocat](https://github.com/Codertocat) [dismissed](https://github.com/Codertocat/Hello-World/pull/2#pullrequestreview-237895671) review on [pull request](https://github.com/hellomouse/GNS/pull/902)
#### [#2 Update the README with new information.](https://github.com/hellomouse/GNS/pull/902)",
"title": "Review dismissed",
}
`;
exports[`github templates pr review can handle pull_request_review_comment_0_created 1`] = `
{
"compactText": undefined,
"text": "#### [Codertocat](https://github.com/Codertocat) created [review comment](https://github.com/Codertocat/Hello-World/pull/2#discussion_r284312630) on [pull request](https://github.com/Codertocat/Hello-World/pull/2)
[#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
14 changes: 13 additions & 1 deletion test/github/templates/review.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { handleReviewComment } from '@/github/templates/comment';
import { handleReview } from '@/github/templates/review';

import { pull_request_2_review_comment_0_created } from '../../fixtures';
import {
pull_request_2_review_comment_0_created,
pull_request_review_2_1_dismissed_dismissed,
} from '../../fixtures';
import { ctx } from '../ctx';

describe('github templates pr review', () => {
Expand All @@ -12,4 +16,12 @@ describe('github templates pr review', () => {
console.log(`pull_request_review_comment_0_created ~ result`, result);
expect(result).toMatchSnapshot();
});
it('can handle pull_request_review_2_1_dismissed_dismissed', async () => {
const result = await handleReview(
pull_request_review_2_1_dismissed_dismissed,
ctx,
);
console.log(`pull_request_review_2_1_dismissed_dismissed ~ result`, result);
expect(result).toMatchSnapshot();
});
});
13 changes: 9 additions & 4 deletions test/queue/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ exports[`queue can composite discussion 1`] = `
"eventName": "discussion.created",
"markdown": {
"text": "#### [[octo-repo]](https://github.com/octo-org/octo-repo) [Codertocat](https://github.com/Codertocat) created [discussion](https://github.com/octo-org/octo-repo/discussions/90)
***
#### [#90 Welcome to discussions!](https://github.com/octo-org/octo-repo/discussions/90)
***
Expand All @@ -16,8 +15,10 @@ We're glad to have you here!
---
#### [[octo-repo]](https://github.com/octo-org/octo-repo) [Codertocat](https://github.com/Codertocat) created [comment](https://github.com/octo-org/octo-repo/discussions/90#discussioncomment-544078) on [discussion](https://github.com/octo-org/octo-repo/discussions/90)
[#90 Welcome to discussions!](https://github.com/octo-org/octo-repo/discussions/90)
#### [#90 Welcome to discussions!](https://github.com/octo-org/octo-repo/discussions/90)
***
I have so many questions to ask you!",
"title": "[octo-repo] Discussion#90 created",
},
Expand All @@ -31,20 +32,24 @@ exports[`queue can composite pr review 1`] = `
"eventName": "pull_request_review.submitted",
"markdown": {
"text": "#### [[Hello-World]](https://github.com/Codertocat/Hello-World) [Codertocat](https://github.com/Codertocat) [requested](https://github.com/Codertocat/Hello-World/pull/2#pullrequestreview-237895671) changes on [pull request](https://github.com/Codertocat/Hello-World/pull/2)
[#2 Update the README with new information.](https://github.com/Codertocat/Hello-World/pull/2)
#### [#2 Update the README with new information.](https://github.com/Codertocat/Hello-World/pull/2)
---
#### [[Hello-World]](https://github.com/Codertocat/Hello-World) [Codertocat](https://github.com/Codertocat) created [review comment](https://github.com/Codertocat/Hello-World/pull/2#discussion_r284312630) on [pull request](https://github.com/Codertocat/Hello-World/pull/2)
[#2 Update the README with new information.](https://github.com/Codertocat/Hello-World/pull/2)
***
Maybe you should use more emoji on this line.
---
#### [[Hello-World]](https://github.com/Codertocat/Hello-World) [Codertocat](https://github.com/Codertocat) created [review comment](https://github.com/Codertocat/Hello-World/pull/2#discussion_r284312630) on [pull request](https://github.com/Codertocat/Hello-World/pull/2)
[#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 requested changes",
},
Expand All @@ -59,7 +64,7 @@ exports[`queue can composite pr review single 1`] = `
"markdown": {
"compactText": undefined,
"text": "#### [[core]](https://github.com/opensumi/core) [bytemain](https://github.com/bytemain) [approved](https://github.com/opensumi/core/pull/3345#pullrequestreview-1892322711) [pull request](https://github.com/opensumi/core/pull/3345)
[#3345 feat: support SourceControl.historyProvider](https://github.com/opensumi/core/pull/3345)",
#### [#3345 feat: support SourceControl.historyProvider](https://github.com/opensumi/core/pull/3345)",
"title": "[core] Review approved",
},
},
Expand Down

0 comments on commit d7f56d2

Please sign in to comment.