Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot config #38

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ubiquibot-config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
priceMultiplier: 1.5
basePriceMultiplier: 1.5
newContributorGreeting:
enabled: true
header: "Thank you for contributing to UbiquiBot! Please be sure to set your wallet address before completing your first task so that the automatic payout upon task completion will work for you."
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
"@octokit/rest": "^20.0.2",
"@openzeppelin/contracts": "^5.0.0",
"@probot/adapter-github-actions": "^3.1.3",
"@sinclair/typebox": "^0.31.5",
"@sinclair/typebox": "^0.31.22",
"@supabase/supabase-js": "^2.4.0",
"@types/ms": "^0.7.31",
"@types/parse5": "^7.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"@uniswap/permit2-sdk": "^1.2.0",
"@vercel/ncc": "^0.34.0",
"ajv": "^8.11.2",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"axios": "^1.3.2",
"cspell": "^7.0.0",
Expand All @@ -63,7 +63,8 @@
"prettier": "^2.7.1",
"probot": "^12.2.4",
"tsx": "^3.12.7",
"yaml": "^2.2.2"
"yaml": "^2.2.2",
"zod": "^3.22.4"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you said you had to remove zod

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my bad

},
"devDependencies": {
"@types/dotenv": "^8.2.0",
Expand Down
31 changes: 14 additions & 17 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createClient } from "@supabase/supabase-js";
import { Context } from "../types";
import { Context as ProbotContext } from "probot";
import { Access } from "./supabase/helpers/tables/access";
import { Label } from "./supabase/helpers/tables/label";
import { Locations } from "./supabase/helpers/tables/locations";
Expand All @@ -9,25 +9,22 @@ import { Super } from "./supabase/helpers/tables/super";
import { User } from "./supabase/helpers/tables/user";
import { Wallet } from "./supabase/helpers/tables/wallet";
import { Database } from "./supabase/types";
import { env } from "../bindings/env";

export function createAdapters(context: Context) {
const client = generateSupabase(context.config.supabase.url, context.config.supabase.key);
const supabaseClient = createClient<Database>(env.SUPABASE_URL, env.SUPABASE_KEY, { auth: { persistSession: false } });

export function createAdapters(context: ProbotContext) {
return {
supabase: {
access: new Access(client, context),
wallet: new Wallet(client, context),
user: new User(client, context),
debit: new Settlement(client, context),
settlement: new Settlement(client, context),
label: new Label(client, context),
logs: new Logs(client, context),
locations: new Locations(client, context),
super: new Super(client, context),
access: new Access(supabaseClient, context),
wallet: new Wallet(supabaseClient, context),
user: new User(supabaseClient, context),
debit: new Settlement(supabaseClient, context),
settlement: new Settlement(supabaseClient, context),
label: new Label(supabaseClient, context),
logs: new Logs(supabaseClient, context, env.LOG_ENVIRONMENT, env.LOG_RETRY_LIMIT, env.LOG_LEVEL),
locations: new Locations(supabaseClient, context),
super: new Super(supabaseClient, context),
},
};
}

function generateSupabase(url: string | null, key: string | null) {
if (!url || !key) throw new Error("Supabase url or key is not defined");
return createClient<Database>(url, key, { auth: { persistSession: false } });
}
4 changes: 2 additions & 2 deletions src/adapters/supabase/helpers/tables/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Database } from "../../types/database";
import { GitHubNode } from "../client";
import { Super } from "./super";
import { UserRow } from "./user";
import { Context } from "../../../../types";
import { Context as ProbotContext } from "probot";
type AccessRow = Database["public"]["Tables"]["access"]["Row"];
type AccessInsert = Database["public"]["Tables"]["access"]["Insert"];
type UserWithAccess = (UserRow & { access: AccessRow | null })[];
Expand All @@ -19,7 +19,7 @@ type _Access = {
};

export class Access extends Super {
constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: ProbotContext) {
super(supabase, context);
}

Expand Down
4 changes: 2 additions & 2 deletions src/adapters/supabase/helpers/tables/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { SupabaseClient } from "@supabase/supabase-js";
import { Repository } from "../../../../types/payload";
import { Database } from "../../types";
import { Super } from "./super";
import { Context } from "../../../../types";
import { Context as ProbotContext } from "probot";
import Runtime from "../../../../bindings/bot-runtime";

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

