forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.ts
More file actions
88 lines (73 loc) · 2.73 KB
/
Copy pathevent.ts
File metadata and controls
88 lines (73 loc) · 2.73 KB
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
import { Context } from "probot";
import { createAdapters } from "../adapters";
import { processors, wildcardProcessors } from "../handlers/processors";
import { shouldSkip } from "../helpers";
import { BotConfig, Payload, PayloadSchema } from "../types";
import { Adapters } from "../types/adapters";
import { ajv } from "../utils";
import { loadConfig } from "./config";
let botContext: Context = {} as Context;
export const getBotContext = () => botContext;
let botConfig: BotConfig = {} as BotConfig;
export const getBotConfig = () => botConfig;
let adapters: Adapters = {} as Adapters;
export const getAdapters = () => adapters;
const allowedActions = ["labeled", "unlabeled", "assigned"];
export const bindEvents = async (context: Context): Promise<void> => {
const { log, id, name } = context;
botContext = context;
const payload = context.payload as Payload;
log.info(`Started binding events... id: ${id}, name: ${name}, action: ${payload.action}}`);
if (payload.action && !allowedActions.includes(payload.action)) {
log.debug(`Skipping the event. reason: not configured`);
return;
}
// Load config
log.info("Loading config from .env...");
botConfig = await loadConfig();
// Create adapters for telegram, supabase, twitter, discord, etc
log.info("Creating adapters for supabase, telegram, twitter, etc...");
adapters = createAdapters(botConfig);
// Validate payload
const validate = ajv.compile(PayloadSchema);
const valid = validate(payload);
if (!valid) {
log.info("Payload schema validation failed!!!", payload);
log.warn(validate.errors!);
return;
}
// Check if we should skip the event
const { skip, reason } = shouldSkip();
if (skip) {
log.info(`Skipping the event. reason: ${reason}`);
return;
}
const actionName = payload.action ?? name;
// Get the handlers for the action
const handlers = processors[actionName];
if (!handlers) {
log.warn(`No handler configured for action: ${actionName}`);
return;
}
const { pre, action, post } = handlers;
// Run pre-handlers
log.info(`Running pre handlers: ${pre.map((fn) => fn.name)}, action: ${actionName}`);
for (const preAction of pre) {
await preAction();
}
// Run main handlers
log.info(`Running main handlers: ${action.map((fn) => fn.name)}, action: ${actionName}`);
for (const mainAction of action) {
await mainAction();
}
// Run post-handlers
log.info(`Running post handlers: ${post.map((fn) => fn.name)}, action: ${actionName}`);
for (const postAction of post) {
await postAction();
}
// Run wildcard handlers
log.info(`Running wildcard handlers: ${wildcardProcessors.map((fn) => fn.name)}`);
for (const wildcardProcessor of wildcardProcessors) {
await wildcardProcessor();
}
};