generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate-permits-from-context.ts
104 lines (93 loc) · 3.6 KB
/
generate-permits-from-context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as github from "@actions/github";
import { Octokit } from "@octokit/rest";
import { Value } from "@sinclair/typebox/value";
import { createClient } from "@supabase/supabase-js";
import { createAdapters } from "./adapters";
import { Database } from "./adapters/supabase/types/database";
import { generatePayoutPermit } from "./handlers";
import { registerWallet } from "./handlers/register-wallet";
import { Context } from "./types/context";
import { envSchema } from "./types/env";
import { permitGenerationSettingsSchema, PluginInputs } from "./types/plugin-input";
/**
* Generates all the permits based on the currently populated context.
*/
export async function generatePermitsFromContext() {
const webhookPayload = github.context.payload.inputs;
const env = Value.Decode(envSchema, process.env);
const settings = Value.Decode(permitGenerationSettingsSchema, JSON.parse(webhookPayload.settings));
const inputs: PluginInputs = {
stateId: webhookPayload.stateId,
eventName: webhookPayload.eventName,
eventPayload: JSON.parse(webhookPayload.eventPayload),
settings: settings,
authToken: webhookPayload.authToken,
ref: webhookPayload.ref,
};
const octokit = new Octokit({ auth: inputs.authToken });
const supabaseClient = createClient<Database>(env.SUPABASE_URL, env.SUPABASE_KEY);
const context: Context = {
eventName: inputs.eventName,
payload: inputs.eventPayload,
config: inputs.settings,
octokit,
env,
logger: {
debug(message: unknown, ...optionalParams: unknown[]) {
console.debug(message, ...optionalParams);
},
info(message: unknown, ...optionalParams: unknown[]) {
console.log(message, ...optionalParams);
},
warn(message: unknown, ...optionalParams: unknown[]) {
console.warn(message, ...optionalParams);
},
error(message: unknown, ...optionalParams: unknown[]) {
console.error(message, ...optionalParams);
},
fatal(message: unknown, ...optionalParams: unknown[]) {
console.error(message, ...optionalParams);
},
},
adapters: {} as ReturnType<typeof createAdapters>,
};
context.adapters = createAdapters(supabaseClient, context);
if (context.eventName === "issue_comment.created") {
await handleSlashCommands(context, octokit);
} else {
const permits = await generatePayoutPermit(context, settings.permitRequests);
await returnDataToKernel(env.GITHUB_TOKEN, inputs.stateId, permits);
return JSON.stringify(permits);
}
return null;
}
async function returnDataToKernel(repoToken: string, stateId: string, output: object) {
const octokit = new Octokit({ auth: repoToken });
await octokit.repos.createDispatchEvent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
event_type: "return_data_to_ubiquibot_kernel",
client_payload: {
state_id: stateId,
output: JSON.stringify(output),
},
});
}
async function handleSlashCommands(context: Context, octokit: Octokit) {
const payload = context.payload as Context<"issue_comment.created">["payload"];
const body = payload.comment.body;
const registrationRegex = /\/wallet (0x[a-fA-F0-9]{40})/g;
const matches = body.match(registrationRegex);
if (matches) {
const address = matches[0];
// try registering wallet, if it fails, notify the user
if (!(await registerWallet(context, address))) {
await octokit.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: payload.issue.number,
body: `Failed to register wallet: ${address}`,
});
}
}
}