|
8 | 8 | * via the "webHookType", starting with v2, we have to do the payload
|
9 | 9 | * validation "by hand".
|
10 | 10 | */
|
11 |
| -const crypto = require('crypto'); |
12 |
| -const https = require('https'); |
| 11 | +const { validateGitHubWebHook } = require('./validate-github-webhook'); |
13 | 12 |
|
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'); |
78 | 14 |
|
79 | 15 | module.exports = async (context, req) => {
|
80 | 16 | try {
|
|
0 commit comments