-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-bot.js
61 lines (53 loc) · 1.64 KB
/
merge-bot.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
const https = require("https");
const { existsSync } = require("fs");
const { resolve } = require("path");
const { execSync } = require("child_process");
const org = "hfnlabs";
const repos = [
{ name: "create-hfc", prefix: "create-hfc" },
{ name: "hfcpack", prefix: "hfcpack" },
{ name: "hfc-cli", prefix: "hfc-cli" },
];
(async () => {
await Promise.all(
repos.map(async (repo) => {
const { name, prefix } = repo;
const exists = existsSync(resolve(__dirname, prefix));
const latestCommit = await getLatestCommit(name);
if (!latestCommit) {
console.error("fail to get latest commit msg: " + name);
return;
}
const action = exists ? "pull" : "add";
const command = `git subtree ${action} --prefix=${prefix} https://github.com/${org}/${name}.git main -m "${latestCommit.commit.message}"`;
console.log("Run: " + command);
const stdout = execSync(command);
console.log(stdout.toString());
})
);
const stdout = execSync("git push");
console.log(stdout.toString());
})();
function getLatestCommit(repo) {
return new Promise((resolve) => {
https
.get(
`https://api.github.com/repos/${org}/${repo}/commits?per_page=1`,
{ headers: { "User-Agent": "hfnlabs" } },
(resp) => {
let data = "";
resp.on("data", (chunk) => {
data += chunk;
});
resp.on("end", () => {
const commits = JSON.parse(data);
resolve(commits && commits[0]);
});
}
)
.on("error", (err) => {
console.log("Error: " + err.message);
resolve(null);
});
});
}