forked from zulip/zulipbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
106 lines (94 loc) · 2.87 KB
/
Copy pathclient.ts
File metadata and controls
106 lines (94 loc) · 2.87 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import fs from "fs";
import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { Octokit } from "@octokit/rest";
import _ from "lodash";
import { assertDefined } from "ts-extras";
import type { Writable } from "type-fest";
import * as custom from "../config/config.ts";
import * as defaults from "../config/default.ts";
import commands, {
type CommandAliases,
type CommandPayload,
} from "./commands/index.ts";
import Template from "./structures/template.ts";
const MyOctokit: typeof Octokit &
(new (
...args: any[]
) => ReturnType<typeof retry> & ReturnType<typeof throttling>) =
Octokit.plugin(retry, throttling);
export class Client extends MyOctokit {
cfg: Writable<typeof defaults>;
commands: Map<
string,
{
run: (
this: Client,
payload: CommandPayload,
commenter: string,
args: string,
) => Promise<unknown>;
aliasPath: (commands: CommandAliases) => string[];
}
>;
invites: Map<string, number>;
templates: Map<string, Template>;
constructor() {
const cfg: Writable<typeof defaults> = _.merge({}, defaults, custom);
super({
auth: cfg.auth.oAuthToken,
retry: {
enabled: process.env["NODE_ENV"] !== "test",
retries: 1,
},
throttle: {
enabled: process.env["NODE_ENV"] !== "test",
onRateLimit: (retryAfter, { method, url }, _octokit, retryCount) => {
if (retryCount < 3) {
this.log.warn(
`Rate limit exceeded ${
retryCount + 1
} times for ${method} ${url}; retrying in ${retryAfter} seconds`,
);
return true;
}
this.log.warn(
`Rate limit exceeded ${
retryCount + 1
} times for ${method} ${url}; aborting`,
);
return; // eslint-disable-line no-useless-return
},
onSecondaryRateLimit: (_retryAfter, { method, url }) => {
this.log.warn(
`Secondary rate limit detected for ${method} ${url}; aborting`,
);
},
},
});
this.cfg = cfg;
this.commands = new Map();
this.invites = new Map();
this.templates = new Map();
for (const data of commands) {
const aliases = data.aliasPath(this.cfg.issues.commands);
for (const alias of aliases) {
this.commands.set(alias, data);
}
}
const templates = fs.readdirSync(
new URL("../config/templates", import.meta.url),
);
for (const file of templates) {
const [name] = file.split(".md");
assertDefined(name);
const content = fs.readFileSync(
new URL(`../config/templates/${file}`, import.meta.url),
"utf8",
);
const template = new Template(this, name, content);
this.templates.set(name, template);
}
}
}
export default new Client();