|
| 1 | +import core from "@actions/core"; |
| 2 | +import github from "@actions/github"; |
| 3 | +import { generateImage } from "./generateImage.js"; |
| 4 | + |
| 5 | +const context = github.context; |
| 6 | +const owner = context.repo.owner; |
| 7 | +const repo = context.repo.repo; |
| 8 | + |
| 9 | +const token = core.getInput("token", { required: true }); |
| 10 | +const baseBranch = "main"; |
| 11 | + |
| 12 | +const octokit = github.getOctokit(token); |
| 13 | +const newBranch = "update-contributors-png"; |
| 14 | +const filePath = "contributors.png"; |
| 15 | + |
| 16 | +const res = await fetch( |
| 17 | + `https://raw.githubusercontent.com/${owner}/${repo}/${baseBranch}/.all-contributorsrc`, |
| 18 | +); |
| 19 | +if (!res.ok) { |
| 20 | + throw new Error("failed to fetch .all-contributorsrc"); |
| 21 | +} |
| 22 | +const data = await res.json(); |
| 23 | +const contributors = data.contributors; |
| 24 | + |
| 25 | +const contentBuffer = await generateImage(contributors); |
| 26 | +const content = contentBuffer.toString("base64"); |
| 27 | + |
| 28 | +// get base branch sha |
| 29 | +const { data: baseRef } = await octokit.rest.git.getRef({ |
| 30 | + owner, |
| 31 | + repo, |
| 32 | + ref: `heads/${baseBranch}`, |
| 33 | +}); |
| 34 | +const baseSha = baseRef.object.sha; |
| 35 | + |
| 36 | +// create branch (or reuse if exists) |
| 37 | +try { |
| 38 | + await octokit.rest.git.createRef({ |
| 39 | + owner, |
| 40 | + repo, |
| 41 | + ref: `refs/heads/${newBranch}`, |
| 42 | + sha: baseSha, |
| 43 | + }); |
| 44 | +} catch (e) { |
| 45 | + // update branch, if already exists |
| 46 | + await octokit.rest.git.updateRef({ |
| 47 | + owner, |
| 48 | + repo, |
| 49 | + ref: `heads/${newBranch}`, |
| 50 | + sha: baseSha, |
| 51 | + force: true, |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +// check if file already exists on branch |
| 56 | +let existingSha = undefined; |
| 57 | +try { |
| 58 | + const { data } = await octokit.rest.repos.getContent({ |
| 59 | + owner, |
| 60 | + repo, |
| 61 | + path: filePath, |
| 62 | + ref: newBranch, |
| 63 | + }); |
| 64 | + |
| 65 | + if (!Array.isArray(data) && data.sha) { |
| 66 | + existingSha = data.sha; |
| 67 | + } |
| 68 | +} catch { |
| 69 | + // file does not exist |
| 70 | +} |
| 71 | + |
| 72 | +// create/update file and commit |
| 73 | +try { |
| 74 | + await octokit.rest.repos.createOrUpdateFileContents({ |
| 75 | + owner, |
| 76 | + repo, |
| 77 | + path: filePath, |
| 78 | + message: "Update contributors.png from .all-contributorsrc", |
| 79 | + content, |
| 80 | + branch: newBranch, |
| 81 | + ...(existingSha && { sha: existingSha }), |
| 82 | + }); |
| 83 | +} catch (e) { |
| 84 | + core.setFailed(e.message); |
| 85 | +} |
| 86 | + |
| 87 | +// create pull request if not exists |
| 88 | +const { data: prs } = await octokit.rest.pulls.list({ |
| 89 | + owner, |
| 90 | + repo, |
| 91 | + head: `${owner}:${newBranch}`, |
| 92 | + base: baseBranch, |
| 93 | + state: "open", |
| 94 | +}); |
| 95 | +if (prs.length === 0) { |
| 96 | + await octokit.rest.pulls.create({ |
| 97 | + owner, |
| 98 | + repo, |
| 99 | + title: "chore: update contributors.png from .all-contributorsrc", |
| 100 | + head: newBranch, |
| 101 | + base: baseBranch, |
| 102 | + body: "This PR updates the contributors.png to reflect changes in .all-contributorsrc", |
| 103 | + }); |
| 104 | +} |
0 commit comments