-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate_remote_settings_records.mjs
356 lines (322 loc) · 9.48 KB
/
update_remote_settings_records.mjs
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
/* global process */
// This script consumes the following env variables:
// - AUTHORIZATION (mandatory): Raw authorization header (e.g. `AUTHORIZATION='Bearer XXXXXXXXXXXXX'`)
// - SERVER (mandatory): Writer server URL (eg. https://remote-settings.allizom.org/v1)
// - ENVIRONMENT (optional): dev, stage, prod. When set to `dev`, the script will approve its own changes.
// - DRY_RUN (optional): If set to 1, no changes will be made to the collection, this will
// only log the actions that would be done.
// This node script fetches `https://github.com/mdn/browser-compat-data/tree/main/browsers`
// and updates records from the associated collection in RemoteSettings.
// In the future, it will also handle https://github.com/mdn/browser-compat-data/tree/main/css.
import fetch from "node-fetch";
import btoa from "btoa";
const SUCCESS_RET_VALUE = 0;
const FAILURE_RET_VALUE = 1;
const VALID_ENVIRONMENTS = ["dev", "stage", "prod"];
if (!process.env.AUTHORIZATION) {
console.error(`AUTHORIZATION environment variable needs to be set`);
process.exit(FAILURE_RET_VALUE);
}
if (!process.env.SERVER) {
console.error(
`SERVER environment variable needs to be set`
);
process.exit(FAILURE_RET_VALUE);
}
if (
process.env.ENVIRONMENT &&
!VALID_ENVIRONMENTS.includes(process.env.ENVIRONMENT)
) {
console.error(
`ENVIRONMENT environment variable needs to be set to one of the following values: ${VALID_ENVIRONMENTS.join(
", "
)}`
);
process.exit(FAILURE_RET_VALUE);
}
const COLLECTION_ENDPOINT = `${process.env.SERVER}/buckets/main-workspace/collections/devtools-compatibility-browsers`;
const RECORDS_ENDPOINT = `${COLLECTION_ENDPOINT}/records`;
const IS_DRY_RUN = process.env.DRY_RUN == "1";
const HEADERS = {
"Content-Type": "application/json",
Authorization: process.env.AUTHORIZATION.startsWith("Bearer ")
? process.env.AUTHORIZATION
: `Basic ${btoa(process.env.AUTHORIZATION)}`,
}
update()
.then(() => {
return process.exit(SUCCESS_RET_VALUE);
})
.catch((e) => {
console.error(e);
return process.exit(FAILURE_RET_VALUE);
});
async function update() {
const records = await getRSRecords();
const operations = { added: [], updated: [], removed: [] };
const browsersMdn = await getFlatBrowsersMdnData();
for (const browserMdn of browsersMdn) {
const rsRecord = records.find(
(record) =>
record.browserid == browserMdn.browserid &&
record.version == browserMdn.version
);
if (browserMdn.status == "retired") {
if (rsRecord) {
const succesful = await deleteRecord(rsRecord);
if (succesful) {
operations.removed.push(rsRecord);
}
}
continue;
}
if (!rsRecord) {
const succesful = await createRecord(browserMdn);
if (succesful) {
operations.added.push(browserMdn);
}
continue;
}
if (
rsRecord.status !== browserMdn.status ||
rsRecord.name !== browserMdn.name
) {
const succesful = await updateRecord(rsRecord, browserMdn);
if (succesful) {
operations.updated.push(browserMdn);
}
}
}
for (const record of records) {
const browserMdn = browsersMdn.find(
(browser) =>
browser.browserid == record.browserid &&
browser.version == record.version
);
if (!browserMdn) {
const succesful = await deleteRecord(record);
if (succesful) {
operations.removed.push(record);
}
}
}
console.group("Results");
console.log("Added:", operations.added.length);
if (operations.added.length > 0) {
console.table(operations.added);
}
console.log("Updated:", operations.updated.length);
if (operations.updated.length > 0) {
console.table(operations.updated);
}
console.log("Removed:", operations.removed.length);
if (operations.removed.length > 0) {
console.table(operations.removed);
}
console.groupEnd();
if (
operations.added.length +
operations.updated.length +
operations.removed.length ==
0
) {
console.log("No changes detected");
} else {
const refreshedRecords = await getRSRecords();
console.log("Browsers data synced ✅\nRefreshed records:");
console.table(refreshedRecords);
if (process.env.ENVIRONMENT === "dev") {
await approveChanges();
} else {
await requestReview();
}
}
}
async function getRSRecords() {
console.log(`Get existing records from ${RECORDS_ENDPOINT}`);
const response = await fetch(RECORDS_ENDPOINT, {
method: "GET",
headers: HEADERS,
});
if (response.status !== 200) {
throw new Error(
`Can't retrieve records: "[${response.status}] ${response.statusText}"`
);
}
const { data } = await response.json();
return data;
}
/**
* Create a record on RemoteSetting
*
* @param {Object} browserMdn: An item from the result of getFlatBrowsersMdnData
* @returns {Boolean} Whether the API call was succesful or not
*/
async function createRecord(browserMdn) {
console.log(
IS_DRY_RUN ? "[DRY_RUN]" : "",
"Create",
browserMdn.browserid,
browserMdn.version
);
if (IS_DRY_RUN) {
return true;
}
const response = await fetch(`${RECORDS_ENDPOINT}`, {
method: "POST",
body: JSON.stringify({ data: browserMdn }),
headers: HEADERS,
});
const succesful = response.status == 201;
if (!succesful) {
console.warn(
`Couldn't create record: "[${response.status}] ${response.statusText}"`
);
}
return succesful;
}
/**
* Update a record on RemoteSetting
*
* @param {Object} record: The existing record on RemoteSetting
* @param {Object} browserMdn: An item from the result of getFlatBrowsersMdnData whose data
* will be put into the record.
* @returns {Boolean} Whether the API call was succesful or not
*/
async function updateRecord(record, browserMdn) {
console.log(
IS_DRY_RUN ? "[DRY_RUN]" : "",
"Update",
record.browserid,
record.version
);
if (IS_DRY_RUN) {
return true;
}
const response = await fetch(`${RECORDS_ENDPOINT}/${record.id}`, {
method: "PUT",
body: JSON.stringify({ data: browserMdn }),
headers: HEADERS,
});
const succesful = response.status == 200;
if (!succesful) {
console.warn(
`Couldn't update record: "[${response.status}] ${response.statusText}"`
);
}
return succesful;
}
/**
* Remove a record on RemoteSetting
*
* @param {Object} record: The existing record on RemoteSetting
* @returns {Boolean} Whether the API call was succesful or not
*/
async function deleteRecord(record) {
console.log(
IS_DRY_RUN ? "[DRY_RUN]" : "",
"Delete",
record.browserid,
record.version
);
if (IS_DRY_RUN) {
return true;
}
const response = await fetch(`${RECORDS_ENDPOINT}/${record.id}`, {
method: "DELETE",
headers: HEADERS,
});
const succesful = response.status == 200;
if (!succesful) {
console.warn(
`Couldn't delete record: "[${response.status}] ${response.statusText}"`
);
}
return succesful;
}
/**
* Ask for review on the collection.
*/
async function requestReview() {
console.log(IS_DRY_RUN ? "[DRY_RUN]" : "", "Requesting review");
if (IS_DRY_RUN) {
return true;
}
const response = await fetch(COLLECTION_ENDPOINT, {
method: "PATCH",
body: JSON.stringify({ data: { status: "to-review" } }),
headers: HEADERS,
});
if (response.status === 200) {
console.log("Review requested ✅");
} else {
console.warn(
`Couldn't request review: "[${response.status}] ${response.statusText}"`
);
}
}
/**
* Automatically approve changes made on the collection.
* ⚠️ This only works on the `dev` server.
*/
async function approveChanges() {
console.log(IS_DRY_RUN ? "[DRY_RUN]" : "", "Approving changes");
if (IS_DRY_RUN) {
return true;
}
const response = await fetch(COLLECTION_ENDPOINT, {
method: "PATCH",
body: JSON.stringify({ data: { status: "to-sign" } }),
headers: HEADERS,
});
if (response.status === 200) {
console.log("Changes approved ✅");
} else {
console.warn(
`Couldn't automatically approve changes: "[${response.status}] ${response.statusText}"`
);
}
}
async function getFlatBrowsersMdnData() {
// See https://github.com/mdn/browser-compat-data.
const response = await fetch("https://unpkg.com/@mdn/browser-compat-data/data.json");
const compatData = await response.json();
const browsers = [];
for (const [browserid, browserInfo] of Object.entries(compatData.browsers)) {
for (const [releaseNumber, releaseInfo] of Object.entries(
browserInfo.releases
)) {
if (!browserInfo.name) {
console.error(
`${browserid} "name" property is expected but wasn't found`,
browserInfo
);
continue;
}
if (!releaseInfo.status) {
console.error(
`${browserid} "status" property is expected but wasn't found`,
releaseInfo
);
continue;
}
if (!releaseNumber || !releaseNumber.match(/\d/)) {
console.error(
`${browserid} "releaseNumber" doesn't have expected shape`,
releaseNumber
);
continue;
}
browsers.push({
browserid,
name: browserInfo.name,
status: releaseInfo.status,
version: releaseNumber,
});
}
}
return browsers;
}