Skip to content

Commit

Permalink
Merge pull request #65 from gentlementlegen/fix/prio-zero
Browse files Browse the repository at this point in the history
fix: priority 0 is now properly handled and prices at zero
  • Loading branch information
gentlementlegen authored Dec 17, 2024
2 parents cd4ceb6 + c4c22d0 commit 3b5a250
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/handlers/handle-parent-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export async function handleParentIssue(context: Context, labels: Label[]) {
}

export function sortLabelsByValue(labels: Label[]) {
return labels.sort((a, b) => calculateLabelValue(a.name) - calculateLabelValue(b.name));
return labels.sort((a, b) => {
return (calculateLabelValue(a.name) || 0) - (calculateLabelValue(b.name) || 0);
});
}

export function isParentIssue(body: string) {
Expand Down
8 changes: 7 additions & 1 deletion src/handlers/sync-labels-to-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export async function syncPriceLabelsToConfig(context: Context): Promise<void> {
const priceLabels: { name: string; collaboratorOnly: boolean }[] = [];
for (const timeLabel of config.labels.time) {
for (const priorityLabel of config.labels.priority) {
const targetPrice = calculateTaskPrice(context, calculateLabelValue(timeLabel.name), calculateLabelValue(priorityLabel.name), config.basePriceMultiplier);
const timeValue = calculateLabelValue(timeLabel.name);
const priorityValue = calculateLabelValue(priorityLabel.name);
if (timeValue === null || priorityValue === null) {
logger.info("Time or Priority label is not defined, skipping.", { timeLabel, priorityLabel });
continue;
}
const targetPrice = calculateTaskPrice(context, timeValue, priorityValue, config.basePriceMultiplier);
const targetPriceLabel = `Price: ${targetPrice} USD`;
priceLabels.push({ name: targetPriceLabel, collaboratorOnly: false });
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ createActionsPlugin<AssistivePricingSettings, Env, Command, SupportedEvents>(
settingsSchema: pluginSettingsSchema,
logLevel: process.env.LOG_LEVEL as LogLevel,
kernelPublicKey: process.env.KERNEL_PUBLIC_KEY,
bypassSignatureVerification: process.env.NODE_ENV === "local",
}
).catch(console.error);
14 changes: 9 additions & 5 deletions src/shared/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ export function getPrice(context: Context, timeLabel: Label, priorityLabel: Labe
if (!recognizedPriorityLabels) throw logger.error("Priority label is not recognized");

const timeValue = calculateLabelValue(recognizedTimeLabels.name);
if (!timeValue) throw logger.error("Time value is not defined");
if (timeValue === null) throw logger.error("Time value is not defined");

const priorityValue = calculateLabelValue(recognizedPriorityLabels.name);
if (!priorityValue) throw logger.error("Priority value is not defined");
if (priorityValue === null) throw logger.error("Priority value is not defined");

const taskPrice = calculateTaskPrice(context, timeValue, priorityValue);
return `Price: ${taskPrice} USD`;
}

export function calculateLabelValue(label: string): number {
/*
* Gets the value associated to the label. Returns null if the value of the label couldn't be extracted.
*/
export function calculateLabelValue(label: string): number | null {
const matches = label.match(/\d+/);
const number = matches && matches.length > 0 ? parseInt(matches[0]) || 0 : 0;
if (!matches?.length) return null;
const number = parseInt(matches[0]);
if (label.toLowerCase().includes("priority")) return number;
if (label.toLowerCase().includes("minute")) return number * 0.002;
if (label.toLowerCase().includes("hour")) return number * 0.125;
if (label.toLowerCase().includes("day")) return 1 + (number - 1) * 0.25;
if (label.toLowerCase().includes("week")) return number + 1;
if (label.toLowerCase().includes("month")) return 5 + (number - 1) * 8;
return 0;
return null;
}
6 changes: 4 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createClient } from "@supabase/supabase-js";
import { createPlugin } from "@ubiquity-os/plugin-sdk";
import { LogLevel } from "@ubiquity-os/ubiquity-os-logger";
import type { ExecutionContext } from "hono";
import { createAdapters } from "./adapters";
import { run } from "./run";
Expand All @@ -11,7 +12,7 @@ import { Command } from "./types/command";
import { Manifest } from "@ubiquity-os/plugin-sdk/manifest";

export default {
async fetch(request: Request, env: Env, executionCtx?: ExecutionContext) {
async fetch(request: Request, env: Record<string, string>, executionCtx?: ExecutionContext) {
return createPlugin<AssistivePricingSettings, Env, Command, SupportedEvents>(
(context) => {
return run({
Expand All @@ -24,8 +25,9 @@ export default {
envSchema: envSchema,
postCommentOnError: true,
settingsSchema: pluginSettingsSchema,
logLevel: env.LOG_LEVEL,
logLevel: (env.LOG_LEVEL as LogLevel) ?? "info",
kernelPublicKey: env.KERNEL_PUBLIC_KEY,
bypassSignatureVerification: env.NODE_ENV === "local",
}
).fetch(request, env, executionCtx);
},
Expand Down
2 changes: 1 addition & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe("User tests", () => {
},
];
for (const testCase of testCases) {
const price = calculateTaskPrice(context as unknown as Context, testCase.timeValue, testCase.priorityValue);
const price = calculateTaskPrice(context as unknown as Context, testCase.timeValue as number, testCase.priorityValue as number);
expect(price).toEqual(testCase.expectedPrice);
}
});
Expand Down
13 changes: 12 additions & 1 deletion tests/price-labels.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest } from "@jest/globals";
import { syncPriceLabelsToConfig } from "../src/handlers/sync-labels-to-config";
// import { listLabelsForRepo } from "../src/shared/label";
import { calculateLabelValue } from "../src/shared/pricing";
import { Context } from "../src/types/context";
import { COLLABORATOR_ONLY_DESCRIPTION } from "../src/types/constants";

Expand Down Expand Up @@ -148,4 +148,15 @@ describe("syncPriceLabelsToConfig function", () => {

expect(mockOctokit.rest.issues.updateLabel).not.toHaveBeenCalled();
}, 15000);

it("Should properly handled 0 priority label", () => {
let labelValue = calculateLabelValue("Priority: 0 (Regression)");
expect(labelValue).toEqual(0);
labelValue = calculateLabelValue("Priority: - (Regression)");
expect(labelValue).toEqual(null);
labelValue = calculateLabelValue("Time: 0 Hours");
expect(labelValue).toEqual(0);
labelValue = calculateLabelValue("Time: some Hours");
expect(labelValue).toEqual(null);
});
});

0 comments on commit 3b5a250

Please sign in to comment.