|
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 | | -} |
| 13 | +const { triggerAzurePipeline } = require('./trigger-azure-pipeline'); |
35 | 14 |
|
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 | | -} |
| 15 | +const { triggerWorkflowDispatch } = require('./trigger-workflow-dispatch') |
78 | 16 |
|
79 | 17 | module.exports = async (context, req) => { |
80 | 18 | try { |
@@ -119,6 +57,22 @@ module.exports = async (context, req) => { |
119 | 57 | context.res = { |
120 | 58 | body: `Ignored event type: ${eventType}`, |
121 | 59 | }; |
| 60 | + } else if (eventType === 'push') { |
| 61 | + if (req.body.repository.full_name !== 'git/git') { |
| 62 | + context.res = { body: `Ignoring pushes to ${req.body.repository.full_name}` } |
| 63 | + } else { |
| 64 | + const run = await triggerWorkflowDispatch( |
| 65 | + context, |
| 66 | + undefined, |
| 67 | + 'gitgitgadget', |
| 68 | + 'gitgitgadget-workflows', |
| 69 | + 'sync-ref.yml', |
| 70 | + 'main', { |
| 71 | + ref: req.body.ref |
| 72 | + } |
| 73 | + ) |
| 74 | + context.res = { body: `push(${req.body.ref}): triggered ${run.html_url}` } |
| 75 | + } |
122 | 76 | } else if (eventType === 'issue_comment') { |
123 | 77 | const triggerToken = process.env['GITGITGADGET_TRIGGER_TOKEN']; |
124 | 78 | if (!triggerToken) { |
|
0 commit comments