From 928469976a9db11568f21e72bcdedd19ad74544a Mon Sep 17 00:00:00 2001 From: "lijiacheng.ljc" Date: Wed, 6 Mar 2024 16:10:58 +0800 Subject: [PATCH] fix: do not notify duplicate comment event --- src/github/templates/comment.ts | 17 +++++++++++++---- src/github/templates/prOrIssue.ts | 10 ++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/github/templates/comment.ts b/src/github/templates/comment.ts index 7714add1..dc8746e1 100644 --- a/src/github/templates/comment.ts +++ b/src/github/templates/comment.ts @@ -3,7 +3,7 @@ import { Repository, User } from '@octokit/webhooks-types'; import { StringBuilder, limitLine } from '@/utils/string-builder'; -import { ExtractPayload, Context } from '../types'; +import { ExtractPayload, Context, THasChanges } from '../types'; import { StopHandleError, @@ -53,7 +53,7 @@ export function CommentBody(comment: { return text; } -const meaningfulCommentEditUser = new Set([ +const meaningfulCommentEditUser = new Set([ 'dependabot[bot]', 'renovate[bot]', 'dependabot-preview[bot]', @@ -91,6 +91,16 @@ function renderComment( `do not render ${comment.user.login} comment edit event`, ); } + + const from = (payload as THasChanges).changes?.body?.from; + if (from === undefined) { + throw new StopHandleError('no from in comment edit'); + } + + const now = comment.body; + if (now === from) { + throw new StopHandleError('no change in comment edit'); + } } return Template( @@ -112,9 +122,8 @@ export async function handleIssueComment( ctx: Context, ): Promise { const issue = payload.issue; - const isUnderPullRequest = Boolean(issue.pull_request); let name: Name = 'issues'; - if (isUnderPullRequest) { + if (Boolean(issue.pull_request)) { name = 'pull_request'; } return renderComment(name, payload, issue, ctx); diff --git a/src/github/templates/prOrIssue.ts b/src/github/templates/prOrIssue.ts index 2351f5bb..0ecc2dfc 100644 --- a/src/github/templates/prOrIssue.ts +++ b/src/github/templates/prOrIssue.ts @@ -128,14 +128,14 @@ export async function handlePr( // 说明是标题改变 // 我们只接收标题带有 WIP 的改变 if ( - changes.title.from.toLowerCase().includes('wip') && - !data.title.toLowerCase().includes('wip') + contains(changes.title.from, 'wip') && + !contains(data.title, 'wip') ) { oldTitle = changes.title.from; } if ( - !changes.title.from.toLowerCase().includes('wip') && - data.title.toLowerCase().includes('wip') + !contains(changes.title.from, 'wip') && + contains(data.title, 'wip') ) { oldTitle = changes.title.from; } @@ -207,3 +207,5 @@ export async function handleDiscussion( ): Promise { return render('discussion', payload, payload.discussion, ctx); } + +const contains = (str: string, sub: string) => str.toLowerCase().includes(sub);