Skip to content

Commit 53c4940

Browse files
authoredAug 2, 2020
infra: theme preview gh action (anuraghazra#296)
* chore: testing theme preview script * fix: workflow * test current pr * comment on pr script * fix: preview theme script * fix: preview theme
1 parent 6f7e354 commit 53c4940

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed
 

‎.github/workflows/preview-theme.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Theme preview
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
branches:
7+
- master
8+
- theme-preview-script
9+
paths:
10+
- "themes/index.js"
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v1
18+
- name: setup node
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: "12.x"
22+
23+
- name: npm install, preview theme
24+
run: |
25+
npm install
26+
npm run preview-theme
27+
env:
28+
CI: true
29+
PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}

‎package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
"scripts": {
77
"test": "jest --coverage",
88
"test:watch": "jest --watch",
9-
"theme-readme-gen": "node scripts/generate-theme-doc"
9+
"theme-readme-gen": "node scripts/generate-theme-doc",
10+
"preview-theme": "node scripts/preview-theme"
1011
},
1112
"author": "Anurag Hazra",
1213
"license": "MIT",
1314
"devDependencies": {
15+
"@actions/core": "^1.2.4",
16+
"@actions/github": "^4.0.0",
1417
"@testing-library/dom": "^7.20.0",
1518
"@testing-library/jest-dom": "^5.11.0",
1619
"axios": "^0.19.2",
1720
"axios-mock-adapter": "^1.18.1",
1821
"css-to-object": "^1.1.0",
1922
"husky": "^4.2.5",
20-
"jest": "^26.1.0"
23+
"jest": "^26.1.0",
24+
"parse-diff": "^0.7.0"
2125
},
2226
"dependencies": {
2327
"dotenv": "^8.2.0",

‎scripts/preview-theme.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const core = require("@actions/core");
2+
const github = require("@actions/github");
3+
const parse = require("parse-diff");
4+
require("dotenv").config();
5+
6+
const parsePullRequestId = (githubRef) => {
7+
const result = /refs\/pull\/(\d+)\/merge/g.exec(githubRef);
8+
if (!result) {
9+
console.log("Reference not found.");
10+
return 297;
11+
}
12+
const [, pullRequestId] = result;
13+
return pullRequestId;
14+
};
15+
16+
async function run() {
17+
try {
18+
const octokit = github.getOctokit(process.env.PERSONAL_TOKEN);
19+
const pullRequestId = parsePullRequestId(process.env.GITHUB_REF);
20+
21+
let res = await octokit.pulls.get({
22+
owner: "anuraghazra",
23+
repo: "github-readme-stats",
24+
pull_number: pullRequestId,
25+
mediaType: {
26+
format: "diff",
27+
},
28+
});
29+
30+
let diff = parse(res.data);
31+
let colorStrings = diff
32+
.find((file) => file.to === "themes/index.js")
33+
.chunks[0].changes.filter((c) => c.type === "add")
34+
.map((c) => c.content.replace("+", ""))
35+
.join("");
36+
37+
let matches = colorStrings.match(/(title_color:.*bg_color.*\")/);
38+
let colors = matches && matches[0].split(",");
39+
40+
if (!colors) {
41+
await octokit.issues.createComment({
42+
owner: "anuraghazra",
43+
repo: "github-readme-stats",
44+
body: `
45+
\rTheme preview (bot)
46+
Cannot create theme preview
47+
`,
48+
issue_number: pullRequestId,
49+
});
50+
return;
51+
}
52+
colors = colors.map((color) =>
53+
color.replace(/.*\:\s/, "").replace(/\"/g, "")
54+
);
55+
56+
let titleColor = colors[0];
57+
let iconColor = colors[1];
58+
let textColor = colors[2];
59+
let bgColor = colors[3];
60+
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`;
61+
62+
await octokit.issues.createComment({
63+
owner: "anuraghazra",
64+
repo: "github-readme-stats",
65+
body: `
66+
\rTheme preview (bot)
67+
\r![](${url})
68+
`,
69+
issue_number: pullRequestId,
70+
});
71+
} catch (error) {
72+
core.setFailed(error.message);
73+
}
74+
}
75+
76+
run();

0 commit comments

Comments
 (0)
Please sign in to comment.