Skip to content

Commit

Permalink
refactor: unified template function
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Feb 7, 2024
1 parent 209248e commit ec9ffdb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 155 deletions.
21 changes: 6 additions & 15 deletions src/github/render/template-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ const defaultOperators = {

return value;
},
} as Record<
string,
(operator: string, value: string, context: Record<string, any>) => string
>;
} as Record<string, (operator: string, key: string, value: any) => string>;

function processInterpolation(
expression: string,
context: Record<string, any>,
) {
function processInterpolation(expression: string, context: any) {
let key = expression;
const operators = [];
if (expression.includes('|')) {
Expand All @@ -54,20 +48,17 @@ function processInterpolation(
}

export function processKey(key: string) {
// 1. remove all spaces and new lines
// remove all spaces and new lines
key = key.replace(/\s/g, '');
return key;
}

export function render(template: string, context: Record<string, any>): string {
export function render(template: string, context: any): string {
return template.replace(/{{\s*([^}]+)\s*}}/g, (raw, key) => {
try {
console.log(`🚀 ~ key:`, key);
key = processKey(key);

return processInterpolation(key, context);
return processInterpolation(processKey(key), context);
} catch (e) {
console.error(`template engine error`, e);
console.error(`render template engine error`, e);
return raw;
}
});
Expand Down
47 changes: 8 additions & 39 deletions src/github/templates/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
limitLine,
renderPrOrIssueLink,
textTpl,
titleTpl,
useRef,
} from './utils';

Expand Down Expand Up @@ -105,29 +104,19 @@ function renderComment(
notRenderBody = true;
}

const title = titleTpl(
{
payload,
event: `${location} comment`,
action,
},
ctx,
);

const text = textTpl(
{
payload,
action,
event: `${location} comment`,
title: `{{sender | link:sender}} ${action} [comment](${comment.html_url}) on [${location}](${data.html_url})`,
body: renderCommentBody(data, payload.comment, ctx.setting.contentLimit),
notRenderBody,
},
ctx,
);

return {
title,
...text,
};
return text;
}

export async function handleIssueComment(
Expand Down Expand Up @@ -177,19 +166,12 @@ export async function handleCommitComment(
}
}

const title = titleTpl(
const text = textTpl(
{
payload,
event: 'commit comment',
action: 'created',
},
ctx,
);

const text = textTpl(
{
payload,
title: `{{sender | link}} commented on [${commitRefInfo}]({{comment.html_url}})`,
title: `{{sender | link}} {{action}} comment on [${commitRefInfo}]({{comment.html_url}})`,
body: renderCommentBody(
{
html_url: comment.html_url,
Expand All @@ -202,10 +184,7 @@ export async function handleCommitComment(
ctx,
);

return {
title,
...text,
};
return text;
}

const allowedReviewCommentAction = new Set(['created']);
Expand All @@ -223,26 +202,16 @@ export async function handleReviewComment(
throw new StopHandleError(`not support action ${action}`);
}

const title = titleTpl(
const text = textTpl(
{
payload,
event: 'review comment',
action,
},
ctx,
);

const text = textTpl(
{
payload,
title: `{{sender | link:sender}} ${action} [review comment](${comment.html_url}) on [pull request](${pr.html_url})`,
body: renderCommentBody(pr, payload.comment, ctx.setting.contentLimit),
},
ctx,
);

return {
title,
...text,
};
return text;
}
32 changes: 6 additions & 26 deletions src/github/templates/prOrIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import {
renderPrOrIssueTitleLink,
renderDeletedPrOrIssueTitleLink,
titleTpl,
renderPrOrIssueBody,
StopHandleError,
renderPrRefInfo,
Expand Down Expand Up @@ -85,27 +84,17 @@ function render(
}

const text = textTpl(
{
payload,
title: textFirstLine,
body: builder.build(),
},
ctx,
);

const title = titleTpl(
{
payload,
event: `${nameBlock}#${data.number}`,
action,
title: textFirstLine,
body: builder.build(),
},
ctx,
);

return {
title,
...text,
};
return text;
}

export async function handlePr(
Expand Down Expand Up @@ -210,24 +199,15 @@ export async function handlePr(
const text = textTpl(
{
payload,
event: `${nameBlock}#${data.number}`,
action,
title: `{{sender | link:sender}} ${action} [${nameBlock}](${data.html_url})`,
body: builder.build(),
},
ctx,
);

const title = titleTpl(
{
payload,
event: `${nameBlock}#${data.number}`,
action,
},
ctx,
);
return {
title,
...text,
};
return text;
}

export async function handleIssue(
Expand Down
16 changes: 4 additions & 12 deletions src/github/templates/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { StringBuilder } from '@/utils/string-builder';
import { Context, ExtractPayload, TemplateRenderResult } from '../types';
import { replaceGitHubUrlToMarkdown } from '../utils';

import { titleTpl } from './utils';

import { useRef, textTpl } from '.';

export async function handleRelease(
Expand All @@ -13,15 +11,6 @@ export async function handleRelease(
): Promise<TemplateRenderResult> {
const action = payload.action;
const release = payload.release;
const title = titleTpl(
{
payload,
event: release.name,
action,
},
ctx,
false,
);

const builder = new StringBuilder();

Expand All @@ -43,14 +32,17 @@ export async function handleRelease(
const text = textTpl(
{
payload,
event: release.name,
action,
title: `{{ sender | link }} ${action} {{ release | link }}`,
body: replaceGitHubUrlToMarkdown(builder.build(), {
owner: payload.repository.owner.login,
repo: payload.repository.name,
}),
notCapitalizeTitle: true,
},
ctx,
);

return { title, ...text };
return text;
}
24 changes: 4 additions & 20 deletions src/github/templates/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { StringBuilder } from '@/utils/string-builder';

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

import {
StopHandleError,
useRef,
titleTpl,
renderPrOrIssueTitleLink,
textTpl,
} from '.';
import { StopHandleError, useRef, renderPrOrIssueTitleLink, textTpl } from '.';

export async function handleReview(
payload: ExtractPayload<'pull_request_review'>,
Expand Down Expand Up @@ -44,15 +38,6 @@ export async function handleReview(
something = 'review';
}

const title = titleTpl(
{
payload,
event: 'review',
action: titleActionText,
},
ctx,
);

const builder = new StringBuilder();

builder.add(renderPrOrIssueTitleLink(pr));
Expand All @@ -67,14 +52,13 @@ export async function handleReview(
const text = textTpl(
{
payload,
event: 'review',
action: titleActionText,
title: textFirstLine,
body: builder.build(),
},
ctx,
);

return {
title,
...text,
};
return text;
}
48 changes: 19 additions & 29 deletions src/github/templates/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,37 +249,13 @@ export class StopHandleError extends Error {
}
}

type TitleTpl = (
data: {
payload: any;
event: string;
action: string;
},
ctx: Context,
capitalize?: boolean,
) => string;

export const titleTpl: TitleTpl = (data, ctx, shouldCapitalize = true) => {
const { payload } = data;

let event = data.event;
if (shouldCapitalize) {
event = capitalize(event);
}
const info = `${event} ${data.action}`;
let text;
if (ctx.setting.notDisplayRepoName) {
text = info;
} else {
text = `[{{repository.name}}] ${info}`;
}
return render(text, payload);
};

export type TextTplInput = {
payload: any;
title: string;
body: string;
event: string;
action: string;
notCapitalizeTitle?: boolean;
notRenderBody?: boolean;
};

Expand All @@ -291,6 +267,7 @@ type TextTpl = (
};
},
) => {
title: string;
text: string;
detail: {
bodyText: string;
Expand All @@ -303,14 +280,14 @@ export type HandlerResult = {
};

export const textTpl: TextTpl = (data, ctx) => {
const { payload, title, body } = data;
const { payload, title: bodyTitle, body, notCapitalizeTitle } = data;
const repo = payload.repository;

let repoInfo = renderRepoLink(repo) + ' ';
if (ctx?.setting?.notDisplayRepoName) {
repoInfo = '';
}
const bodyHeader = render(`#### ${repoInfo}${title.trim()} `, payload);
const bodyHeader = render(`#### ${repoInfo}${bodyTitle.trim()} `, payload);

const text = new StringBuilder(bodyHeader);

Expand All @@ -326,7 +303,20 @@ export const textTpl: TextTpl = (data, ctx) => {

bodyText = render(bodyText, payload);

let event = data.event;
if (!notCapitalizeTitle) {
event = capitalize(event);
}
const info = `${event} ${data.action}`;
let title = '';
if (ctx?.setting?.notDisplayRepoName) {
title = info;
} else {
title = `[{{repository.name}}] ${info}`;
}

return {
title: render(title, payload),
text: text.toString(),
detail: {
bodyText,
Expand Down
Loading

0 comments on commit ec9ffdb

Please sign in to comment.