-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
273 lines (249 loc) · 7.55 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// @ts-check
import * as core from '@actions/core';
import FormData from 'form-data';
import jwt from 'jsonwebtoken';
import * as fs from 'node:fs';
import * as path from 'node:path';
import utf8 from 'utf8';
const AMO_HOST = 'addons.mozilla.org';
/**
* @typedef {object} UploadDetail
* @property {string} uuid
* @property {string} channel
* @property {boolean} processed
* @property {boolean} submitted
* @property {string} url
* @property {boolean} valid
* @property {Record<string, unknown>} [validation]
* @property {string} version
*/
/**
* @typedef {object} VersionDetail
* @property {number} id
* @property {'listed' | 'unlisted'} channel
* @property {Record<string, { min?: string; max?: string }>} compatibility
* @property {string} edit_url
* @property {FileDetail} file
* @property {LicenseDetail} license
* @property {Record<string, string> | null} release_notes
* @property {string} [reviewed]
* @property {boolean} is_strict_compatibility_enabled
* @property {string | null} source
* @property {string} version
*/
/**
* @typedef {object} FileDetail
* @property {number} id
* @property {string} created
* @property {string} hash
* @property {boolean} is_mozilla_signed_extension
* @property {Array<string>} optional_permissions
* @property {Array<string>} permissions
* @property {number} size
* @property {number} status
* @property {string} url
*/
/**
* @typedef {object} LicenseDetail
* @property {boolean} is_custom
* @property {Record<string, string> | null} name
* @property {Record<string, string> | null} text
* @property {string | null} url
* @property {string | null} slug
*/
async function main() {
// Look for the add-on asset
const workspace = /** @type string */ (process.env.GITHUB_WORKSPACE);
const addonFile = path.join(
workspace,
core.getInput('addon_file', { required: true })
);
if (!fs.existsSync(addonFile)) {
throw new Error(`Asset file not found ${addonFile}`);
}
core.info(`Found add-on file: ${addonFile}`);
// Validate the source asset, if any
let srcFile = core.getInput('src_file');
if (srcFile?.length) {
srcFile = path.join(workspace, srcFile);
if (!fs.existsSync(srcFile)) {
throw new Error(`Source file not found ${srcFile}`);
}
core.info(`Found source file: ${srcFile}`);
}
// Upload the add-on asset
const formData = new FormData();
formData.append('upload', fs.createReadStream(addonFile));
formData.append('channel', 'listed');
const uploadResponse = await uploadToAmo({
path: '/api/v5/addons/upload/',
formData,
});
/** @type UploadDetail */
const { uuid, version, valid: initiallyValid } = JSON.parse(uploadResponse);
core.info(
`Successfully uploaded add-on for version ${version} with uuid ${uuid}`
);
// Query the upload details API until it is valid
//
// Set an overall timeout of 10 minutes first, however.
const tenMinuteTimeout = setTimeout(
() => {
core.setFailed('Timed out waiting for upload to be valid');
process.exit(1);
},
10 * 60 * 1000
);
try {
let valid = initiallyValid;
while (!valid) {
// Recommended polling interval is 5~10 seconds according to:
// https://blog.mozilla.org/addons/2022/03/17/new-api-for-submitting-and-updating-add-ons/
core.info('Waiting before checking if upload is valid');
await new Promise((resolve) => setTimeout(resolve, 5000));
core.info('Checking upload status...');
/** @type UploadDetail */
const { valid, validation } = JSON.parse(
await getFromAmo(`/api/v5/addons/upload/${uuid}`)
);
if (valid) {
core.info('Upload is valid');
break;
} else if (valid === false && validation) {
// I have no idea what these validation objects look like.
// The docs just say, "the validation results JSON blob".
throw new Error(`Validation error: ${JSON.stringify(validation)}`);
}
core.info('Upload is still not valid');
}
} finally {
clearTimeout(tenMinuteTimeout);
}
// Create a new version with the provided release notes
const addonId = core.getInput('addon_id', { required: true });
// TODO: Support a release_notes_json parameter that allows specifying a JSON
// string with all the localized release notes.
const releaseNotes = core.getInput('release_notes');
const postData = JSON.stringify({
compatibility: ['android', 'firefox'],
release_notes: releaseNotes ? { 'en-US': releaseNotes } : null,
upload: uuid,
});
/** @type VersionDetail */
const { id: versionId, version: versionString } = JSON.parse(
await postToAmo({
path: `/api/v5/addons/addon/${addonId}/versions/`,
jsonData: postData,
})
);
core.info(`Successfully created version ${versionString} (id: ${versionId})`);
// Upload the source file (if any)
if (srcFile?.length) {
const form = new FormData();
form.append('source', fs.createReadStream(srcFile));
uploadToAmo({
path: `/api/v5/addons/addon/${addonId}/versions/${versionId}/`,
formData: form,
method: 'PATCH',
});
core.info('Successfully uploaded source asset');
}
core.info('Publishing complete.');
}
main().catch((error) => {
core.error(error);
core.setFailed(error.message);
});
/**
* @param {string} path
* @returns {Promise<string>}
*/
async function getFromAmo(path) {
const url = `https://${AMO_HOST}${path}`;
const res = await fetch(url, {
headers: { Authorization: `JWT ${getJwtToken()}` },
});
if (!res.ok) {
throw new Error(
`Got status ${res.status} (${res.statusText}) for GET ${url}`
);
}
return res.text();
}
/**
* @param {object} options
* @param {string} options.path
* @param {string} options.jsonData
* @returns {Promise<string>}
*/
async function postToAmo({ path, jsonData }) {
const url = `https://${AMO_HOST}${path}`;
const res = await fetch(url, {
method: 'POST',
headers: {
Authorization: `JWT ${getJwtToken()}`,
'Content-Type': 'application/json',
'Content-Length': String(utf8.encode(jsonData).length),
},
body: jsonData,
});
return res.text();
}
/**
* @param {object} options
* @param {string} options.path
* @param {FormData} options.formData
* @param {string} [options.method]
* @returns {Promise<string>}
*/
function uploadToAmo({ path, formData, method = 'POST' }) {
core.info(`Uploading to https://${AMO_HOST}${path} (${method})`);
return new Promise((resolve, reject) => {
formData.submit(
{
host: AMO_HOST,
method,
path,
protocol: 'https:',
headers: {
Authorization: `JWT ${getJwtToken()}`,
},
},
(err, res) => {
if (err) {
reject(err);
return;
}
if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) {
reject(
new Error(
`Got status ${res.statusCode} for POST https://${AMO_HOST}${path}`
)
);
} else {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
resolve(body);
});
}
}
);
});
}
function getJwtToken() {
const jwtIss = core.getInput('amo_jwt_iss', { required: true });
const jwtSecret = core.getInput('amo_jwt_secret', { required: true });
const issuedAt = Math.floor(Date.now() / 1000);
const payload = {
iss: jwtIss,
jti: Math.random().toString(),
iat: issuedAt,
exp: issuedAt + 60,
};
return jwt.sign(payload, jwtSecret, {
algorithm: 'HS256', // HMAC-SHA256 signing algorithm
});
}