|
| 1 | +const https = require('https'); |
| 2 | +const { existsSync } = require('fs'); |
| 3 | +const { spawn } = require('child_process'); |
| 4 | + |
| 5 | +// GitHub JavaScript Actions require we "must include any package dependencies |
| 6 | +// required to run the JavaScript code" - import node_modules in version control |
| 7 | +// or other weird things. |
| 8 | +// (https://help.github.com/en/articles/creating-a-javascript-action#commit-and-push-your-action-to-github). |
| 9 | +// To overcome that, we do `npm install` dynamically from within this script 🎉. |
| 10 | +let core; // this will become lazily imported '@actions/core' |
| 11 | + |
| 12 | +main().catch(err => { |
| 13 | + core && core.setFailed(err); |
| 14 | + process.exit(1); |
| 15 | +}); |
| 16 | + |
| 17 | +async function main() { |
| 18 | + await install(['@actions/core']); |
| 19 | + core = require('@actions/core'); |
| 20 | + |
| 21 | + await core.group('Install dependencies', installDependencies); |
| 22 | + await core.group('Validate spec', validate); |
| 23 | + await core.group('Publish to /TR/', publish); |
| 24 | +} |
| 25 | + |
| 26 | +async function installDependencies() { |
| 27 | + await install(['respec', 'respec-validator']); |
| 28 | +} |
| 29 | + |
| 30 | +async function validate() { |
| 31 | + const file = 'index.html'; |
| 32 | + |
| 33 | + if (!existsSync(file)) { |
| 34 | + throw `❌ ${file} not found!`; |
| 35 | + } |
| 36 | + |
| 37 | + const ghUser = core.getInput('GH_USER'); |
| 38 | + const ghToken = core.getInput('GH_USER'); |
| 39 | + |
| 40 | + const validator = './node_modules/.bin/respec-validator'; |
| 41 | + |
| 42 | + if (!ghUser || !ghToken) { |
| 43 | + await shell(validator, [file]); |
| 44 | + } else { |
| 45 | + await shell(validator, [ |
| 46 | + `--gh-user=${ghUser}`, |
| 47 | + `--gh-token=${ghToken}`, |
| 48 | + file |
| 49 | + ]); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +async function publish() { |
| 54 | + // PUBLISH could be 'false' or '0' or 0 or something like that... sanity check |
| 55 | + const shouldPublish = JSON.parse(`${core.getInput('PUBLISH')}`); |
| 56 | + if (!shouldPublish) { |
| 57 | + console.log('👻 Skipped.'); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + console.log( |
| 62 | + '💁♂️ If it fails, check https://lists.w3.org/Archives/Public/public-tr-notifications/' |
| 63 | + ); |
| 64 | + const res = await request('https://labs.w3.org/echidna/api/request', { |
| 65 | + method: 'POST', |
| 66 | + body: JSON.stringify({ |
| 67 | + url: core.getInput('URL'), |
| 68 | + decision: core.getInput('DECISION'), |
| 69 | + token: core.getInput('ECHIDNA_TOKEN'), |
| 70 | + cc: core.getInput('CC') |
| 71 | + }) |
| 72 | + }); |
| 73 | + console.log(res); |
| 74 | +} |
| 75 | + |
| 76 | +// Utils |
| 77 | + |
| 78 | +function shell(command, args = [], options = {}) { |
| 79 | + return new Promise((resolve, reject) => { |
| 80 | + console.log(`💲 ${command} ${args.join(' ')}`); |
| 81 | + const child = spawn(command, args, { stdio: 'inherit', ...options }); |
| 82 | + child.on('close', code => { |
| 83 | + if (code === 0) { |
| 84 | + resolve(); |
| 85 | + } else { |
| 86 | + reject(`❌ The process exited with status code: ${code}`); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +async function install(dependencies) { |
| 93 | + await shell('npm', ['install', '--quiet', ...dependencies]); |
| 94 | +} |
| 95 | + |
| 96 | +function request(url, options) { |
| 97 | + return new Promise((resolve, reject) => { |
| 98 | + const req = https.request(url, options, res => { |
| 99 | + const chunks = []; |
| 100 | + res.on('data', data => chunks.push(data)); |
| 101 | + res.on('end', () => { |
| 102 | + let body = Buffer.concat(chunks); |
| 103 | + if (res.headers['content-type'] === 'application/json') { |
| 104 | + body = JSON.parse(body); |
| 105 | + } |
| 106 | + resolve(body); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + req.on('error', reject); |
| 111 | + if (options.body) req.write(options.body); |
| 112 | + req.end(); |
| 113 | + }); |
| 114 | +} |
0 commit comments