-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (53 loc) · 1.79 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
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require("fs");
let octokit;
async function generateBadges(report) {
let badgeContent = `### Code Duplication Stats in app-v2`;
for (const [key, value] of Object.entries(report.formats)) {
badgeContent += `\n ![${key}](https://img.shields.io/badge/${key}-${value.total.percentage}%25-lightgrey)`;
}
return badgeContent;
}
async function run() {
try {
octokit = new github.getOctokit(core.getInput("token"));
const filepath = core.getInput("path");
const data = fs.readFileSync(
`${process.env.GITHUB_WORKSPACE}/${filepath}`,
"utf8"
);
const json = JSON.parse(data);
const badgesString = await generateBadges(json.statistics);
appendBadgeToReadMe(badgesString);
} catch (error) {
core.setFailed(error.message);
}
}
async function appendBadgeToReadMe(badge) {
const res = await octokit.request(
`GET /repos/fylein/fyle-app/contents/README.md`,
{ ref: core.getInput("branch") }
);
const { path, sha, content, encoding } = res.data;
const rawContent = Buffer.from(content, encoding).toString();
const startIndex = rawContent.indexOf("### Code Duplication Stats in app-v2");
const updatedContent = `${
startIndex === -1 ? rawContent : rawContent.slice(0, startIndex)
}${badge}`;
commitNewReadme(path, sha, encoding, updatedContent);
}
async function commitNewReadme(path, sha, encoding, updatedContent) {
try {
await octokit.request(`PUT /repos/fylein/fyle-app/contents/README.md`, {
message: "Added Code Duplicate Report",
content: Buffer.from(updatedContent, "utf-8").toString(encoding),
path,
sha,
branch: core.getInput("branch"),
});
} catch (error) {
core.setFailed(error.message);
}
}
run();