export class Label extends Super {
constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: ProbotContext) {
super(supabase, context);
}

Expand Down
6 changes: 3 additions & 3 deletions src/adapters/supabase/helpers/tables/locations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Super } from "./super";
import { Database } from "../../types/database";
import { Context } from "../../../../types";
import { Context as ProbotContext } from "probot";

// currently trying to save all of the location metadata of the event.
// seems that focusing on the IssueComments will provide the most value
Expand All @@ -17,7 +17,7 @@ export class Locations extends Super {
node_id: string | undefined;
node_type: string | undefined;

constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: ProbotContext) {
super(supabase, context);
}

Expand Down Expand Up @@ -62,7 +62,7 @@ export class Locations extends Super {
}
`;

this.locationResponse = (await this.context.event.octokit.graphql(graphQlQuery)) as LocationResponse;
this.locationResponse = (await this.context.octokit.graphql(graphQlQuery)) as LocationResponse;
console.trace(this.locationResponse);

this.user_id = this.locationResponse.data.node.author.id;
Expand Down
36 changes: 16 additions & 20 deletions src/adapters/supabase/helpers/tables/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { Database } from "../../types";
import { prettyLogs } from "../pretty-logs";
import { Super } from "./super";
import { execSync } from "child_process";
import { Context } from "../../../../types";
import { LogLevel } from "../../../../types/logs";
import { Context as ProbotContext } from "probot";
import Runtime from "../../../../bindings/bot-runtime";

type LogFunction = (message: string, metadata?: any) => void;
Expand Down Expand Up @@ -222,13 +223,18 @@ export class Logs extends Super {
});
}

constructor(supabase: SupabaseClient, context: Context) {
constructor(
supabase: SupabaseClient,
context: ProbotContext,
environment: string,
retryLimit: number,
logLevel: LogLevel
) {
super(supabase, context);
const logConfig = this.context.config.log;

this.environment = logConfig.logEnvironment;
this.retryLimit = logConfig.retryLimit;
this.maxLevel = this._getNumericLevel(logConfig.level ?? LogLevel.DEBUG);
this.environment = environment;
this.retryLimit = retryLimit;
this.maxLevel = this._getNumericLevel(logLevel);
}

private async _sendLogsToSupabase(log: LogInsert) {
Expand Down Expand Up @@ -363,11 +369,11 @@ export class Logs extends Super {
}

private _postComment(message: string) {
this.context.event.octokit.issues
this.context.octokit.issues
.createComment({
owner: this.context.event.issue().owner,
repo: this.context.event.issue().repo,
issue_number: this.context.event.issue().issue_number,
owner: this.context.issue().owner,
repo: this.context.issue().repo,
issue_number: this.context.issue().issue_number,
body: message,
})
// .then((x) => console.trace(x))
Expand Down Expand Up @@ -411,13 +417,3 @@ export class Logs extends Super {
return obj;
}
}

export enum LogLevel {
ERROR = "error",
WARN = "warn",
INFO = "info",
HTTP = "http",
VERBOSE = "verbose",
DEBUG = "debug",
SILLY = "silly",
}
4 changes: 2 additions & 2 deletions src/adapters/supabase/helpers/tables/settlement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Comment, Payload } from "../../../../types/payload";
import { Database } from "../../types/database";
import { Super } from "./super";
import { Context } from "../../../../types";
import { Context as ProbotContext } from "probot";

type DebitInsert = Database["public"]["Tables"]["debits"]["Insert"];
type CreditInsert = Database["public"]["Tables"]["credits"]["Insert"];
Expand All @@ -26,7 +26,7 @@
};

export class Settlement extends Super {
constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: ProbotContext) {
super(supabase, context);
}

Expand Down Expand Up @@ -112,11 +112,11 @@
if (!networkId) throw new Error("Network ID not provided");

const permitData: PermitInsert = {
amount: transactionData.permit.permitted.amount,

Check failure on line 115 in src/adapters/supabase/helpers/tables/settlement.ts

View workflow job for this annotation

GitHub Actions / build

Property 'permit' does not exist on type '{}'.
nonce: transactionData.permit.nonce,

Check failure on line 116 in src/adapters/supabase/helpers/tables/settlement.ts

View workflow job for this annotation

GitHub Actions / build

Property 'permit' does not exist on type '{}'.
deadline: transactionData.permit.deadline,

Check failure on line 117 in src/adapters/supabase/helpers/tables/settlement.ts

View workflow job for this annotation

GitHub Actions / build

Property 'permit' does not exist on type '{}'.
signature: transactionData.signature,

Check failure on line 118 in src/adapters/supabase/helpers/tables/settlement.ts

View workflow job for this annotation

GitHub Actions / build

Property 'signature' does not exist on type '{}'.
token_id: await this._lookupTokenId(networkId, transactionData.permit.permitted.token),

Check failure on line 119 in src/adapters/supabase/helpers/tables/settlement.ts

View workflow job for this annotation

GitHub Actions / build

Property 'permit' does not exist on type '{}'.
partner_id: organization.id,
beneficiary_id: userId,
};
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/supabase/helpers/tables/super.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { SupabaseClient } from "@supabase/supabase-js";
import Runtime from "../../../../bindings/bot-runtime";
import { Context } from "../../../../types";
import { Context as ProbotContext } from "probot";

export class Super {
protected supabase: SupabaseClient;
protected runtime: Runtime; // convenience accessor
protected context: Context;
protected context: ProbotContext;

constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: ProbotContext) {
this.supabase = supabase;
this.runtime = Runtime.getState();
this.context = context;
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/supabase/helpers/tables/user.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Database } from "../../types/database";
import { Super } from "./super";
import { Context } from "../../../../types";
import { Context as ProbotContext } from "probot";

export type UserRow = Database["public"]["Tables"]["users"]["Row"];
export class User extends Super {
constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: ProbotContext) {
super(supabase, context);
}

public async getUserId(username: string): Promise<number> {
const octokit = this.context.event.octokit;
const octokit = this.context.octokit;
const { data } = await octokit.rest.users.getByUsername({ username });
return data.id;
}
Expand Down
7 changes: 2 additions & 5 deletions src/adapters/supabase/helpers/tables/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ dotenv.config();

import { Context as ProbotContext } from "probot";
import { createAdapters } from "../../..";
import { loadConfig } from "../../../../bindings/config";
import { Context, User } from "../../../../types";
import { User } from "../../../../types";
const SUPABASE_URL = process.env.SUPABASE_URL;
if (!SUPABASE_URL) throw new Error("SUPABASE_URL is not defined");
const SUPABASE_KEY = process.env.SUPABASE_KEY;
Expand All @@ -13,9 +12,7 @@ if (!SUPABASE_KEY) throw new Error("SUPABASE_KEY is not defined");
const mockContext = { supabase: { url: SUPABASE_URL, key: SUPABASE_KEY } } as unknown as ProbotContext;

async function getWalletAddressAndUrlTest(eventContext: ProbotContext) {
const botConfig = await loadConfig(eventContext);
const context: Context = { event: eventContext, config: botConfig };
const { wallet } = createAdapters(context).supabase;
const { wallet } = createAdapters(eventContext).supabase;
const userId = 4975670 as User["id"];
const results = [] as unknown[];
try {
Expand Down
8 changes: 4 additions & 4 deletions src/adapters/supabase/helpers/tables/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PostgrestError, SupabaseClient } from "@supabase/supabase-js";
import { Context as ProbotContext } from "probot/lib/context";
import { Context as ProbotContext } from "probot";
import Runtime from "../../../../bindings/bot-runtime";
import { Context, User } from "../../../../types";
import { User } from "../../../../types";
import { Database } from "../../types/database";
import { Super } from "./super";
import { UserRow } from "./user";
Expand All @@ -16,7 +16,7 @@ type IssueCommentPayload =
| ProbotContext<"issue_comment.edited">["payload"];

export class Wallet extends Super {
constructor(supabase: SupabaseClient, context: Context) {
constructor(supabase: SupabaseClient, context: ProbotContext) {
super(supabase, context);
}

Expand All @@ -26,7 +26,7 @@ export class Wallet extends Super {
}

public async upsertWalletAddress(address: string) {
const payload = this.context.event.payload as
const payload = this.context.payload as
| ProbotContext<"issue_comment.created">["payload"]
| ProbotContext<"issue_comment.edited">["payload"];

Expand Down
Loading
Loading