Skip to content

Commit

Permalink
Merge pull request ubiquity-os-marketplace#50 from gentlementlegen/fi…
Browse files Browse the repository at this point in the history
…x/sdk

fix: SDK usage
  • Loading branch information
gentlementlegen authored Oct 28, 2024
2 parents 2be8b8e + 143d0f5 commit eb5b16e
Show file tree
Hide file tree
Showing 38 changed files with 265 additions and 347 deletions.
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"typebox",
"typeguards",
"mswjs",
"ubiquity-os"
"ubiquity-os",
"hono"
],
"dictionaries": ["typescript", "node", "software-terms"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
Expand Down
3 changes: 2 additions & 1 deletion .dev.vars.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
SUPABASE_URL=
SUPABASE_KEY=
SUPABASE_KEY=
KERNEL_PUBLIC_KEY=
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
SUPABASE_URL=
SUPABASE_KEY=
SUPABASE_KEY=
KERNEL_PUBLIC_KEY=
2 changes: 2 additions & 0 deletions .github/workflows/worker-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ jobs:
secrets: |
SUPABASE_URL
SUPABASE_KEY
KERNEL_PUBLIC_KEY
env:
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
KERNEL_PUBLIC_KEY: ${{ secrets.KERNEL_PUBLIC_KEY }}

- name: Write Deployment URL to Summary
run: |
Expand Down
12 changes: 12 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ module.exports = {
coverageReporters: ["json", "lcov", "text", "clover", "json-summary"],
reporters: ["default", "jest-junit", "jest-md-dashboard"],
coverageDirectory: "coverage",
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
useESM: true,
},
],
},
} as JestConfigWithTsJest;
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"engines": {
"node": ">=20.10.0"
},
"type": "module",
"scripts": {
"worker": "wrangler dev --env dev --port 4000",
"start": "tsx src/index.ts",
Expand All @@ -19,7 +20,7 @@
"knip": "knip --config .github/knip.ts",
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts",
"prepare": "husky install",
"test": "jest --setupFiles dotenv/config --coverage",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --setupFiles dotenv/config --coverage",
"supabase:generate:local": "supabase gen types typescript --local > src/adapters/supabase/types/database.ts",
"supabase:generate:remote": "dotenv -- cross-env-shell supabase gen types typescript --project-id $SUPABASE_PROJECT_ID --schema public > src/adapters/supabase/types/database.ts"
},
Expand All @@ -31,22 +32,19 @@
"open-source"
],
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@commander-js/extra-typings": "12.0.1",
"@octokit/plugin-retry": "^7.1.2",
"@octokit/plugin-throttling": "^9.3.2",
"@octokit/rest": "^20.0.2",
"@octokit/webhooks": "^13.1.0",
"@sinclair/typebox": "^0.32.15",
"@sinclair/typebox": "^0.33.17",
"@supabase/supabase-js": "2.43.1",
"@ubiquity-os/ubiquity-os-kernel": "^2.4.0",
"@ubiquity-os/ubiquity-os-kernel": "^2.5.1",
"@ubiquity-os/ubiquity-os-logger": "^1.3.2",
"commander": "12.0.0",
"decimal.js": "^10.4.3",
"dotenv": "^16.4.5",
"hono": "^4.6.5",
"typebox-validators": "0.3.5"
"hono": "^4.6.7"
},
"devDependencies": {
"@commitlint/cli": "^18.6.1",
Expand Down Expand Up @@ -78,7 +76,7 @@
"ts-node": "10.9.2",
"tsx": "^4.7.1",
"typescript": "5.6.2",
"wrangler": "3.79.0"
"wrangler": "^3.83.0"
},
"lint-staged": {
"*.ts": [
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Context } from "../types/context";
import { Context } from "@ubiquity-os/ubiquity-os-kernel";
import { Access } from "./supabase/helpers/tables/access";
import { User } from "./supabase/helpers/tables/user";
import { Label } from "./supabase/helpers/tables/label";
import { Super } from "./supabase/helpers/tables/super";
import { User } from "./supabase/helpers/tables/user";

export function createAdapters(supabaseClient: SupabaseClient, context: Context) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/supabase/helpers/tables/access.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Context } from "../../../../types/context";
import { Context } from "@ubiquity-os/ubiquity-os-kernel";
import { Database } from "../../types/database";
import { Super } from "./super";

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/supabase/helpers/tables/label.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Context } from "@ubiquity-os/ubiquity-os-kernel";

