-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathindex.js
More file actions
341 lines (332 loc) · 9.46 KB
/
index.js
File metadata and controls
341 lines (332 loc) · 9.46 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// pkg/dist-src/index.js
import { Octokit as OctokitCore } from "@octokit/core";
import { createAppAuth as createAppAuth3 } from "@octokit/auth-app";
import { OAuthApp } from "@octokit/oauth-app";
// pkg/dist-src/version.js
var VERSION = "16.0.1";
// pkg/dist-src/webhooks.js
import { createAppAuth } from "@octokit/auth-app";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";
import { Webhooks } from "@octokit/webhooks";
function webhooks(appOctokit, options) {
return new Webhooks({
secret: options.secret,
transform: async (event) => {
if (!("installation" in event.payload) || typeof event.payload.installation !== "object") {
const octokit2 = new appOctokit.constructor({
authStrategy: createUnauthenticatedAuth,
auth: {
reason: `"installation" key missing in webhook event payload`
}
});
return {
...event,
octokit: octokit2
};
}
const installationId = event.payload.installation.id;
const octokit = await appOctokit.auth({
type: "installation",
installationId,
factory(auth) {
return new auth.octokit.constructor({
...auth.octokitOptions,
authStrategy: createAppAuth,
...{
auth: {
...auth,
installationId
}
}
});
}
});
octokit.hook.before("request", (options2) => {
options2.headers["x-github-delivery"] = event.id;
});
return {
...event,
octokit
};
}
});
}
// pkg/dist-src/each-installation.js
import { composePaginateRest } from "@octokit/plugin-paginate-rest";
// pkg/dist-src/get-installation-octokit.js
import { createAppAuth as createAppAuth2 } from "@octokit/auth-app";
async function getInstallationOctokit(app, installationId) {
return app.octokit.auth({
type: "installation",
installationId,
factory(auth) {
const options = {
...auth.octokitOptions,
authStrategy: createAppAuth2,
...{ auth: { ...auth, installationId } }
};
return new auth.octokit.constructor(options);
}
});
}
// pkg/dist-src/each-installation.js
function eachInstallationFactory(app) {
return Object.assign(eachInstallation.bind(null, app), {
iterator: eachInstallationIterator.bind(null, app)
});
}
async function eachInstallation(app, callback) {
const i = eachInstallationIterator(app)[Symbol.asyncIterator]();
let result = await i.next();
while (!result.done) {
await callback(result.value);
result = await i.next();
}
}
function eachInstallationIterator(app) {
return {
async *[Symbol.asyncIterator]() {
const iterator = composePaginateRest.iterator(
app.octokit,
"GET /app/installations"
);
for await (const { data: installations } of iterator) {
for (const installation of installations) {
const installationOctokit = await getInstallationOctokit(
app,
installation.id
);
yield { octokit: installationOctokit, installation };
}
}
}
};
}
// pkg/dist-src/each-repository.js
import { composePaginateRest as composePaginateRest2 } from "@octokit/plugin-paginate-rest";
function eachRepositoryFactory(app) {
return Object.assign(eachRepository.bind(null, app), {
iterator: eachRepositoryIterator.bind(null, app)
});
}
async function eachRepository(app, queryOrCallback, callback) {
const i = eachRepositoryIterator(
app,
callback ? queryOrCallback : void 0
)[Symbol.asyncIterator]();
let result = await i.next();
while (!result.done) {
if (callback) {
await callback(result.value);
} else {
await queryOrCallback(result.value);
}
result = await i.next();
}
}
function singleInstallationIterator(app, installationId) {
return {
async *[Symbol.asyncIterator]() {
yield {
octokit: await app.getInstallationOctokit(installationId)
};
}
};
}
function eachRepositoryIterator(app, query) {
return {
async *[Symbol.asyncIterator]() {
const iterator = query ? singleInstallationIterator(app, query.installationId) : app.eachInstallation.iterator();
for await (const { octokit } of iterator) {
const repositoriesIterator = composePaginateRest2.iterator(
octokit,
"GET /installation/repositories"
);
for await (const { data: repositories } of repositoriesIterator) {
for (const repository of repositories) {
yield { octokit, repository };
}
}
}
}
};
}
// pkg/dist-src/get-installation-url.js
function getInstallationUrlFactory(app) {
let installationUrlBasePromise;
return async function getInstallationUrl(options = {}) {
if (!installationUrlBasePromise) {
installationUrlBasePromise = getInstallationUrlBase(app);
}
const installationUrlBase = await installationUrlBasePromise;
const installationUrl = new URL(installationUrlBase);
if (options.target_id !== void 0) {
installationUrl.pathname += "/permissions";
installationUrl.searchParams.append(
"target_id",
options.target_id.toFixed()
);
}
if (options.state !== void 0) {
installationUrl.searchParams.append("state", options.state);
}
return installationUrl.href;
};
}
async function getInstallationUrlBase(app) {
const { data: appInfo } = await app.octokit.request("GET /app");
if (!appInfo) {
throw new Error("[@octokit/app] unable to fetch metadata for app");
}
return `${appInfo.html_url}/installations/new`;
}
// pkg/dist-src/middleware/node/index.js
import {
createNodeMiddleware as oauthNodeMiddleware,
sendNodeResponse,
unknownRouteResponse
} from "@octokit/oauth-app";
import { createNodeMiddleware as webhooksNodeMiddleware } from "@octokit/webhooks";
function noop() {
}
function createNodeMiddleware(app, options = {}) {
const log = Object.assign(
{
debug: noop,
info: noop,
warn: console.warn.bind(console),
error: console.error.bind(console)
},
options.log
);
const optionsWithDefaults = {
pathPrefix: "/api/github",
...options,
log
};
const webhooksMiddleware = webhooksNodeMiddleware(app.webhooks, {
path: optionsWithDefaults.pathPrefix + "/webhooks",
log
});
const oauthMiddleware = oauthNodeMiddleware(app.oauth, {
pathPrefix: optionsWithDefaults.pathPrefix + "/oauth"
});
return middleware.bind(
null,
optionsWithDefaults.pathPrefix,
webhooksMiddleware,
oauthMiddleware
);
}
async function middleware(pathPrefix, webhooksMiddleware, oauthMiddleware, request, response, next) {
const { pathname } = new URL(request.url, "http://localhost");
if (pathname.startsWith(`${pathPrefix}/`)) {
if (pathname === `${pathPrefix}/webhooks`) {
webhooksMiddleware(request, response);
} else if (pathname.startsWith(`${pathPrefix}/oauth/`)) {
oauthMiddleware(request, response);
} else {
sendNodeResponse(unknownRouteResponse(request), response);
}
return true;
} else {
next?.();
return false;
}
}
// pkg/dist-src/index.js
var App = class {
static VERSION = VERSION;
static defaults(defaults) {
const AppWithDefaults = class extends this {
constructor(...args) {
super({
...defaults,
...args[0]
});
}
};
return AppWithDefaults;
}
octokit;
// @ts-ignore calling app.webhooks will throw a helpful error when options.webhooks is not set
webhooks;
// @ts-ignore calling app.oauth will throw a helpful error when options.oauth is not set
oauth;
getInstallationOctokit;
eachInstallation;
eachRepository;
getInstallationUrl;
log;
constructor(options) {
const Octokit = options.Octokit || OctokitCore;
const authOptions = Object.assign(
{
appId: options.appId,
privateKey: options.privateKey
},
options.oauth ? {
clientId: options.oauth.clientId,
clientSecret: options.oauth.clientSecret
} : {}
);
const octokitOptions = {
authStrategy: createAppAuth3,
auth: authOptions
};
if ("log" in options && typeof options.log !== "undefined") {
octokitOptions.log = options.log;
}
this.octokit = new Octokit(octokitOptions);
this.log = Object.assign(
{
debug: () => {
},
info: () => {
},
warn: console.warn.bind(console),
error: console.error.bind(console)
},
options.log
);
if (options.webhooks) {
this.webhooks = webhooks(this.octokit, options.webhooks);
} else {
Object.defineProperty(this, "webhooks", {
get() {
throw new Error("[@octokit/app] webhooks option not set");
}
});
}
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
);
this.eachInstallation = eachInstallationFactory(
this
);
this.eachRepository = eachRepositoryFactory(
this
);
this.getInstallationUrl = getInstallationUrlFactory(this);
}
};
export {
App,
createNodeMiddleware
};