This repository was archived by the owner on Mar 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
201 lines (176 loc) · 5.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const https = require('https');
const { existsSync } = require('fs');
const { spawn } = require('child_process');
const core = require('@actions/core');
const { issueCommand } = require('@actions/core/lib/command');
const ECHIDNA_SUCCESS_STATUS = 'success';
const ECHIDNA_FAILURE_STATUS = 'failure';
(async function main() {
core.warning(
`The action "respec-w3c-auto-publish" has been deprecated in favor of the "spec-prod" action.` +
` Please use spec-prod: https://github.com/w3c/spec-prod`,
);
process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = '1';
process.env.PUPPETEER_EXECUTABLE_PATH = '/usr/bin/google-chrome';
await run('Install dependencies', installDependencies);
await run('Validate spec', validate);
await run('Publish to /TR/', publish);
})();
async function run(name, fn) {
try {
core.startGroup(name);
await fn();
core.endGroup();
} catch (error) {
core.endGroup();
core.setFailed(error.message);
process.exit(1);
}
}
async function installDependencies() {
await install(['respec', 'respec-validator']);
}
async function validate() {
const file = core.getInput('INPUT_FILE');
if (!existsSync(file)) {
throw new Error(`📛 INPUT_FILE: "${file}" not found!`);
}
const validator = './node_modules/.bin/respec-validator';
const args = [file];
const validateMarkup = JSON.parse(core.getInput('VALIDATE_MARKUP'));
if (!validateMarkup) args.push('--no-validator');
const checkLinks = JSON.parse(core.getInput('CHECK_LINKS'));
if (!checkLinks) args.push('--no-links');
await shell(validator, args);
}
async function publish() {
const shouldPublish = process.env.GITHUB_EVENT_NAME !== 'pull_request';
if (!shouldPublish) {
console.log('👻 Skipped.');
return;
}
console.log(
'💁♂️ If it fails, check https://lists.w3.org/Archives/Public/public-tr-notifications/',
);
const data = {
url: core.getInput('ECHIDNA_MANIFEST_URL', { required: true }),
decision: core.getInput('WG_DECISION_URL', { required: true }),
token: core.getInput('ECHIDNA_TOKEN', { required: true }),
cc: core.getInput('CC'),
};
const file = core.getInput('INPUT_FILE');
core.setSecret(data.token);
const body = new URLSearchParams(Object.entries(data)).toString();
const id = await request('https://labs.w3.org/echidna/api/request', {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
const result = await getPublishStatus(id);
console.log(result);
switch (result.status) {
case ECHIDNA_SUCCESS_STATUS:
return core.info(`🎉 Published at: ${result.url}`);
case ECHIDNA_FAILURE_STATUS: {
for (const { message, id, name } of getSpecberusErrors(result.response)) {
const msg = `echidna/specberus:\n\t${name}/${id}:\n\t${message}`;
issueCommand('error', { file, line: '1' }, msg);
}
throw new Error('💥 Echidna publish has failed.');
}
default:
core.warning('🚧 Echidna publish job is pending.');
}
}
async function getPublishStatus(id) {
let url = new URL('https://labs.w3.org/echidna/api/status');
url.searchParams.set('id', id);
url = url.href;
const isJSON = arg => typeof arg === 'string' && arg.startsWith('{');
// wait this many seconds before each job status check attempt;
// ... with a maximum of 18 seconds total wait.
let RETRY_DURATIONS = [2, 3, 2, 4, 2, 5];
const state = { id, status: 'pending', url, response: undefined };
do {
const wait = RETRY_DURATIONS.shift();
console.log(`⏱️ Wait ${wait}s for job to finish...`);
await new Promise(res => setTimeout(res, wait * 1000));
let response;
try {
response = await request(url, { method: 'GET' });
if (typeof response === 'string' && !response.startsWith('{')) {
throw response;
}
response = isJSON(response) ? JSON.parse(response) : response;
state.status = response.results.status;
if (state.status !== ECHIDNA_SUCCESS_STATUS) {
throw state.status;
}
return {
id,
status: state.status,
url: response.results.metadata.thisVersion,
};
} catch {
state.response = response;
}
} while (
state.status !== ECHIDNA_SUCCESS_STATUS &&
state.status !== ECHIDNA_FAILURE_STATUS &&
RETRY_DURATIONS.length > 0
);
return state;
}
function getSpecberusErrors(response) {
let errors = [];
let profile;
try {
profile = response.results.metadata.profile;
errors = response.results.jobs.specberus.errors || [];
} catch (err) {
console.error(err);
return [];
}
const specberus = require('./specberus/messages.js');
return errors.map(({ type, key, extra }) =>
specberus.getMessage(profile, type, key, extra),
);
}
// Utils
function shell(command, args = [], options = {}) {
return new Promise((resolve, reject) => {
console.log(`💲 ${command} ${args.join(' ')}`);
const child = spawn(command, args, { stdio: 'inherit', ...options });
child.on('close', code => {
if (code === 0) {
resolve();
} else {
reject(new Error(`💥 The process exited with status code: ${code}`));
}
});
});
}
async function install(dependencies) {
await shell('npm', ['install', '--silent', ...dependencies]);
}
function request(url, options) {
return new Promise((resolve, reject) => {
console.log(`📡 Request: ${url}`);
const req = https.request(url, options, res => {
const chunks = [];
res.on('data', data => chunks.push(data));
res.on('end', () => {
let body = Buffer.concat(chunks).toString();
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
resolve(body);
});
});
req.on('error', reject);
if (options.body) req.write(options.body);
req.end();
});
}