Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

build: making ignoring of children dynamic #825

Open
wants to merge 11 commits into
base: development
Choose a base branch
from
1 change: 1 addition & 0 deletions src/configs/ubiquibot-config-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig: MergedConfig = {
totals: {
word: 0,
},
ignore_children: ["code", "i", "em", "blockquote", "pre"],
},
},
enableAccessControl: {
Expand Down
10 changes: 5 additions & 5 deletions src/handlers/payout/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -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}`);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -271,7 +271,7 @@ const generatePermitForComments = async (
paymentPermitMaxPrice: number
): Promise<undefined | { account: string; amountInETH: Decimal }> => {
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}`);
Expand Down
15 changes: 6 additions & 9 deletions src/helpers/comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Decimal from "decimal.js";
import { isEmpty } from "lodash";
import * as parse5 from "parse5";
import { DefaultConfig } from "../configs";

type Node = {
nodeName: string;
Expand All @@ -9,30 +10,26 @@
childNodes?: Node[];
};

const traverse = (result: Record<string, string[]>, node: Node, itemsToExclude: string[]): Record<string, string[]> => {
if (itemsToExclude.includes(node.nodeName)) {
return result;
}

const traverse = (result: Record<string, string[]>, node: Node): Record<string, string[]> => {
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 && !DefaultConfig.incentives.comment.ignore_children.includes(node.nodeName)) {
whilefoo marked this conversation as resolved.
Show resolved Hide resolved
node.childNodes.forEach((child) => traverse(result, child));
}

return result;
};

export const parseComments = (comments: string[], itemsToExclude: string[]): Record<string, string[]> => {
export const parseComments = (comments: string[]): Record<string, string[]> => {
const result: Record<string, string[]> = {};

for (const comment of comments) {
const fragment = parse5.parseFragment(comment);
traverse(result, fragment as Node, itemsToExclude);
traverse(result, fragment as Node);
}

// remove empty values
Expand Down Expand Up @@ -99,7 +96,7 @@

if (!isEmpty(debug)) {
const data = Object.entries(debug)
.filter(([_, value]) => value.count > 0)

Check warning on line 99 in src/helpers/comment.ts

View workflow job for this annotation

GitHub Actions / build

'_' is defined but never used
.map(([key, value]) => {
const element = key === "#text" ? "words" : key;
const units = value.count;
Expand Down
1 change: 1 addition & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const CommentIncentivesSchema = Type.Object(
},
{ additionalProperties: false }
),
ignore_children: Type.Array(Type.String()),
GaryThisSidee marked this conversation as resolved.
Show resolved Hide resolved
},
{ additionalProperties: false }
);
Expand Down
Loading