Skip to content

Commit

Permalink
feat: price overide (ubiquity#735)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
BeanieMen and 0xcodercrane committed Oct 3, 2023
1 parent f0b3ac9 commit 9d00d7f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
36 changes: 31 additions & 5 deletions src/handlers/pricing/action.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,7 +12,7 @@ export const pricingLabelLogic = async (): Promise<void> => {
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.");
Expand All @@ -37,10 +37,36 @@ export const pricingLabelLogic = async (): Promise<void> => {
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();

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/wildcard/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));

Expand All @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions src/helpers/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
const context = getBotContext();
const logger = getLogger();
Expand Down

0 comments on commit 9d00d7f

Please sign in to comment.