From 9d00d7f8be6e7b2eeb91f8a01246a1d9697881d7 Mon Sep 17 00:00:00 2001 From: me505 <62057938+me505@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:03:27 +0530 Subject: [PATCH] feat: price overide (#735) * feat: price override * feat: price override * feat: price overide * chore: t * chore: t * chore: t * feat: price override * feat: price override * feat: price override * feat: price override * feat: price override * feat: price override * feat: price override * feat: price override * feat: price override * refactor: simplify --------- Co-authored-by: 0xcodercrane <108444211+0xcodercrane@users.noreply.github.com> --- src/handlers/pricing/action.ts | 36 ++++++++++++++++++++---- src/handlers/wildcard/analytics.ts | 4 +-- src/helpers/issue.ts | 44 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/handlers/pricing/action.ts b/src/handlers/pricing/action.ts index 0b8982eaa..7e9673c6f 100644 --- a/src/handlers/pricing/action.ts +++ b/src/handlers/pricing/action.ts @@ -1,7 +1,7 @@ import { getBotConfig, getBotContext, getLogger } from "../../bindings"; import { GLOBAL_STRINGS } from "../../configs"; -import { addCommentToIssue, addLabelToIssue, clearAllPriceLabelsOnIssue, createLabel, getLabel, calculateWeight } from "../../helpers"; -import { Payload } from "../../types"; +import { addCommentToIssue, addLabelToIssue, clearAllPriceLabelsOnIssue, createLabel, getLabel, calculateWeight, getAllLabeledEvents } from "../../helpers"; +import { Payload, UserType } from "../../types"; import { handleLabelsAccess } from "../access"; import { getTargetPriceLabel } from "../shared"; @@ -12,7 +12,7 @@ export const pricingLabelLogic = async (): Promise => { const payload = context.payload as Payload; if (!payload.issue) return; const labels = payload.issue.labels; - + const labelNames = labels.map((i) => i.name); logger.info(`Checking if the issue is a parent issue.`); if (payload.issue.body && isParentIssue(payload.issue.body)) { logger.error("Identified as parent issue. Disabling price label."); @@ -37,10 +37,36 @@ export const pricingLabelLogic = async (): Promise => { const minPriorityLabel = priorityLabels.length > 0 ? priorityLabels.reduce((a, b) => (calculateWeight(a) < calculateWeight(b) ? a : b)).name : undefined; const targetPriceLabel = getTargetPriceLabel(minTimeLabel, minPriorityLabel); + if (targetPriceLabel) { - if (labels.map((i) => i.name).includes(targetPriceLabel)) { - logger.info(`Skipping... already exists`); + const _targetPriceLabel = labelNames.find((name) => name.includes("Price") && name.includes(targetPriceLabel)); + + if (_targetPriceLabel) { + // get all issue events of type "labeled" and the event label includes Price + let labeledEvents = await getAllLabeledEvents(); + if (!labeledEvents) return; + + labeledEvents = labeledEvents.filter((event) => event.label?.name.includes("Price")); + if (!labeledEvents.length) return; + + // check if the latest price label has been added by a user + if (labeledEvents[labeledEvents.length - 1].actor?.type == UserType.User) { + logger.info(`Skipping... already exists`); + } else { + // add price label to issue becuase wrong price has been added by bot + logger.info(`Adding price label to issue`); + await clearAllPriceLabelsOnIssue(); + + const exist = await getLabel(targetPriceLabel); + + if (assistivePricing && !exist) { + logger.info(`${targetPriceLabel} doesn't exist on the repo, creating...`); + await createLabel(targetPriceLabel, "price"); + } + await addLabelToIssue(targetPriceLabel); + } } else { + // add price if there is none logger.info(`Adding price label to issue`); await clearAllPriceLabelsOnIssue(); diff --git a/src/handlers/wildcard/analytics.ts b/src/handlers/wildcard/analytics.ts index 62f24edf9..df877f070 100644 --- a/src/handlers/wildcard/analytics.ts +++ b/src/handlers/wildcard/analytics.ts @@ -2,7 +2,6 @@ import { getMaxIssueNumber, upsertIssue, upsertUser } from "../../adapters/supab import { getBotConfig, getLogger } from "../../bindings"; import { listIssuesForRepo, getUser, calculateWeight } from "../../helpers"; import { Issue, IssueType, User, UserProfile } from "../../types"; -import { getTargetPriceLabel } from "../shared"; /** * Checks the issue whether it's a bounty for hunters or an issue for not @@ -19,6 +18,7 @@ export const bountyInfo = ( } => { const config = getBotConfig(); const labels = issue.labels; + const timeLabels = config.price.timeLabels.filter((item) => labels.map((i) => i.name).includes(item.name)); const priorityLabels = config.price.priorityLabels.filter((item) => labels.map((i) => i.name).includes(item.name)); @@ -27,7 +27,7 @@ export const bountyInfo = ( const minTimeLabel = timeLabels.length > 0 ? timeLabels.reduce((a, b) => (calculateWeight(a) < calculateWeight(b) ? a : b)).name : undefined; const minPriorityLabel = priorityLabels.length > 0 ? priorityLabels.reduce((a, b) => (calculateWeight(a) < calculateWeight(b) ? a : b)).name : undefined; - const priceLabel = getTargetPriceLabel(minTimeLabel, minPriorityLabel); + const priceLabel = labels.find((label) => label.name.includes("Price"))?.name; return { isBounty, diff --git a/src/helpers/issue.ts b/src/helpers/issue.ts index 27ab9562d..c3cc89972 100644 --- a/src/helpers/issue.ts +++ b/src/helpers/issue.ts @@ -3,6 +3,50 @@ import { getBotConfig, getBotContext, getLogger } from "../bindings"; import { AssignEvent, Comment, IssueType, Payload, StreamlinedComment, UserType } from "../types"; import { checkRateLimitGit } from "../utils"; +export const getAllIssueEvents = async () => { + const context = getBotContext(); + const logger = getLogger(); + const payload = context.payload as Payload; + if (!payload.issue) return; + + let shouldFetch = true; + let page_number = 1; + const events = []; + + try { + while (shouldFetch) { + // Fetch issue events + const response = await context.octokit.issues.listEvents({ + owner: payload.repository.owner.login, + repo: payload.repository.full_name, + issue_number: payload.issue.number, + per_page: 100, + page: page_number, + }); + + await checkRateLimitGit(response?.headers); + + if (response?.data?.length > 0) { + events.push(...response.data); + page_number++; + } else { + shouldFetch = false; + } + } + } catch (e: unknown) { + shouldFetch = false; + logger.error(`Getting all issue events failed, reason: ${e}`); + return null; + } + return events; +}; + +export const getAllLabeledEvents = async () => { + const events = await getAllIssueEvents(); + if (!events) return null; + return events.filter((event) => event.event === "labeled"); +}; + export const clearAllPriceLabelsOnIssue = async (): Promise => { const context = getBotContext(); const logger = getLogger();