Skip to content

Commit

Permalink
infra: theme preview gh action (anuraghazra#296)
Browse files Browse the repository at this point in the history
* chore: testing theme preview script

* fix: workflow

* test current pr

* comment on pr script

* fix: preview theme script

* fix: preview theme
  • Loading branch information
anuraghazra authored Aug 2, 2020
1 parent 6f7e354 commit 53c4940
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/preview-theme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Theme preview

on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- master
- theme-preview-script
paths:
- "themes/index.js"

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: setup node
uses: actions/setup-node@v1
with:
node-version: "12.x"

- name: npm install, preview theme
run: |
npm install
npm run preview-theme
env:
CI: true
PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
"scripts": {
"test": "jest --coverage",
"test:watch": "jest --watch",
"theme-readme-gen": "node scripts/generate-theme-doc"
"theme-readme-gen": "node scripts/generate-theme-doc",
"preview-theme": "node scripts/preview-theme"
},
"author": "Anurag Hazra",
"license": "MIT",
"devDependencies": {
"@actions/core": "^1.2.4",
"@actions/github": "^4.0.0",
"@testing-library/dom": "^7.20.0",
"@testing-library/jest-dom": "^5.11.0",
"axios": "^0.19.2",
"axios-mock-adapter": "^1.18.1",
"css-to-object": "^1.1.0",
"husky": "^4.2.5",
"jest": "^26.1.0"
"jest": "^26.1.0",
"parse-diff": "^0.7.0"
},
"dependencies": {
"dotenv": "^8.2.0",
Expand Down
76 changes: 76 additions & 0 deletions scripts/preview-theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const core = require("@actions/core");
const github = require("@actions/github");
const parse = require("parse-diff");
require("dotenv").config();

const parsePullRequestId = (githubRef) => {
const result = /refs\/pull\/(\d+)\/merge/g.exec(githubRef);
if (!result) {
console.log("Reference not found.");
return 297;
}
const [, pullRequestId] = result;
return pullRequestId;
};

async function run() {
try {
const octokit = github.getOctokit(process.env.PERSONAL_TOKEN);
const pullRequestId = parsePullRequestId(process.env.GITHUB_REF);

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();

0 comments on commit 53c4940

Please sign in to comment.