From 766307b447bc8af4bd5eff6c7309f750191ac1ee Mon Sep 17 00:00:00 2001 From: AnakinSkywalkeer Date: Sun, 24 Sep 2023 21:02:48 +0530 Subject: [PATCH 1/8] build: making ignoring of children dynamic --- src/configs/ubiquibot-config-default.ts | 1 + src/handlers/payout/post.ts | 10 +++++----- src/helpers/comment.ts | 3 ++- src/types/config.ts | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/configs/ubiquibot-config-default.ts b/src/configs/ubiquibot-config-default.ts index 4a989995f..2c8aeceb6 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: [], }, }, enableAccessControl: { diff --git a/src/handlers/payout/post.ts b/src/handlers/payout/post.ts index 18d9aa1df..d0b23d5a8 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 @@ -79,7 +78,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, calculateIncentives.incentives.comment.ignore_children); const rewardValue = calculateRewardValue(commentsByNode, calculateIncentives.incentives); if (rewardValue.equals(0)) { logger.info(`Skipping to generate a permit url because the reward value is 0. user: ${user}`); @@ -245,8 +244,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, incentivesCalculation.incentives.comment.ignore_children); const rewardValue = calculateRewardValue(commentsByNode, incentivesCalculation.incentives); + if (rewardValue.equals(0)) { logger.info(`calculatePullRequestReviewsReward: Skipping to generate a permit url because the reward value is 0. user: ${user}`); continue; @@ -280,7 +280,7 @@ const generatePermitForComments = async ( paymentPermitMaxPrice: number ): Promise => { const logger = getLogger(); - const commentsByNode = await parseComments(comments, ItemsToExclude); + const commentsByNode = await parseComments(comments, incentives.comment.ignore_children); const rewardValue = calculateRewardValue(commentsByNode, incentives); if (rewardValue.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 cba6c165f..a2a9e2494 100644 --- a/src/helpers/comment.ts +++ b/src/helpers/comment.ts @@ -9,6 +9,7 @@ type Node = { const traverse = (result: Record, node: Node, itemsToExclude: string[]): Record => { if (itemsToExclude.includes(node.nodeName)) { + result[node.nodeName].push(node.nodeName); return result; } @@ -18,7 +19,7 @@ const traverse = (result: Record, node: Node, itemsToExclude: result[node.nodeName].push(node.value?.trim() ?? ""); - if (node.childNodes && node.childNodes.length > 0) { + if (node.childNodes && node.childNodes.length > 0 && !itemsToExclude.includes(node.nodeName)) { node.childNodes.forEach((child) => traverse(result, child, itemsToExclude)); } diff --git a/src/types/config.ts b/src/types/config.ts index cd6d6d109..cf7bc79b0 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -20,6 +20,7 @@ const CommentIncentivesSchema = Type.Object( }, { additionalProperties: false } ), + ignore_children: Type.Array(Type.String()), }, { additionalProperties: false } ); From 7154de7b70c8ad0bc12c244c6a8394ea115d14f0 Mon Sep 17 00:00:00 2001 From: Gary <144094016+GaryThisSidee@users.noreply.github.com> Date: Mon, 25 Sep 2023 15:48:58 +0530 Subject: [PATCH 2/8] build: added children tag names Co-authored-by: whilefoo <139262667+whilefoo@users.noreply.github.com> --- src/configs/ubiquibot-config-default.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/configs/ubiquibot-config-default.ts b/src/configs/ubiquibot-config-default.ts index 2c8aeceb6..1f4f4cbaf 100644 --- a/src/configs/ubiquibot-config-default.ts +++ b/src/configs/ubiquibot-config-default.ts @@ -91,7 +91,7 @@ export const DefaultConfig: MergedConfig = { totals: { word: 0, }, - ignore_children: [], + ignore_children: ["code", "i", "em", "blockquote", "pre"], }, }, enableAccessControl: { From 05af9104c8bc37c9d766dfcffc0ffeac30fd0ca5 Mon Sep 17 00:00:00 2001 From: AnakinSkywalkeer Date: Mon, 25 Sep 2023 15:59:40 +0530 Subject: [PATCH 3/8] fix: removed the condition which was not needed --- src/helpers/comment.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/helpers/comment.ts b/src/helpers/comment.ts index a2a9e2494..bbb912490 100644 --- a/src/helpers/comment.ts +++ b/src/helpers/comment.ts @@ -8,10 +8,7 @@ type Node = { }; const traverse = (result: Record, node: Node, itemsToExclude: string[]): Record => { - if (itemsToExclude.includes(node.nodeName)) { - result[node.nodeName].push(node.nodeName); - return result; - } + result[node.nodeName].push(node.nodeName); if (!result[node.nodeName]) { result[node.nodeName] = []; From 71491f829077d231c1369fcd9afb0242f115ed0c Mon Sep 17 00:00:00 2001 From: Gary <144094016+GaryThisSidee@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:29:38 +0530 Subject: [PATCH 4/8] fix: removed the uncessary line Co-authored-by: whilefoo <139262667+whilefoo@users.noreply.github.com> --- src/helpers/comment.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/helpers/comment.ts b/src/helpers/comment.ts index bbb912490..866f7c2fa 100644 --- a/src/helpers/comment.ts +++ b/src/helpers/comment.ts @@ -8,7 +8,6 @@ type Node = { }; const traverse = (result: Record, node: Node, itemsToExclude: string[]): Record => { - result[node.nodeName].push(node.nodeName); if (!result[node.nodeName]) { result[node.nodeName] = []; From 6edb475604a2cccf6bdf975460074bfec98b2b3c Mon Sep 17 00:00:00 2001 From: AnakinSkywalkeer Date: Wed, 27 Sep 2023 02:35:26 +0530 Subject: [PATCH 5/8] fix: made the comments parser functions to take the ignore children from the config directly --- src/handlers/payout/post.ts | 6 +++--- src/helpers/comment.ts | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/handlers/payout/post.ts b/src/handlers/payout/post.ts index d0b23d5a8..b0518f072 100644 --- a/src/handlers/payout/post.ts +++ b/src/handlers/payout/post.ts @@ -78,7 +78,7 @@ export const calculateIssueConversationReward = async (calculateIncentives: Ince for (const user of Object.keys(issueCommentsByUser)) { const commentsByUser = issueCommentsByUser[user]; - const commentsByNode = await parseComments(commentsByUser.comments, calculateIncentives.incentives.comment.ignore_children); + const commentsByNode = await parseComments(commentsByUser.comments); const rewardValue = calculateRewardValue(commentsByNode, calculateIncentives.incentives); if (rewardValue.equals(0)) { logger.info(`Skipping to generate a permit url because the reward value is 0. user: ${user}`); @@ -244,7 +244,7 @@ export const calculatePullRequestReviewsReward = async (incentivesCalculation: I for (const user of Object.keys(prReviewsByUser)) { const commentByUser = prReviewsByUser[user]; - const commentsByNode = await parseComments(commentByUser.comments, incentivesCalculation.incentives.comment.ignore_children); + const commentsByNode = await parseComments(commentByUser.comments); const rewardValue = calculateRewardValue(commentsByNode, incentivesCalculation.incentives); if (rewardValue.equals(0)) { @@ -280,7 +280,7 @@ const generatePermitForComments = async ( paymentPermitMaxPrice: number ): Promise => { const logger = getLogger(); - const commentsByNode = await parseComments(comments, incentives.comment.ignore_children); + const commentsByNode = await parseComments(comments); const rewardValue = calculateRewardValue(commentsByNode, incentives); if (rewardValue.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 866f7c2fa..38e9abbd9 100644 --- a/src/helpers/comment.ts +++ b/src/helpers/comment.ts @@ -1,4 +1,5 @@ import * as parse5 from "parse5"; +import { DefaultConfig } from "../configs"; type Node = { nodeName: string; @@ -7,27 +8,26 @@ type Node = { childNodes?: Node[]; }; -const traverse = (result: Record, node: Node, itemsToExclude: string[]): Record => { - +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 && !itemsToExclude.includes(node.nodeName)) { - node.childNodes.forEach((child) => traverse(result, child, itemsToExclude)); + if (node.childNodes && node.childNodes.length > 0 && !DefaultConfig.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 From 254a52b55373bd6440f2dd34775a0ef4da488458 Mon Sep 17 00:00:00 2001 From: AnakinSkywalkeer Date: Wed, 4 Oct 2023 15:21:50 +0530 Subject: [PATCH 6/8] fix: used the getBotConfig instead of default ubiquity config file --- src/helpers/comment.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/comment.ts b/src/helpers/comment.ts index f72790592..86bb47158 100644 --- a/src/helpers/comment.ts +++ b/src/helpers/comment.ts @@ -1,7 +1,7 @@ import Decimal from "decimal.js"; import { isEmpty } from "lodash"; import * as parse5 from "parse5"; -import { DefaultConfig } from "../configs"; +import { getBotConfig } from "../bindings"; type Node = { nodeName: string; @@ -17,7 +17,7 @@ const traverse = (result: Record, node: Node): Record 0 && !DefaultConfig.incentives.comment.ignore_children.includes(node.nodeName)) { + if (node.childNodes && node.childNodes.length > 0 && !getBotConfig().price.incentives.comment.ignore_children.includes(node.nodeName)) { node.childNodes.forEach((child) => traverse(result, child)); } From c295810d283ea4b0eb830870a5405e58000fdc5a Mon Sep 17 00:00:00 2001 From: Gary <144094016+GaryThisSidee@users.noreply.github.com> Date: Fri, 6 Oct 2023 00:28:53 +0530 Subject: [PATCH 7/8] fix: make the ignore_children property optional Co-authored-by: whilefoo <139262667+whilefoo@users.noreply.github.com> --- src/types/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/config.ts b/src/types/config.ts index 9e5474f03..49a277931 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -20,7 +20,7 @@ const CommentIncentivesSchema = Type.Object( }, { additionalProperties: false } ), - ignore_children: Type.Array(Type.String()), + ignore_children: Type.Optional(Type.Array(Type.String())), }, { additionalProperties: false } ); From d907861b03167ba3f560b70f2e2904781832af7f Mon Sep 17 00:00:00 2001 From: AnakinSkywalkeer Date: Thu, 5 Oct 2023 15:10:48 +0530 Subject: [PATCH 8/8] fix: placed a Optional chaining operator --- src/helpers/comment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/comment.ts b/src/helpers/comment.ts index 86bb47158..8cb3ebbeb 100644 --- a/src/helpers/comment.ts +++ b/src/helpers/comment.ts @@ -17,7 +17,7 @@ const traverse = (result: Record, node: Node): Record 0 && !getBotConfig().price.incentives.comment.ignore_children.includes(node.nodeName)) { + if (node.childNodes && node.childNodes.length > 0 && !getBotConfig().price.incentives.comment.ignore_children?.includes(node.nodeName)) { node.childNodes.forEach((child) => traverse(result, child)); }