import { Database } from "../../types/database";
import { Super } from "./super";
import { Context } from "../../../../types/context";

type LabelRow = Database["public"]["Tables"]["labels"]["Row"];

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/supabase/helpers/tables/super.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Context } from "../../../../types/context";
import { Context } from "@ubiquity-os/ubiquity-os-kernel";
import { Database } from "../../types/database";

export class Super {
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/supabase/helpers/tables/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Context } from "@ubiquity-os/ubiquity-os-kernel";
import { Super } from "./super";
import { Context } from "../../../../types/context";

export class User extends Super {
constructor(supabase: SupabaseClient, context: Context) {
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/check-modified-base-rate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CONFIG_FULL_PATH, DEV_CONFIG_FULL_PATH } from "@ubiquity-os/ubiquity-os-kernel";
import { Context } from "../types/context";
import { ContextPlugin } from "../types/plugin-input";
import { isPushEvent } from "../types/typeguards";
import { getCommitChanges } from "./get-commit-changes";

export const ZERO_SHA = "0000000000000000000000000000000000000000";
const BASE_RATE_FILES = [DEV_CONFIG_FULL_PATH, CONFIG_FULL_PATH];

export async function isConfigModified(context: Context): Promise<boolean> {
export async function isConfigModified(context: ContextPlugin): Promise<boolean> {
if (!isPushEvent(context)) {
context.logger.debug("Not a push event");
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/comment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { addCommentToIssue, isUserAdminOrBillingManager } from "../shared/issue";
import { Context } from "../types/context";
import { ContextPlugin } from "../types/plugin-input";
import { isCommentEvent } from "../types/typeguards";
import commandParser, { AllowedCommand, CommandArguments, isValidCommand } from "./command-parser";

const commandHandlers: { [k in AllowedCommand]: (context: Context, commandArguments: CommandArguments) => Promise<void> } = {
const commandHandlers: { [k in AllowedCommand]: (context: ContextPlugin, commandArguments: CommandArguments) => Promise<void> } = {
async allow(context, { username, labels }: CommandArguments) {
const logger = context.logger;
if (!isCommentEvent(context)) {
Expand All @@ -25,7 +25,7 @@ const commandHandlers: { [k in AllowedCommand]: (context: Context, commandArgume
},
};

export async function handleComment(context: Context) {
export async function handleComment(context: ContextPlugin) {
const logger = context.logger;
if (!isCommentEvent(context)) {
return logger.debug("Not an comment event");
Expand Down
7 changes: 3 additions & 4 deletions src/handlers/get-base-rate-changes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Context } from "../types/context";
import { Rates } from "../types/plugin-input";
import { ContextPlugin, Rates } from "../types/plugin-input";
import { isPushEvent } from "../types/typeguards";

/**
* Parses the diff of changes to the org config file to find the old and new base rates.
*
* This will capture changes to either the plugin's config or the global basePriceMultiplier.
*/
export async function getBaseRateChanges(context: Context): Promise<Rates> {
export async function getBaseRateChanges(context: ContextPlugin): Promise<Rates> {
if (!isPushEvent(context)) {
context.logger.debug("Not a push event");
return {
Expand All @@ -32,7 +31,7 @@ export async function getBaseRateChanges(context: Context): Promise<Rates> {
}

try {
commitData = await context.octokit.repos.getCommit({
commitData = await context.octokit.rest.repos.getCommit({
owner,
repo: repository.name,
ref: commitSha,
Expand Down
20 changes: 10 additions & 10 deletions src/handlers/global-config-update.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { isUserAdminOrBillingManager, listOrgRepos, listRepoIssues } from "../shared/issue";
import { Label } from "../types/github";
import { ContextPlugin } from "../types/plugin-input";
import { isPushEvent } from "../types/typeguards";
import { isConfigModified } from "./check-modified-base-rate";
import { getBaseRateChanges } from "./get-base-rate-changes";
import { Context } from "../types/context";
import { syncPriceLabelsToConfig } from "./sync-labels-to-config";
import { setPriceLabel } from "./pricing-label";
import { isPushEvent } from "../types/typeguards";
import { isUserAdminOrBillingManager, listOrgRepos, listRepoIssues } from "../shared/issue";
import { Label } from "../types/github";
import { syncPriceLabelsToConfig } from "./sync-labels-to-config";

async function isAuthed(context: Context): Promise<boolean> {
async function isAuthed(context: ContextPlugin): Promise<boolean> {
if (!isPushEvent(context)) {
context.logger.debug("Not a push event");
return false;
Expand All @@ -33,7 +33,7 @@ async function isAuthed(context: Context): Promise<boolean> {
return !!(isPusherAuthed && isSenderAuthed);
}

export async function globalLabelUpdate(context: Context) {
export async function globalLabelUpdate(context: ContextPlugin) {
if (!isPushEvent(context)) {
context.logger.debug("Not a push event");
return;
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function globalLabelUpdate(context: Context) {
payload: {
repository: repo,
},
} as Context;
} as ContextPlugin;

// this should create labels on the repos that are missing
await syncPriceLabelsToConfig(ctx);
Expand All @@ -80,7 +80,7 @@ export async function globalLabelUpdate(context: Context) {
}
}

async function updateAllIssuePriceLabels(context: Context) {
async function updateAllIssuePriceLabels(context: ContextPlugin) {
const { logger, config } = context;
const repos = await listOrgRepos(context);

Expand All @@ -97,7 +97,7 @@ async function updateAllIssuePriceLabels(context: Context) {
repository: repo,
issue,
},
} as Context,
} as ContextPlugin,
issue.labels as Label[],
config
);
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/handle-parent-issue.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { clearAllPriceLabelsOnIssue } from "../shared/label";
import { calculateLabelValue } from "../shared/pricing";
import { Context } from "../types/context";
import { Label } from "../types/github";
import { ContextPlugin } from "../types/plugin-input";

export async function handleParentIssue(context: Context, labels: Label[]) {
export async function handleParentIssue(context: ContextPlugin, labels: Label[]) {
const issuePrices = labels.filter((label) => label.name.toString().startsWith("Price:"));
if (issuePrices.length) {
await clearAllPriceLabelsOnIssue(context);
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/label-change.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isUserAdminOrBillingManager } from "../shared/issue";
import { Context } from "../types/context";
import { ContextPlugin } from "../types/plugin-input";
import { isLabelEditedEvent } from "../types/typeguards";

export async function watchLabelChange(context: Context) {
export async function watchLabelChange(context: ContextPlugin) {
const logger = context.logger;
if (!isLabelEditedEvent(context)) {
logger.debug("Not a label event");
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function watchLabelChange(context: Context) {
return logger.debug("label name change saved to db");
}

async function hasLabelEditPermission(context: Context, label: string, caller: string) {
async function hasLabelEditPermission(context: ContextPlugin, label: string, caller: string) {
const sufficientPrivileges = await isUserAdminOrBillingManager(context, caller);

// get text before :
Expand Down
22 changes: 10 additions & 12 deletions src/handlers/pricing-label.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Context } from "../types/context";

import { addLabelToIssue, clearAllPriceLabelsOnIssue, createLabel, listLabelsForRepo, removeLabelFromIssue } from "../shared/label";
import { labelAccessPermissionsCheck } from "../shared/permissions";
import { Label, UserType } from "../types/github";
import { getPrice } from "../shared/pricing";
import { handleParentIssue, isParentIssue, sortLabelsByValue } from "./handle-parent-issue";
import { AssistivePricingSettings } from "../types/plugin-input";
import { AssistivePricingSettings, ContextPlugin } from "../types/plugin-input";
import { isIssueLabelEvent } from "../types/typeguards";

export async function onLabelChangeSetPricing(context: Context): Promise<void> {
export async function onLabelChangeSetPricing(context: ContextPlugin): Promise<void> {
if (!isIssueLabelEvent(context)) {
context.logger.debug("Not an issue event");
return;
Expand Down Expand Up @@ -50,7 +48,7 @@ export async function onLabelChangeSetPricing(context: Context): Promise<void> {
const smallestPriceLabelName = smallestPriceLabel?.name;
if (smallestPriceLabelName) {
for (const label of sortedPriceLabels) {
await context.octokit.issues.removeLabel({
await context.octokit.rest.issues.removeLabel({
owner,
repo: payload.repository.name,
issue_number: payload.issue.number,
Expand All @@ -65,7 +63,7 @@ export async function onLabelChangeSetPricing(context: Context): Promise<void> {
await setPriceLabel(context, labels, config);
}

export async function setPriceLabel(context: Context, issueLabels: Label[], config: AssistivePricingSettings) {
export async function setPriceLabel(context: ContextPlugin, issueLabels: Label[], config: AssistivePricingSettings) {
const logger = context.logger;
const labelNames = issueLabels.map((i) => i.name);

Expand Down Expand Up @@ -121,7 +119,7 @@ function getMinLabels(recognizedLabels: { time: Label[]; priority: Label[] }) {
return { time: minTimeLabel, priority: minPriorityLabel };
}

async function handleTargetPriceLabel(context: Context, targetPriceLabel: string, labelNames: string[]) {
async function handleTargetPriceLabel(context: ContextPlugin, targetPriceLabel: string, labelNames: string[]) {
const { repository } = context.payload;
if (repository.name === "devpool-directory") {
targetPriceLabel = targetPriceLabel.replace("Price: ", "Pricing: ");
Expand All @@ -139,7 +137,7 @@ async function handleTargetPriceLabel(context: Context, targetPriceLabel: string
}
}

async function handleExistingPriceLabel(context: Context, targetPriceLabel: string) {
async function handleExistingPriceLabel(context: ContextPlugin, targetPriceLabel: string) {
const logger = context.logger;
let labeledEvents = await getAllLabeledEvents(context);
if (!labeledEvents) return logger.error("No labeled events found");
Expand All @@ -154,25 +152,25 @@ async function handleExistingPriceLabel(context: Context, targetPriceLabel: stri
}
}

async function addPriceLabelToIssue(context: Context, targetPriceLabel: string) {
async function addPriceLabelToIssue(context: ContextPlugin, targetPriceLabel: string) {
await clearAllPriceLabelsOnIssue(context);
await addLabelToIssue(context, targetPriceLabel);
}

async function getAllLabeledEvents(context: Context) {
async function getAllLabeledEvents(context: ContextPlugin) {
const events = await getAllIssueEvents(context);
if (!events) return null;
return events.filter((event) => event.event === "labeled");
}

async function getAllIssueEvents(context: Context) {
async function getAllIssueEvents(context: ContextPlugin) {
if (!("issue" in context.payload) || !context.payload.issue) {
context.logger.debug("Not an issue event");
return;
}

try {
return await context.octokit.paginate(context.octokit.issues.listEvents, {
return await context.octokit.paginate(context.octokit.rest.issues.listEvents, {
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
Expand Down
15 changes: 0 additions & 15 deletions src/handlers/return-data-to-kernel.ts

This file was deleted.

Loading

0 comments on commit eb5b16e

Please sign in to comment.