diff --git a/src/configs/ubiquibot-config-default.ts b/src/configs/ubiquibot-config-default.ts index 9598ef782..9010a8907 100644 --- a/src/configs/ubiquibot-config-default.ts +++ b/src/configs/ubiquibot-config-default.ts @@ -91,6 +91,7 @@ export const DefaultConfig: MergedConfig = { totals: { word: 0, }, + ignore_children: ["code", "i", "em", "blockquote", "pre"], }, }, enableAccessControl: { diff --git a/src/handlers/payout/post.ts b/src/handlers/payout/post.ts index e1ef60e76..8ed8ce163 100644 --- a/src/handlers/payout/post.ts +++ b/src/handlers/payout/post.ts @@ -2,7 +2,7 @@ import { getWalletAddress } from "../../adapters/supabase"; import { getBotContext, getLogger } from "../../bindings"; import { getAllIssueComments, getAllPullRequestReviews, getIssueDescription, parseComments } from "../../helpers"; import { getLatestPullRequest, gitLinkedPrParser } from "../../helpers/parser"; -import { Incentives, MarkdownItem, Payload, UserType } from "../../types"; +import { Incentives, Payload, UserType } from "../../types"; import { RewardsResponse, commentParser } from "../comment"; import Decimal from "decimal.js"; import { bountyInfo } from "../wildcard"; @@ -19,7 +19,6 @@ export interface CreatorCommentResult { user?: string | undefined; } -const ItemsToExclude: string[] = [MarkdownItem.BlockQuote]; /** * Incentivize the contributors based on their contribution. * The default formula has been defined in https://github.com/ubiquity/ubiquibot/issues/272 @@ -78,7 +77,7 @@ export const calculateIssueConversationReward = async (calculateIncentives: Ince for (const user of Object.keys(issueCommentsByUser)) { const commentsByUser = issueCommentsByUser[user]; - const commentsByNode = await parseComments(commentsByUser.comments, ItemsToExclude); + const commentsByNode = await parseComments(commentsByUser.comments); const rewardValue = calculateRewardValue(commentsByNode, calculateIncentives.incentives); if (rewardValue.sum.equals(0)) { logger.info(`Skipping to generate a permit url because the reward value is 0. user: ${user}`); @@ -234,8 +233,9 @@ export const calculatePullRequestReviewsReward = async (incentivesCalculation: I for (const user of Object.keys(prReviewsByUser)) { const commentByUser = prReviewsByUser[user]; - const commentsByNode = await parseComments(commentByUser.comments, ItemsToExclude); + const commentsByNode = await parseComments(commentByUser.comments); const rewardValue = calculateRewardValue(commentsByNode, incentivesCalculation.incentives); + if (rewardValue.sum.equals(0)) { logger.info(`calculatePullRequestReviewsReward: Skipping to generate a permit url because the reward value is 0. user: ${user}`); continue; @@ -271,7 +271,7 @@ const generatePermitForComments = async ( paymentPermitMaxPrice: number ): Promise => { const logger = getLogger(); - const commentsByNode = await parseComments(comments, ItemsToExclude); + const commentsByNode = await parseComments(comments); const rewardValue = calculateRewardValue(commentsByNode, incentives); if (rewardValue.sum.equals(0)) { logger.info(`No reward for the user: ${user}. comments: ${JSON.stringify(commentsByNode)}, sum: ${rewardValue}`); diff --git a/src/helpers/comment.ts b/src/helpers/comment.ts index 7be39376c..8cb3ebbeb 100644 --- a/src/helpers/comment.ts +++ b/src/helpers/comment.ts @@ -1,6 +1,7 @@ import Decimal from "decimal.js"; import { isEmpty } from "lodash"; import * as parse5 from "parse5"; +import { getBotConfig } from "../bindings"; type Node = { nodeName: string; @@ -9,30 +10,26 @@ type Node = { childNodes?: Node[]; }; -const traverse = (result: Record, node: Node, itemsToExclude: string[]): Record => { - if (itemsToExclude.includes(node.nodeName)) { - return result; - } - +const traverse = (result: Record, node: Node): Record => { if (!result[node.nodeName]) { result[node.nodeName] = []; } result[node.nodeName].push(node.value?.trim() ?? ""); - if (node.childNodes && node.childNodes.length > 0) { - node.childNodes.forEach((child) => traverse(result, child, itemsToExclude)); + if (node.childNodes && node.childNodes.length > 0 && !getBotConfig().price.incentives.comment.ignore_children?.includes(node.nodeName)) { + node.childNodes.forEach((child) => traverse(result, child)); } return result; }; -export const parseComments = (comments: string[], itemsToExclude: string[]): Record => { +export const parseComments = (comments: string[]): Record => { const result: Record = {}; for (const comment of comments) { const fragment = parse5.parseFragment(comment); - traverse(result, fragment as Node, itemsToExclude); + traverse(result, fragment as Node); } // remove empty values diff --git a/src/types/config.ts b/src/types/config.ts index a22683a28..49a277931 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -20,6 +20,7 @@ const CommentIncentivesSchema = Type.Object( }, { additionalProperties: false } ), + ignore_children: Type.Optional(Type.Array(Type.String())), }, { additionalProperties: false } );