Skip to content

Commit

Permalink
feat: optimize multiple view renderer effect
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Feb 28, 2024
1 parent 96d3b2d commit 573e1c8
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 54 deletions.
8 changes: 6 additions & 2 deletions src/github/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import { error, json } from '@/api/utils/response';
import Environment from '@/env';
import { Logger } from '@/utils/logger';

import { getTemplates, StopHandleError } from './templates';
import type { Context, IHasSender, TemplateRenderResult } from './types';
import {
getTemplates,
StopHandleError,
TemplateRenderResult,
} from './templates';
import type { Context, IHasSender } from './types';

export class ValidationError extends Error {
constructor(public statusCode: number, message: string) {
Expand Down
6 changes: 4 additions & 2 deletions src/github/templates/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Repository, User } from '@octokit/webhooks-types';

import { StringBuilder } from '@/utils/string-builder';

import { ExtractPayload, Context, TemplateRenderResult } from '../types';
import { ExtractPayload, Context } from '../types';

import { Name, NameBlock } from './prOrIssue';
import {
StopHandleError,
TemplateRenderResult,
limitLine,
renderPrOrIssueLink,
renderPrOrIssueTitleLink,
textTpl,
useRef,
Expand Down Expand Up @@ -90,6 +90,7 @@ function renderComment(
target: renderPrOrIssueTitleLink(data),
title: `{{sender | link:sender}} ${action} [comment](${comment.html_url}) on [${location}](${data.html_url})`,
body: renderCommentBody(payload.comment),
compactTitle: `{{sender | link:sender}} ${action} [comment](${data.html_url}): \n`,
doNotRenderBody,
},
ctx,
Expand Down Expand Up @@ -196,6 +197,7 @@ export async function handleReviewComment(
target: '{{pull_request|link}}',
title: `{{sender | link:sender}} ${action} [review comment](${comment.html_url}) on [pull request](${pr.html_url})`,
body: renderCommentBody(payload.comment),
compactTitle: `{{sender | link}} ${action} [review comment](${comment.html_url}): \n`,
},
ctx,
);
Expand Down
3 changes: 2 additions & 1 deletion src/github/templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pick from 'lodash/pick';

import { Context, TemplateRenderResult } from '@/github/types';
import { Context } from '@/github/types';

import {
handleCommitComment,
Expand All @@ -12,6 +12,7 @@ import { handleDiscussion, handleIssue, handlePr } from './prOrIssue';
import { handleRelease } from './release';
import { handleReview } from './review';
import { handleStar } from './star';
import { TemplateRenderResult } from './utils';
import { handleWorkflowRun } from './workflow';
export * from './utils';

Expand Down
8 changes: 2 additions & 6 deletions src/github/templates/prOrIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { Issue, PullRequest, Discussion } from '@octokit/webhooks-types';

import { StringBuilder } from '@/utils/string-builder';

import {
ExtractPayload,
THasChanges,
Context,
TemplateRenderResult,
} from '../types';
import { ExtractPayload, THasChanges, Context } from '../types';

import {
renderPrOrIssueTitleLink,
Expand All @@ -19,6 +14,7 @@ import {
renderRequestedReviewersInfo,
textTpl,
prettyUnderlineWord,
TemplateRenderResult,
} from './utils';

export type Name = 'issues' | 'pull_request' | 'discussion';
Expand Down
4 changes: 2 additions & 2 deletions src/github/templates/release.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StringBuilder } from '@/utils/string-builder';

import { Context, ExtractPayload, TemplateRenderResult } from '../types';
import { Context, ExtractPayload } from '../types';
import { replaceGitHubUrlToMarkdown } from '../utils';

import { textTpl } from '.';
import { TemplateRenderResult, textTpl } from './utils';

export async function handleRelease(
payload: ExtractPayload<'release'>,
Expand Down
4 changes: 2 additions & 2 deletions src/github/templates/review.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, ExtractPayload, TemplateRenderResult } from '../types';
import { Context, ExtractPayload } from '../types';

import { StopHandleError, textTpl } from './utils';
import { StopHandleError, textTpl, TemplateRenderResult } from './utils';

export async function handleReview(
payload: ExtractPayload<'pull_request_review'>,
Expand Down
4 changes: 2 additions & 2 deletions src/github/templates/star.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from '../renderer';
import { ExtractPayload, TemplateRenderResult } from '../types';
import { ExtractPayload } from '../types';

import { StopHandleError } from './utils';
import { StopHandleError, TemplateRenderResult } from './utils';

export async function handleStar(
payload: ExtractPayload<'star'>,
Expand Down
14 changes: 7 additions & 7 deletions src/github/templates/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import capitalize from 'lodash/capitalize';
import { StringBuilder, limitTextByPosition } from '@/utils/string-builder';

import { render } from '../renderer';
import { Context } from '../types';
import { replaceGitHubText } from '../utils';

export function renderRepoLink(repository: { name: string; html_url: string }) {
return `[[${repository.name}]](${repository.html_url})`;
Expand Down Expand Up @@ -227,6 +225,12 @@ export type TextTplInput = {
autoRef?: boolean;
};

export type TemplateRenderResult = {
title: string;
text: string;
compactText?: string;
};

type TextTpl = (
data: TextTplInput,
ctx?: {
Expand All @@ -239,11 +243,7 @@ type TextTpl = (
afterRender?: (result: string) => string;
};
},
) => {
title: string;
text: string;
compactText?: string;
};
) => TemplateRenderResult;

export type HandlerResult = {
text: TextTplInput;
Expand Down
4 changes: 2 additions & 2 deletions src/github/templates/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Octokit } from '@octokit/rest';

import { StringBuilder } from '@/utils/string-builder';

import { Context, ExtractPayload, TemplateRenderResult } from '../types';
import { Context, ExtractPayload } from '../types';

import { textTpl, StopHandleError } from '.';
import { textTpl, StopHandleError, TemplateRenderResult } from './utils';

function renderWorkflow(
payload: ExtractPayload<'workflow_run'>,
Expand Down
9 changes: 0 additions & 9 deletions src/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ export type MarkdownContent = {
text: string;
};

export type TemplateRenderResult = {
title: string;
text: string;
detail?: {
bodyText: string;
bodyHeader: string;
};
};

export type THasAction = {
action?: string;
};
Expand Down
8 changes: 5 additions & 3 deletions src/queue/worker/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import DefaultMap from 'mnemonist/default-map';

import { initApp } from '@/github/app';
import { setupWebhooksTemplate } from '@/github/handler';
import { TemplateRenderResult } from '@/github/types';
import { TemplateRenderResult } from '@/github/templates';
import { sendToDing } from '@/github/utils';
import { GitHubKVManager } from '@/kv/github';
import { ISetting } from '@/kv/types';
Expand Down Expand Up @@ -222,14 +222,16 @@ class EventComposite {

const result = [] as IResult[];

const separator = '\n\n---\n\n';
const separator = '\n\n***\n\n';

if (subView.length > 0) {
const chunked = chunk(subView, 5);
chunked.forEach((v, i) => {
let title = '';
let eventName = '';
let text = v.map((d) => d.markdown.text).join(separator);
let text = v
.map((d) => d.markdown.compactText || d.markdown.text)
.join(separator);

if (i === 0 && mainView) {
title = mainView.markdown.title;
Expand Down
4 changes: 3 additions & 1 deletion test/github/templates/__snapshots__/review.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ exports[`github templates pr review can handle pull_request_review_2_1_dismissed
exports[`github templates pr review can handle pull_request_review_comment_0_created 1`] = `
{
"compactText": undefined,
"compactText": "[Codertocat](https://github.com/Codertocat) created [review comment](https://github.com/Codertocat/Hello-World/pull/2#discussion_r284312630):
Maybe you should use more emoji on this line.",
"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)
Expand Down
21 changes: 6 additions & 15 deletions test/queue/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ exports[`queue can composite discussion 1`] = `
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)
***
[Codertocat](https://github.com/Codertocat) created [comment](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 @@ -34,22 +31,16 @@ exports[`queue can composite pr review 1`] = `
"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)
---
#### [[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.
---
[Codertocat](https://github.com/Codertocat) created [review comment](https://github.com/Codertocat/Hello-World/pull/2#discussion_r284312630):
#### [[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.
***
[Codertocat](https://github.com/Codertocat) created [review comment](https://github.com/Codertocat/Hello-World/pull/2#discussion_r284312630):
Maybe you should use more emojji on this line.",
"title": "[Hello-World] Review requested changes",
},
Expand Down

0 comments on commit 573e1c8

Please sign in to comment.