forked from pingcap/docs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilterUpdateFiles.js
122 lines (110 loc) · 2.9 KB
/
filterUpdateFiles.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
import * as fs from "fs";
import path from "path";
import axios from "axios";
import { Octokit } from "octokit";
const GH_TOKEN = process.env.GH_TOKEN || "";
const octokit = GH_TOKEN
? new Octokit({
auth: GH_TOKEN,
})
: new Octokit();
const getLocalCfg = () => {
const fileContent = fs.readFileSync("./latest_translation_commit.json");
const data = JSON.parse(fileContent);
return data;
};
const writeLocalCfg = (cfg) => {
const data = JSON.stringify(cfg);
fs.writeFileSync("./latest_translation_commit.json", data);
};
const ghGetBranch = async (branchName = "master") => {
const result = await octokit.request(
`GET /repos/pingcap/docs/branches/${branchName}`,
{
owner: "pingcap",
repo: "docs",
branch: branchName,
}
);
if (result.status === 200) {
const data = result.data;
return data;
}
throw new Error(`ghGetBranch error: ${result}`);
};
const ghCompareCommits = async (base = "", head = "") => {
const basehead = `${base}...${head}`;
const result = await octokit.request(
`GET /repos/pingcap/docs/compare/${basehead}`,
{
owner: "pingcap",
repo: "docs",
basehead,
}
);
if (result.status === 200) {
const data = result.data;
return data;
}
throw new Error(`ghGetBranch error: ${result}`);
};
const downloadFile = async (url, targetPath) => {
const response = await axios({
method: "GET",
url,
responseType: "stream",
});
const dir = path.dirname(targetPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// pipe the result stream into a file on disc
response.data.pipe(fs.createWriteStream(targetPath));
// return a promise and resolve when download finishes
return new Promise((resolve, reject) => {
response.data.on("end", () => {
resolve();
});
response.data.on("error", () => {
reject();
});
});
};
const deleteFile = (targetFile) => {
fs.rmSync(targetFile);
};
const handleFiles = async (fileList = []) => {
console.log(fileList);
for (let file of fileList) {
const { status, raw_url, filename, previous_filename } = file;
if (!filename.endsWith(".md")) {
continue;
}
switch (status) {
case "added":
case "modified":
await downloadFile(raw_url, `tmp/${filename}`);
break;
case "removed":
deleteFile(filename);
break;
case "renamed":
deleteFile(previous_filename);
await downloadFile(raw_url, `tmp/${filename}`);
break;
}
}
};
const main = async () => {
const { target: branchName, sha: base } = getLocalCfg();
const targetBranchData = await ghGetBranch(branchName);
const head = targetBranchData?.commit?.sha;
const comparedDetails = await ghCompareCommits(base, head);
const files = comparedDetails?.files || [];
handleFiles(files);
writeLocalCfg({
target: branchName,
sha: head,
});
};
main();