|
| 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