* Hits the GitHub repot contents API to determine if the commit that triggered * event supports the PRX CI system by looking for a buildspec.yml file * https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#contents * @param {GitHubPullRequestWebhookPayload|GitHubPushWebhookPayload}
(event)
| 240 | * @returns {Promise<string|false>} The JSON response |
| 241 | */ |
| 242 | function getBuildspecContentJson(event) { |
| 243 | return new Promise((resolve, reject) => { |
| 244 | console.log('Getting buildspec contents'); |
| 245 | |
| 246 | // Get request properties |
| 247 | const repo = event.repository.full_name; |
| 248 | const path = 'buildspec.yml'; |
| 249 | const ref = eventIsPullRequest(event) |
| 250 | ? event.pull_request.head.sha |
| 251 | : event.after; |
| 252 | |
| 253 | // Setup request options |
| 254 | const options = { |
| 255 | host: 'api.github.com', |
| 256 | path: `/repos/${repo}/contents/${path}?ref=${ref}`, |
| 257 | method: 'GET', |
| 258 | headers: GITHUB_HEADERS, |
| 259 | }; |
| 260 | |
| 261 | options.headers['Content-Length'] = Buffer.byteLength(''); |
| 262 | |
| 263 | // Request with response handler |
| 264 | console.log(`Calling contents API: ${options.path}`); |
| 265 | const req = request(options, (res) => { |
| 266 | res.setEncoding('utf8'); |
| 267 | |
| 268 | let json = ''; |
| 269 | res.on('data', (chunk) => { |
| 270 | console.log(chunk); |
| 271 | json += chunk; |
| 272 | }); |
| 273 | res.on('end', () => { |
| 274 | switch (res.statusCode) { |
| 275 | case 200: |
| 276 | console.log('Found CodeBuild support'); |
| 277 | resolve(json); |
| 278 | break; |
| 279 | case 404: |
| 280 | console.log('No CodeBuild support!'); |
| 281 | resolve(false); |
| 282 | break; |
| 283 | default: |
| 284 | console.error(`Request failed ${res.statusCode}!`); |
| 285 | console.error(json); |
| 286 | reject(new Error('Contents request failed!')); |
| 287 | } |
| 288 | }); |
| 289 | }); |
| 290 | |
| 291 | req.setTimeout(1000, () => { |
| 292 | console.log('========= request timed out'); |
| 293 | }); |
| 294 | |
| 295 | // Generic request error handling |
| 296 | req.on('error', (e) => reject(e)); |
| 297 | |
| 298 | req.write(''); |
| 299 | req.end(); |
no test coverage detected