-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.ts
146 lines (130 loc) · 4.33 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Octokit as OctokitCore } from "@octokit/core";
import { createAppAuth } from "@octokit/auth-app";
import { OAuthApp } from "@octokit/oauth-app";
import { Webhooks } from "@octokit/webhooks";
import {
Options,
ConstructorOptions,
EachInstallationInterface,
EachRepositoryInterface,
GetInstallationOctokitInterface,
} from "./types";
import { VERSION } from "./version";
import { webhooks } from "./webhooks";
import { eachInstallationFactory } from "./each-installation";
import { eachRepositoryFactory } from "./each-repository";
import { getInstallationOctokit } from "./get-installation-octokit";
type Constructor<T> = new (...args: any[]) => T;
type OctokitType<TOptions extends Options> =
TOptions["Octokit"] extends typeof OctokitCore
? InstanceType<TOptions["Octokit"]>
: OctokitCore;
type OctokitClassType<TOptions extends Options> =
TOptions["Octokit"] extends typeof OctokitCore
? TOptions["Octokit"]
: typeof OctokitCore;
export class App<TOptions extends Options = Options> {
static VERSION = VERSION;
static defaults<
TDefaults extends Options,
S extends Constructor<App<TDefaults>>
>(this: S, defaults: Partial<TDefaults>) {
const AppWithDefaults = class extends this {
constructor(...args: any[]) {
super({
...defaults,
...args[0],
});
}
};
return AppWithDefaults as typeof AppWithDefaults & typeof this;
}
octokit: OctokitType<TOptions>;
// @ts-ignore calling app.webhooks will throw a helpful error when options.webhooks is not set
webhooks: Webhooks<{ octokit: OctokitType<TOptions> }>;
// @ts-ignore calling app.oauth will throw a helpful error when options.oauth is not set
oauth: OAuthApp<{
clientType: "github-app";
Octokit: OctokitClassType<TOptions>;
}>;
getInstallationOctokit: GetInstallationOctokitInterface<
OctokitType<TOptions>
>;
eachInstallation: EachInstallationInterface<OctokitType<TOptions>>;
eachRepository: EachRepositoryInterface<OctokitType<TOptions>>;
log: {
debug: (message: string, additionalInfo?: object) => void;
info: (message: string, additionalInfo?: object) => void;
warn: (message: string, additionalInfo?: object) => void;
error: (message: string, additionalInfo?: object) => void;
[key: string]: unknown;
};
constructor(options: ConstructorOptions<TOptions>) {
const Octokit = (options.Octokit ||
OctokitCore) as OctokitClassType<TOptions>;
const authOptions = Object.assign(
{
appId: options.appId,
privateKey: options.privateKey,
},
options.oauth
? {
clientId: options.oauth.clientId,
clientSecret: options.oauth.clientSecret,
}
: {}
);
this.octokit = new Octokit({
authStrategy: createAppAuth,
auth: authOptions,
log: options.log,
}) as OctokitType<TOptions>;
this.log = Object.assign(
{
debug: () => {},
info: () => {},
warn: console.warn.bind(console),
error: console.error.bind(console),
},
options.log
);
// set app.webhooks depending on whether "webhooks" option has been passed
if (options.webhooks) {
// @ts-expect-error TODO: figure this out
this.webhooks = webhooks(this.octokit, options.webhooks);
} else {
Object.defineProperty(this, "webhooks", {
get() {
throw new Error("[@octokit/app] webhooks option not set");
},
});
}
// set app.oauth depending on whether "oauth" option has been passed
if (options.oauth) {
this.oauth = new OAuthApp({
...options.oauth,
clientType: "github-app",
Octokit,
});
} else {
Object.defineProperty(this, "oauth", {
get() {
throw new Error(
"[@octokit/app] oauth.clientId / oauth.clientSecret options are not set"
);
},
});
}
this.getInstallationOctokit = getInstallationOctokit.bind(
null,
this
) as GetInstallationOctokitInterface<OctokitType<TOptions>>;
this.eachInstallation = eachInstallationFactory(
this
) as EachInstallationInterface<OctokitType<TOptions>>;
this.eachRepository = eachRepositoryFactory(
this
) as EachRepositoryInterface<OctokitType<TOptions>>;
}
}
export { createNodeMiddleware } from "./middleware/node/index";