-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (63 loc) · 3.05 KB
/
app.js
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
import dotenv from 'dotenv'
import fs from 'fs'
import http from 'http'
import { Octokit, App } from 'octokit'
import { createNodeMiddleware } from '@octokit/webhooks'
import * as alertStatusAuthorization from './behaviors/alert-status-authorization.js'
import * as workflowFailures from './behaviors/workflow-failures.js'
// Load environment variables from .env file
dotenv.config();
// Set configured values
const appId = process.env.APP_ID;
const privateKeyPath = process.env.PRIVATE_KEY_PATH;
const privateKey = fs.readFileSync(privateKeyPath, 'utf8');
const secret = process.env.WEBHOOK_SECRET;
const enterpriseHostname = process.env.ENTERPRISE_HOSTNAME;
// Create an authenticated Octokit client authenticated as a GitHub App
const app = new App({
appId,
privateKey,
webhooks: {
secret
},
log: console,
...(enterpriseHostname && {
Octokit: Octokit.defaults({
baseUrl: `https://${enterpriseHostname}/api/v3`
})
})
});
// Verify the app can successfully authenticate
const { data } = await app.octokit.request('/app');
app.octokit.log.debug(`Authenticated as '${data.name}'`);
// Get an octokit instance authenticated with the organization containing the repository where issues will be created.
// To allow the sample to run in either an organization or in a user account, all installations are listed
// to determine the correct id. In an organization-only setting, calling getOrgInstallation is more efficient.
const installations = await app.octokit.paginate(app.octokit.rest.apps.listInstallations);
const issueAccountInstallation = installations.find(i => i.account.login === process.env.ISSUE_ORG);
if (!issueAccountInstallation) {
throw new Error(`A GitHub app installation with the login ${process.env.ISSUE_ORG} could not be found`);
}
const issueOctokit = await app.getInstallationOctokit(issueAccountInstallation.id);
// Register event handlers
app.webhooks.on('code_scanning_alert.closed_by_user', (event) => alertStatusAuthorization.codeScanningAlertClosedByUser({ ...event, issueOctokit }));
app.webhooks.on('dependabot_alert.dismissed', (event) => alertStatusAuthorization.dependabotAlertDismissed({ ...event, issueOctokit }));
app.webhooks.on('secret_scanning_alert.resolved', (event) => alertStatusAuthorization.secretScanningAlertResolved({ ...event, issueOctokit }));
app.webhooks.on('workflow_run.completed', (event) => workflowFailures.workflowRunCompleted({ ...event, issueOctokit }));
// Optional: Handle errors
app.webhooks.onError((error) => {
if (error.name === 'AggregateError') {
// Log Secret verification errors
console.log(`Error processing request: ${error.event}`)
} else {
console.log(error)
}
});
// Launch a web server to listen for GitHub webhooks
const port = process.env.PORT || 3000;
const path = '/api/webhook';
const middleware = createNodeMiddleware(app.webhooks, { path });
http.createServer(middleware).listen(port, () => {
console.log(`Server is listening for events at: http://localhost:${port}${path}`)
console.log('Press Ctrl + C to quit.')
});