-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
webhook.js
159 lines (125 loc) · 3.33 KB
/
webhook.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const {createProbot} = require('probot');
const {resolve} = require('probot/lib/resolver');
const {findPrivateKey} = require('probot/lib/private-key');
const {GraphQLClient} = require('graphql-request');
const githubApp = require('@graphql-inspector/github').default;
let probot;
const loadProbot = appFn => {
probot =
probot ||
createProbot({
id: process.env.APP_ID,
secret: process.env.WEBHOOK_SECRET,
cert: findPrivateKey(),
});
if (typeof appFn === 'string') {
appFn = resolve(appFn);
}
probot.load(appFn);
return probot;
};
const lowerCaseKeys = obj =>
Object.keys(obj).reduce(
(accumulator, key) =>
Object.assign(accumulator, {[key.toLocaleLowerCase()]: obj[key]}),
{},
);
module.exports = serverless(githubApp);
function serverless(appFn) {
console.log('Created');
return async (req, res) => {
console.log('Invoked');
// A friendly homepage if there isn't a payload
if (req.method === 'GET') {
res.setHeader('Content-Type', 'text/plain');
res.status(200);
res.send(`Visit graphql-inspector.com`);
return;
}
// Otherwise let's listen handle the payload
probot = probot || loadProbot(appFn);
// Determine incoming webhook event type
const headers = lowerCaseKeys(req.headers);
const e = headers['x-github-event'];
const event = `${e}${req.body.action ? '.' + req.body.action : ''}`;
req.body = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
// Do the thing
console.log(`Received event ${event}`);
if (req) {
try {
await probot.receive({
name: e,
payload: req.body,
});
await logEvent({
event,
ok: true,
});
res.status(200);
res.json({
message: `Received ${e}.${req.body.action}`,
});
return;
} catch (err) {
console.error(err);
await logEvent({
event,
ok: false,
error: err,
});
res.status(500);
res.json(err);
return;
}
} else {
console.error({req, res});
await logEvent({
event,
ok: false,
error: 'No req object...',
});
res.status(500);
res.send('unknown error');
return;
}
};
}
async function logEvent({event, ok, error}) {
const secret = process.env.FAUNADB_SECRET;
if (!secret) {
return;
}
const status = ok === true ? 'success' : 'failure';
console.log('log', {event, status});
try {
const graphQLClient = new GraphQLClient(
'https://graphql.fauna.com/graphql',
{
headers: {
Authorization: `Basic ${secret}`,
},
},
);
const query = /* GraphQL */ `
mutation invocation($event: String!, $status: Status!, $error: String) {
createInvocation(
data: {event: $event, status: $status, error: $error}
) {
_id
_ts
}
}
`;
const variables = {event, status};
if (error) {
variables.error =
typeof error.toString !== 'undefined' ? error.toString() : `${error}`;
}
const result = await graphQLClient.request(query, variables);
console.log('LOG SENT');
console.log(result);
} catch (e) {
console.log('FAILED TO SEND LOG');
console.error(e);
}
}