-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
135 lines (129 loc) · 3.64 KB
/
index.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
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
/**
* Packages
*/
const express = require('express');
const expressApp = express();
const path = require('path');
const bodyParser = require('body-parser');
const {
verifyWebhookData
} = require('./middleware/verifyWebhooks');
/**
* Controllers
*/
const {
commitAndTitleValidator
} = require('./controllers/pullRequest');
const {
marketplaceEventHandlers,
getAccessToken
} = require('./controllers/marketplace');
const {
listForSuite
} = require('./controllers/checks');
/**
* Constants
*/
const {
configFileName,
messages,
events
} = require('./constants.js');
const publicDirectory = path.join(`${__dirname}`, 'public');
/**
* This is the main entrypoint to Probot app
* @param {import('probot').Application} app
*/
module.exports = async app => {
/**
* Created pull request event listener
*/
app.on(events.PULL_REQUEST_OPEN, async (context) => {
try {
const configuration = await context.config(configFileName);
await commitAndTitleValidator(app, context, configuration, false, true);
} catch (error) {
console.log('Error while performing operation in Event PULL_REQUEST_OPEN', error);
app.log(error);
}
});
/**
* Check re-run event listener
*/
app.on(events.CHECK_RUN_REREQUESTED, async (context) => {
try {
const configuration = await context.config(configFileName);
await commitAndTitleValidator(app, context, configuration, true, false);
} catch (error) {
console.log('Error while performing operation in Event CHECK_RUN_REREQUESTED', error);
app.log(error);
}
});
/**
* Re-run all checks (Check Suite Re-requested) event listener
*/
app.on(events.CHECK_SUITE_REREQUESTED, async (context) => {
try {
const listOfCheckRuns = await listForSuite(app, context);
context.payload.check_run = {
id: listOfCheckRuns.data.check_runs[0].id
};
const configuration = await context.config(configFileName);
await commitAndTitleValidator(app, context, configuration, true, false);
} catch (error) {
console.log('Error while performing operation in Event CHECK_SUITE_REREQUESTED', error);
app.log(error);
}
});
/**
* Run all checks (Check Suite Requested) event listener
*/
app.on(events.CHECK_SUITE_REQUESTED, async (context) => {
try {
const configuration = await context.config(configFileName);
await commitAndTitleValidator(app, context, configuration, false, true);
} catch (error) {
console.log('Error while performing operation in Event CHECK_SUITE_REQUESTED', error);
app.log(error);
}
});
app.on(events.CHECK_SUITE, async context => {
try {
const configuration = await context.config(configFileName);
await commitAndTitleValidator(app, context, configuration, false, true);
} catch (error) {
console.log('Error while performing operation in Event CHECK_SUITE', error);
app.log(error);
}
});
/**
* Middlewares
*/
expressApp.use(bodyParser.json());
/**
* Web APIs
*/
/**
* Home page API
*/
expressApp.get('/', (req, res) => {
res.send(messages.home_page_message);
});
/**
* Privacy page API
*/
expressApp.get('/privacy', (req, res) => {
res.sendFile(path.join(`${publicDirectory}`, 'privacy.html'));
});
/**
* Marketplace API
* This API gets hit by github on any marketplace event
*/
expressApp.post('/marketplace', verifyWebhookData, marketplaceEventHandlers);
/**
* This API gets hit when redirected by `POST: /marketplace` API
* This API exchanges code with github for accessToken
*/
expressApp.get('/auth', getAccessToken);
app.router.use('/', expressApp);
};