-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcloudflare.js
More file actions
312 lines (277 loc) · 9.42 KB
/
cloudflare.js
File metadata and controls
312 lines (277 loc) · 9.42 KB
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
/**
* @description This module provides functions to interact with Cloudflare's Rules API for managing lists and redirects.
* It includes functionality to fetch, update, and manage redirect rules in a specified list.
*
* @see https://developers.cloudflare.com/api/node/resources/rules/subresources/lists/
*/
const fs = require('fs');
const Cloudflare = require('cloudflare');
/**
* @typedef {{
* id: string;
* created_on: string;
* kind: 'ip' | 'redirect' | 'hostname' | 'asn';
* modified_on: string;
* name: string;
* num_items: number;
* num_referencing_filters: number;
* description?: string;
* }} ListsList
*/
/**
* @typedef {{
* source_url: string;
* target_url: string;
* status_code: number;
* include_subdomains?: boolean;
* preserve_query_string?: boolean;
* subpath_matching?: boolean;
* preserve_path_suffix?: boolean;
* }} Redirect
*/
/**
* @typedef {{
* redirect: Redirect;
* comment?: string;
* }} ListsListItemRedirectComment
*/
/**
* @typedef {{
* operation_id: string;
* }} ItemUpdateResponse
*/
/**
* @typedef {{
* id: string;
* redirect: Redirect;
* created_on: string;
* modified_on: string;
* }} ListItemRedirect
*/
/**
* @typedef {{
* before: string;
* after: string;
* }} ListCursor
*/
/**
* @typedef {{
* result: ListItemRedirect[];
* result_info: {
* cursors: ListCursor;
* };
* }} ListItemsPage
*/
/**
* @typedef {{
* id: string;
* status: 'pending' | 'running';
* }} ListsPendingOrRunningBulkOperation
*/
/**
* @typedef {{
* id: string;
* completed: string;
* status: 'completed';
* }} ListsCompletedBulkOperation
*/
/**
* @typedef {{
* id: string;
* completed: string;
* error: string;
* status: 'failed';
* }} ListsFailedBulkOperation
*/
/**
* @typedef {ListsPendingOrRunningBulkOperation | ListsCompletedBulkOperation | ListsFailedBulkOperation} BulkOperationGetResponse
*/
if (!process.env.CF_API_TOKEN || !process.env.CF_ACCOUNT_ID) {
process.stderr.write('Error: CF_API_TOKEN and CF_ACCOUNT_ID environment variables must be set.\n');
process.exit(1);
}
const accountId = process.env.CF_ACCOUNT_ID;
const listName = 'bitrise_docs'; // 'dev_center';
const client = new Cloudflare({
apiToken: process.env.CF_API_TOKEN,
});
/**
* Fetches the devcenter list by name.
*
* @returns {Promise<ListsList|null>} - A promise that resolves to the devcenter list or null if not found.
*/
const getDevcenterList = async () => {
try {
/** @type {{ result: ListsList[]; }} */
const response = await client.rules.lists.list({ account_id: accountId });
return response.result.filter(list => list.name === listName).pop() || null;
} catch (error) {
process.stderr.write(`Error fetching ${listName} list: ${error}\n`);
throw error;
}
};
/**
* Polls the status of a Cloudflare bulk operation by its operation ID.
*
* @param {string} operationId - The ID of the bulk operation to poll.
* @returns {Promise<ListsCompletedBulkOperation>} - A promise that resolves to the bulk operation status response.
*/
const pollBulkOperation = async (operationId) => {
try {
/** @type {BulkOperationGetResponse} */
const response = await client.rules.lists.bulkOperations.get(operationId, { account_id: accountId });
if (response.status === 'failed') {
throw new Error(response.error);
}
if (response.status === 'completed') {
return response;
}
// If the operation is still pending or running, wait and poll again
process.stdout.write(`[${response.status}]\n`);
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for 5 seconds before polling again
return pollBulkOperation(operationId);
} catch (error) {
process.stderr.write(`Error polling bulk operation ${operationId}: ${error}\n`);
throw error;
}
};
/**
* Fetches all redirect list items for a given list ID, handling pagination if necessary.
*
* @param {string} listId - The ID of the list to fetch items from.
* @param {string|null} cursorAfter - The cursor for pagination, if any.
* @returns {Promise<ListItemRedirect[]>} - A promise that resolves to an array of redirect list items.
*/
const getRedirectListItems = async (listId, cursorAfter) => {
try {
process.stdout.write('.');
const options = { account_id: accountId };
if (cursorAfter) {
options.cursor = cursorAfter;
}
/** @type {ListItemsPage} */
const listItemsPage = await client.rules.lists.items.list(listId, options);
const redirects = listItemsPage.result;
if (listItemsPage.result_info.cursors.after) {
return redirects.concat(await getRedirectListItems(listId, listItemsPage.result_info.cursors.after));
}
return redirects;
} catch (error) {
process.stderr.write(`Error fetching list items for list ${listId}: ${error}\n`);
throw error;
}
};
const main = async () => {
const devCenterList = await getDevcenterList();
if (devCenterList) {
process.stdout.write(`Found ${listName} list: ${devCenterList.id}\n`);
const newRedirectsJson = './redirects.json';
const newRedirects = JSON.parse(await fs.promises.readFile(newRedirectsJson, 'utf8'));
/** @type {ListsListItemRedirectComment[]} */
const redirectsToUpload = [];
Object.keys(newRedirects).forEach(sourceUrl => {
if (sourceUrl.match(/^[^/]/)) {
process.stderr.write(`Error: Source URL "${sourceUrl}" must start with a slash.\n`);
return;
}
const targetUrl = newRedirects[sourceUrl];
const urlPrefix = 'https://docs.bitrise.io';
const targetUrlPrefix = targetUrl.match(/^https?:\/\//) ? '' : urlPrefix;
const options = {
status_code: 301,
preserve_query_string: true
};
if (sourceUrl.endsWith('*')) {
options.subpath_matching = true;
if (targetUrl.endsWith('$1')) {
options.target_url = `${targetUrlPrefix}${targetUrl.slice(0, -2)}`;
options.preserve_path_suffix = true;
} else {
options.target_url = `${targetUrlPrefix}${targetUrl}`;
options.preserve_path_suffix = false;
}
redirectsToUpload.push({
redirect: {
source_url: `${urlPrefix}${sourceUrl.slice(0, -1)}`,
...options,
}
});
} else {
options.target_url = `${targetUrlPrefix}${targetUrl}`;
// Redirects for both .html and without .html
redirectsToUpload.push({
redirect: {
source_url: `${urlPrefix}${sourceUrl.replace(/\.html$/, '')}`,
...options
}
});
redirectsToUpload.push({
redirect: {
source_url: `${urlPrefix}${sourceUrl.replace(/\.html$/, '')}.html`,
...options
}
});
if (sourceUrl.match(/^\/en\//)) {
if (targetUrlPrefix === urlPrefix && targetUrl.match(/^\/en\//)) {
options.target_url = `${targetUrlPrefix}${targetUrl.replace(/^\/en\//, '/ja/')}`;
}
// Japanese redirects
redirectsToUpload.push({
redirect: {
source_url: `${urlPrefix}${sourceUrl.replace(/^\/en\//, '/ja/').replace(/\.html$/, '')}`,
...options
}
});
redirectsToUpload.push({
redirect: {
source_url: `${urlPrefix}${sourceUrl.replace(/^\/en\//, '/ja/').replace(/\.html$/, '')}.html`,
...options
}
});
// Legacy Japanese redirects
redirectsToUpload.push({
redirect: {
source_url: `${urlPrefix}${sourceUrl.replace(/^\/en\//, '/jp/').replace(/\.html$/, '')}`,
...options
}
});
redirectsToUpload.push({
redirect: {
source_url: `${urlPrefix}${sourceUrl.replace(/^\/en\//, '/jp/').replace(/\.html$/, '')}.html`,
...options
}
});
}
}
});
process.stdout.write(`Updating items in list ${devCenterList.id}...\n`);
/** @type {ItemUpdateResponse} */
const item = await client.rules.lists.items.update(devCenterList.id, {
account_id: accountId,
body: redirectsToUpload
});
await pollBulkOperation(item.operation_id);
process.stdout.write(`Updated items in list ${devCenterList.id}\n`);
// process.stdout.write(`Fetching redirects from list ${devCenterList.id}`);
// /** @type {ListItemRedirect[]} */
// const verifyRedirects = await getRedirectListItems(devCenterList.id);
// process.stdout.write(`done (${verifyRedirects.length})\n`);
// console.log(verifyRedirects);
// process.stdout.write(`Fetching redirects from list ${devCenterList.id}`);
// /** @type {ListItemRedirect[]} */
// const redirects = await getRedirectListItems(devCenterList.id);
// process.stdout.write(`done (${redirects.length})\n`);
// const oldRedirects = {};
// redirects.forEach(redirect => {
// const sourceUrl = redirect.redirect.source_url.trim().replace(/^https?:\/\/devcenter.bitrise.io/, '').replace(/(\.html|\/)$/, '');
// const targetUrl = redirect.redirect.target_url.trim().replace(/^https?:\/\/devcenter.bitrise.io/, '').replace(/(\.html|\/)$/, '.html');
// oldRedirects[sourceUrl] = targetUrl;
// });
// console.log(`Found ${Object.keys(oldRedirects).length} redirects`);
// await fs.promises.writeFile('./old_redirects.json', JSON.stringify(oldRedirects, null, 2));
}
};
main().catch((error) => {
process.stderr.write(`${error}\n`);
process.exit(1);
});