Skip to content

Commit 1142a2f

Browse files
committed
Refactor functions into their own modules
This makes it much easier to mock things in the future, and also structures the code better. This commit is best viewed with `--color-moved`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4bd5d1b commit 1142a2f

File tree

3 files changed

+77
-66
lines changed

3 files changed

+77
-66
lines changed

GitGitGadget/index.js

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,9 @@
88
* via the "webHookType", starting with v2, we have to do the payload
99
* validation "by hand".
1010
*/
11-
const crypto = require('crypto');
12-
const https = require('https');
11+
const { validateGitHubWebHook } = require('./validate-github-webhook');
1312

14-
const validateGitHubWebHook = (context) => {
15-
const secret = process.env['GITHUB_WEBHOOK_SECRET'];
16-
if (!secret) {
17-
throw new Error('Webhook secret not configured');
18-
}
19-
if (context.req.headers['content-type'] !== 'application/json') {
20-
throw new Error('Unexpected content type: ' + context.req.headers['content-type']);
21-
}
22-
const signature = context.req.headers['x-hub-signature-256'];
23-
if (!signature) {
24-
throw new Error('Missing X-Hub-Signature');
25-
}
26-
const sha256 = signature.match(/^sha256=(.*)/);
27-
if (!sha256) {
28-
throw new Error('Unexpected X-Hub-Signature format: ' + signature);
29-
}
30-
const computed = crypto.createHmac('sha256', secret).update(context.req.rawBody).digest('hex');
31-
if (sha256[1] !== computed) {
32-
throw new Error('Incorrect X-Hub-Signature');
33-
}
34-
}
35-
36-
const triggerAzurePipeline = async (token, organization, project, buildDefinitionId, sourceBranch, parameters) => {
37-
const auth = Buffer.from('PAT:' + token).toString('base64');
38-
const headers = {
39-
'Accept': 'application/json; api-version=5.0-preview.5; excludeUrls=true',
40-
'Authorization': 'Basic ' + auth,
41-
};
42-
const json = JSON.stringify({
43-
'definition': { 'id': buildDefinitionId },
44-
'sourceBranch': sourceBranch,
45-
'parameters': JSON.stringify(parameters),
46-
});
47-
headers['Content-Type'] = 'application/json';
48-
headers['Content-Length'] = Buffer.byteLength(json);
49-
50-
const requestOptions = {
51-
host: 'dev.azure.com',
52-
port: '443',
53-
path: `/${organization}/${project}/_apis/build/builds?ignoreWarnings=false&api-version=5.0-preview.5`,
54-
method: 'POST',
55-
headers: headers
56-
};
57-
58-
return new Promise((resolve, reject) => {
59-
const handleResponse = (res) => {
60-
res.setEncoding('utf8');
61-
var response = '';
62-
res.on('data', (chunk) => {
63-
response += chunk;
64-
});
65-
res.on('end', () => {
66-
resolve(JSON.parse(response));
67-
});
68-
res.on('error', (err) => {
69-
reject(err);
70-
})
71-
};
72-
73-
const request = https.request(requestOptions, handleResponse);
74-
request.write(json);
75-
request.end();
76-
});
77-
}
13+
const { triggerAzurePipeline } = require('./trigger-azure-pipeline');
7814

7915
module.exports = async (context, req) => {
8016
try {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const https = require('https');
2+
3+
const triggerAzurePipeline = async (token, organization, project, buildDefinitionId, sourceBranch, parameters) => {
4+
const auth = Buffer.from('PAT:' + token).toString('base64');
5+
const headers = {
6+
'Accept': 'application/json; api-version=5.0-preview.5; excludeUrls=true',
7+
'Authorization': 'Basic ' + auth,
8+
};
9+
const json = JSON.stringify({
10+
'definition': { 'id': buildDefinitionId },
11+
'sourceBranch': sourceBranch,
12+
'parameters': JSON.stringify(parameters),
13+
});
14+
headers['Content-Type'] = 'application/json';
15+
headers['Content-Length'] = Buffer.byteLength(json);
16+
17+
const requestOptions = {
18+
host: 'dev.azure.com',
19+
port: '443',
20+
path: `/${organization}/${project}/_apis/build/builds?ignoreWarnings=false&api-version=5.0-preview.5`,
21+
method: 'POST',
22+
headers: headers
23+
};
24+
25+
return new Promise((resolve, reject) => {
26+
const handleResponse = (res) => {
27+
res.setEncoding('utf8');
28+
var response = '';
29+
res.on('data', (chunk) => {
30+
response += chunk;
31+
});
32+
res.on('end', () => {
33+
resolve(JSON.parse(response));
34+
});
35+
res.on('error', (err) => {
36+
reject(err);
37+
})
38+
};
39+
40+
const request = https.request(requestOptions, handleResponse);
41+
request.write(json);
42+
request.end();
43+
});
44+
}
45+
46+
module.exports = {
47+
triggerAzurePipeline
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const crypto = require('crypto');
2+
3+
const validateGitHubWebHook = (context) => {
4+
const secret = process.env['GITHUB_WEBHOOK_SECRET'];
5+
if (!secret) {
6+
throw new Error('Webhook secret not configured');
7+
}
8+
if (context.req.headers['content-type'] !== 'application/json') {
9+
throw new Error('Unexpected content type: ' + context.req.headers['content-type']);
10+
}
11+
const signature = context.req.headers['x-hub-signature-256'];
12+
if (!signature) {
13+
throw new Error('Missing X-Hub-Signature');
14+
}
15+
const sha256 = signature.match(/^sha256=(.*)/);
16+
if (!sha256) {
17+
throw new Error('Unexpected X-Hub-Signature format: ' + signature);
18+
}
19+
const computed = crypto.createHmac('sha256', secret).update(context.req.rawBody).digest('hex');
20+
if (sha256[1] !== computed) {
21+
throw new Error('Incorrect X-Hub-Signature');
22+
}
23+
}
24+
25+
module.exports = {
26+
validateGitHubWebHook
27+
}

0 commit comments

Comments
 (0)