-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-workflows.js
143 lines (122 loc) · 4.05 KB
/
update-workflows.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
/* eslint-disable no-console */
/* eslint-disable no-restricted-globals */
import * as fs from "node:fs/promises";
import { argv, env, exit } from "node:process";
import path from "node:path";
import { execSync, spawnSync } from "node:child_process";
import glob from "glob";
import { parse } from "yaml";
import { Octokit } from "@octokit/core";
import { throttling } from "@octokit/plugin-throttling";
const TITLE = "DEV: Update CI workflows";
const BRANCH = "update-ci";
const PR_BODY = "Updates CI from discourse/.github";
const RETRY_COUNT = 20;
const DELAY = 5 * 60;
function run(command, ...args) {
console.log(`> ${command} ${args.join(" ")}`);
const { error, output } = spawnSync(command, args, { cwd: "./repo" });
if (error) {
console.error(error, output);
}
}
const ThrottledOctokit = Octokit.plugin(throttling);
const octokit = new ThrottledOctokit({
auth: env["GITHUB_TOKEN"],
throttle: {
minimumSecondaryRateRetryAfter: DELAY,
onRateLimit: (retryAfter, options) => {
if (options.request.retryCount < RETRY_COUNT) {
octokit.log.warn(
`Request quota exhausted for request ${options.method} ${options.url}`,
`Retrying after ${retryAfter} seconds!`
);
return true;
}
},
onSecondaryRateLimit: (retryAfter, options) => {
if (options.request.retryCount < RETRY_COUNT) {
octokit.log.warn(
`Secondary rate limit hit for ${options.method} ${options.url}`,
`Retrying after ${retryAfter} seconds!`
);
return true;
}
},
},
});
// Workaround for @octokit/plugin-throttling bug
// See: https://github.com/octokit/plugin-throttling.js/pull/462
octokit.hook.after("request", async (response, options) => {
if (options.request.retryCount) {
options.request.retryCount = 0;
}
});
if (argv[2] !== "plugins" && argv[2] !== "themes") {
console.log("Usage: node update-workflows.js [plugins|themes]");
exit(1);
}
const type = argv[2];
const { repositories } = parse(await fs.readFile(`./${type}.yml`, "utf8"));
const workflows = glob.sync(
`./${type === "themes" ? "theme-" : "plugin-"}workflow-templates/*.yml`
);
for (const repository of repositories) {
await fs.rm("./repo", { recursive: true, force: true });
try {
execSync(
`git clone https://github.com/discourse/${repository} -q --depth 1 repo`
);
} catch (e) {
console.error(`🔥 Error cloning '${repository}'`, e);
continue;
}
await fs.mkdir("./repo/.github/workflows", { recursive: true });
await Promise.all(
workflows.map((workflowPath) => {
const filename = path.basename(workflowPath);
return fs.cp(workflowPath, `./repo/.github/workflows/${filename}`);
})
);
const oldWorkflowFiles = [
".github/workflows/plugin-linting.yml",
".github/workflows/plugin-tests.yml",
".github/workflows/component-linting.yml",
".github/workflows/component-tests.yml",
];
run("git", "rm", "--ignore-unmatch", ...oldWorkflowFiles);
const anyChanges =
execSync("git -C repo status --porcelain", {
encoding: "utf8",
}).trim() !== "";
if (!anyChanges) {
console.log(`✅ '${repository}' is already up to date`);
continue;
}
console.log(`Updating '${repository}'`);
run("git", "checkout", "-b", BRANCH);
run("git", "add", ".github/workflows/*");
run("git", "commit", "-m", TITLE);
run("git", "push", "-f", "--set-upstream", "origin", BRANCH);
try {
await octokit.request("POST /repos/{owner}/{repo}/pulls", {
owner: "discourse",
repo: repository,
title: TITLE,
head: BRANCH,
base: "main",
body: PR_BODY,
});
console.log(`✅ PR created for '${repository}'`);
} catch (error) {
const message = error.response?.data?.errors?.[0]?.message;
if (message && /A pull request already exists/.test(message)) {
console.log(`✅ PR already exists for '${repository}'`);
} else {
console.error(`❓ Failed to create PR for '${repository}'`);
console.error(error);
break;
}
}
}
await fs.rm("./repo", { recursive: true, force: true });