-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
150 lines (116 loc) · 4.38 KB
/
index.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
144
145
146
147
148
149
150
const core = require("@actions/core");
const { Toolkit } = require('actions-toolkit');
const fs = require("fs");
const { spawn } = require("child_process");
import { Octokit } from "@octokit/rest";
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
const owner = core.getInput("COMMIT_OWNER");
const repo = core.getInput("COMMIT_REPO");
const path = core.getInput("COMMIT_PATH") || '';
// const owner = 'kerong2002';
// const repo = 'github_action';
console.log(`Owner: ${owner}, Repo: ${repo}, Path: ${path}`);
const files = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: owner,
repo: repo,
path: path,
});
// yml input
const GITHUB_TOKEN = core.getInput("GITHUB_TOKEN");
const COMMITTER_USERNAME = core.getInput("COMMITTER_USERNAME");
const COMMITTER_EMAIL = core.getInput("COMMITTER_EMAIL");
const COMMIT_MSG = core.getInput("COMMIT_MSG");
core.setSecret(GITHUB_TOKEN);
const baseUrl = "https://kerong2002.github.io";
const exec = (cmd, args = [], options = {}) =>
new Promise((resolve, reject) => {
let outputData = "";
const optionsToCLI = {
...options,
};
if (!optionsToCLI.stdio) {
Object.assign(optionsToCLI, { stdio: ["inherit", "inherit", "inherit"] });
}
const app = spawn(cmd, args, optionsToCLI);
if (app.stdout) {
// Only needed for pipes
app.stdout.on("data", function (data) {
outputData += data.toString();
});
}
app.on("close", (code) => {
if (code !== 0) {
return reject({ code, outputData });
}
return resolve({ code, outputData });
});
app.on("error", () => reject({ code: 1, outputData }));
});
const commitReadmeFile = async () => {
await exec("git", ["config", "--global", "user.email", COMMITTER_EMAIL]);
if (GITHUB_TOKEN) {
await exec("git", [
"remote",
"set-url",
"origin",
`https://${GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`,
]);
}
await exec("git", ["config", "--global", "user.name", COMMITTER_USERNAME]);
await exec("git", ["add", "."]);
await exec("git", ["commit", "-m", COMMIT_MSG]);
await exec("git", ["push"]);
};
Toolkit.run(async (tools) => {
tools.log.success("Edit README.md Start...");
console.log("kerong test"); //輸出在螢幕上
const readmeContent = fs.readFileSync("./README.md", "utf-8").split("\n");
//找到 START TAG
let startIndex = readmeContent.findIndex(
(content) => content.trim() === "<!-- UPDATE_README:START -->"
);
let endIndex = readmeContent.findIndex(
(content) => content.trim() === "<!-- UPDATE_README:END -->"
);
// START TAG 不存在
if (startIndex === -1 || endIndex ==-1)
return tools.exit.failure("Not Found Start Update Comments");
if(startIndex != -1 && endIndex !=-1){
startIndex++;
endIndex++;
}
//過濾出所有的 `.cpp` 文件
const cppFiles = files.data.filter(file => file.name.endsWith('.cpp'));
const cppFileCount = cppFiles.length;
const oldContent = readmeContent.slice(startIndex, endIndex-1).join("\n");
const newContent = `**I have ${cppFileCount} cpp files.**`;
const compareOldContent = oldContent.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
const compareNewContent = newContent.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
// tools.log(compareOldContent);
// tools.log(compareNewContent);
if (compareOldContent === compareNewContent)
tools.exit.success("Same result");
// 把 <!-- UPDATE_README:START --> 到 <!-- UPDATE_README:END -->間的內容刪掉
// 取得 START ~ END 的間隙
let gap = endIndex - startIndex;
readmeContent.splice(startIndex, gap-1);
if(startIndex != -1 && endIndex !=-1){
readmeContent.splice(
startIndex,
0,
`**I have ${cppFileCount} cpp files.**`
)
}
tools.log.success("Updated README with the recent blog outline");
fs.writeFileSync("./README.md", readmeContent.join("\n"));
try {
await commitReadmeFile();
tools.log.success("Commit file success");
} catch (err) {
tools.log.debug("Something went wrong");
return tools.exit.failure(err);
}
tools.exit.success("Exit Edit README.md Action");
});