|
| 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 |
| 68 | + `, |
| 69 | + issue_number: pullRequestId, |
| 70 | + }); |
| 71 | + } catch (error) { |
| 72 | + core.setFailed(error.message); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +run(); |
0 commit comments