Skip to content

Commit 32570ca

Browse files
authoredAug 1, 2023
add cron job scripts (pingcap#14395)
1 parent 3409f26 commit 32570ca

File tree

4 files changed

+1246
-0
lines changed

4 files changed

+1246
-0
lines changed
 

‎latest_translation_commit.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"target":"master","sha":""}

‎package-lock.json

+1,124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"license": "MIT",
66
"type": "module",
77
"dependencies": {
8+
"axios": "^1.4.0",
89
"glob": "^8.0.3",
910
"mdast-util-from-markdown": "^1.2.0",
1011
"mdast-util-frontmatter": "^1.0.0",
@@ -14,6 +15,7 @@
1415
"micromark-extension-frontmatter": "^1.0.0",
1516
"micromark-extension-gfm": "^2.0.1",
1617
"micromark-extension-mdxjs": "^1.0.0",
18+
"octokit": "^3.1.0",
1719
"unist-util-visit": "^4.1.0"
1820
}
1921
}

‎scripts/filterUpdateFiles.js

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import * as fs from "fs";
2+
import path from "path";
3+
import axios from "axios";
4+
import { Octokit } from "octokit";
5+
6+
const GH_TOKEN = process.env.GH_TOKEN || "";
7+
8+
const octokit = GH_TOKEN
9+
? new Octokit({
10+
auth: GH_TOKEN,
11+
})
12+
: new Octokit();
13+
14+
const getLocalCfg = () => {
15+
const fileContent = fs.readFileSync("./latest_translation_commit.json");
16+
const data = JSON.parse(fileContent);
17+
return data;
18+
};
19+
20+
const writeLocalCfg = (cfg) => {
21+
const data = JSON.stringify(cfg);
22+
fs.writeFileSync("./latest_translation_commit.json", data);
23+
};
24+
25+
const ghGetBranch = async (branchName = "master") => {
26+
const result = await octokit.request(
27+
`GET /repos/pingcap/docs/branches/${branchName}`,
28+
{
29+
owner: "pingcap",
30+
repo: "docs",
31+
branch: branchName,
32+
}
33+
);
34+
if (result.status === 200) {
35+
const data = result.data;
36+
return data;
37+
}
38+
throw new Error(`ghGetBranch error: ${result}`);
39+
};
40+
41+
const ghCompareCommits = async (base = "", head = "") => {
42+
const basehead = `${base}...${head}`;
43+
const result = await octokit.request(
44+
`GET /repos/pingcap/docs/compare/${basehead}`,
45+
{
46+
owner: "pingcap",
47+
repo: "docs",
48+
basehead,
49+
}
50+
);
51+
if (result.status === 200) {
52+
const data = result.data;
53+
return data;
54+
}
55+
throw new Error(`ghGetBranch error: ${result}`);
56+
};
57+
58+
const downloadFile = async (url, targetPath) => {
59+
const response = await axios({
60+
method: "GET",
61+
url,
62+
responseType: "stream",
63+
});
64+
const dir = path.dirname(targetPath);
65+
if (!fs.existsSync(dir)) {
66+
fs.mkdirSync(dir, { recursive: true });
67+
}
68+
// pipe the result stream into a file on disc
69+
response.data.pipe(fs.createWriteStream(targetPath));
70+
// return a promise and resolve when download finishes
71+
return new Promise((resolve, reject) => {
72+
response.data.on("end", () => {
73+
resolve();
74+
});
75+
76+
response.data.on("error", () => {
77+
reject();
78+
});
79+
});
80+
};
81+
82+
const deleteFile = (targetFile) => {
83+
fs.rmSync(targetFile);
84+
};
85+
86+
const handleFiles = async (fileList = []) => {
87+
console.log(fileList);
88+
for (let file of fileList) {
89+
const { status, raw_url, filename, previous_filename } = file;
90+
switch (status) {
91+
case "added":
92+
case "modified":
93+
await downloadFile(raw_url, `tmp/${filename}`);
94+
break;
95+
case "removed":
96+
deleteFile(filename);
97+
break;
98+
case "renamed":
99+
deleteFile(previous_filename);
100+
await downloadFile(raw_url, `tmp/${filename}`);
101+
break;
102+
}
103+
}
104+
};
105+
106+
const main = async () => {
107+
const { target: branchName, sha: base } = getLocalCfg();
108+
const targetBranchData = await ghGetBranch(branchName);
109+
const head = targetBranchData?.commit?.sha;
110+
const comparedDetails = await ghCompareCommits(base, head);
111+
const files = comparedDetails?.files || [];
112+
handleFiles(files);
113+
writeLocalCfg({
114+
target: branchName,
115+
sha: head,
116+
});
117+
};
118+
119+
main();

0 commit comments

Comments
 (0)
Please sign in to comment.