forked from anuraghazra/github-readme-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview-theme.js
81 lines (70 loc) · 2.08 KB
/
preview-theme.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
const core = require("@actions/core");
const github = require("@actions/github");
const parse = require("parse-diff");
require("dotenv").config();
function getPrNumber() {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
return undefined;
}
return pullRequest.number;
}
async function run() {
try {
const token = core.getInput("token");
const octokit = github.getOctokit(token || process.env.PERSONAL_TOKEN);
const pullRequestId = getPrNumber();
if (!pullRequestId) {
console.log("PR not found");
return;
}
let res = await octokit.pulls.get({
owner: "anuraghazra",
repo: "github-readme-stats",
pull_number: pullRequestId,
mediaType: {
format: "diff",
},
});
let diff = parse(res.data);
let colorStrings = diff
.find((file) => file.to === "themes/index.js")
.chunks[0].changes.filter((c) => c.type === "add")
.map((c) => c.content.replace("+", ""))
.join("");
let matches = colorStrings.match(/(title_color:.*bg_color.*\")/);
let colors = matches && matches[0].split(",");
if (!colors) {
await octokit.issues.createComment({
owner: "anuraghazra",
repo: "github-readme-stats",
body: `
\rTheme preview (bot)
Cannot create theme preview
`,
issue_number: pullRequestId,
});
return;
}
colors = colors.map((color) =>
color.replace(/.*\:\s/, "").replace(/\"/g, "")
);
let titleColor = colors[0];
let iconColor = colors[1];
let textColor = colors[2];
let bgColor = colors[3];
const url = `https://github-readme-stats.vercel.app/api?username=anuraghazra&title_color=${titleColor}&icon_color=${iconColor}&text_color=${textColor}&bg_color=${bgColor}&show_icons=true`;
await octokit.issues.createComment({
owner: "anuraghazra",
repo: "github-readme-stats",
body: `
\rTheme preview (bot)
\r![](${url})
`,
issue_number: pullRequestId,
});
} catch (error) {
core.setFailed(error.message);
}
}
run();