-
Notifications
You must be signed in to change notification settings - Fork 1
Add contributors image generation action #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| # Created by https://www.toptal.com/developers/gitignore/api/node | ||
| # Edit at https://www.toptal.com/developers/gitignore?templates=node | ||
|
|
||
| ### Node ### | ||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| lerna-debug.log* | ||
| .pnpm-debug.log* | ||
|
|
||
| # Diagnostic reports (https://nodejs.org/api/report.html) | ||
| report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
|
||
| # Runtime data | ||
| pids | ||
| *.pid | ||
| *.seed | ||
| *.pid.lock | ||
|
|
||
| # Directory for instrumented libs generated by jscoverage/JSCover | ||
| lib-cov | ||
|
|
||
| # Coverage directory used by tools like istanbul | ||
| coverage | ||
| *.lcov | ||
|
|
||
| # nyc test coverage | ||
| .nyc_output | ||
|
|
||
| # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
| .grunt | ||
|
|
||
| # Bower dependency directory (https://bower.io/) | ||
| bower_components | ||
|
|
||
| # node-waf configuration | ||
| .lock-wscript | ||
|
|
||
| # Compiled binary addons (https://nodejs.org/api/addons.html) | ||
| build/Release | ||
|
|
||
| # Dependency directories | ||
| node_modules/ | ||
| jspm_packages/ | ||
|
|
||
| # Snowpack dependency directory (https://snowpack.dev/) | ||
| web_modules/ | ||
|
|
||
| # TypeScript cache | ||
| *.tsbuildinfo | ||
|
|
||
| # Optional npm cache directory | ||
| .npm | ||
|
|
||
| # Optional eslint cache | ||
| .eslintcache | ||
|
|
||
| # Optional stylelint cache | ||
| .stylelintcache | ||
|
|
||
| # Microbundle cache | ||
| .rpt2_cache/ | ||
| .rts2_cache_cjs/ | ||
| .rts2_cache_es/ | ||
| .rts2_cache_umd/ | ||
|
|
||
| # Optional REPL history | ||
| .node_repl_history | ||
|
|
||
| # Output of 'npm pack' | ||
| *.tgz | ||
|
|
||
| # Yarn Integrity file | ||
| .yarn-integrity | ||
|
|
||
| # dotenv environment variable files | ||
| .env | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
| .env.local | ||
|
|
||
| # parcel-bundler cache (https://parceljs.org/) | ||
| .cache | ||
| .parcel-cache | ||
|
|
||
| # Next.js build output | ||
| .next | ||
| out | ||
|
|
||
| # Nuxt.js build / generate output | ||
| .nuxt | ||
|
|
||
| # Gatsby files | ||
| .cache/ | ||
| # Comment in the public line in if your project uses Gatsby and not Next.js | ||
| # https://nextjs.org/blog/next-9-1#public-directory-support | ||
| # public | ||
|
|
||
| # vuepress build output | ||
| .vuepress/dist | ||
|
|
||
| # vuepress v2.x temp and cache directory | ||
| .temp | ||
|
|
||
| # Docusaurus cache and generated files | ||
| .docusaurus | ||
|
|
||
| # Serverless directories | ||
| .serverless/ | ||
|
|
||
| # FuseBox cache | ||
| .fusebox/ | ||
|
|
||
| # DynamoDB Local files | ||
| .dynamodb/ | ||
|
|
||
| # TernJS port file | ||
| .tern-port | ||
|
|
||
| # Stores VSCode versions used for testing VSCode extensions | ||
| .vscode-test | ||
|
|
||
| # yarn v2 | ||
| .yarn/cache | ||
| .yarn/unplugged | ||
| .yarn/build-state.yml | ||
| .yarn/install-state.gz | ||
| .pnp.* | ||
|
|
||
| ### Node Patch ### | ||
| # Serverless Webpack directories | ||
| .webpack/ | ||
|
|
||
| # Optional stylelint cache | ||
|
|
||
| # SvelteKit build / generate output | ||
| .svelte-kit | ||
|
|
||
| # End of https://www.toptal.com/developers/gitignore/api/node |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,47 @@ | ||
| # all-contributors-image | ||
| Generates an image of the icons of all users in all-contributors list | ||
|
|
||
| Generates a contributors image (`contributors.png`) from the `.all-contributorsrc` file and opens a pull request with the updated image. | ||
|
|
||
| ## What it does | ||
|
|
||
| - Fetches `.all-contributorsrc` from the repository | ||
| - Extracts contributor avatars | ||
| - Generates a grid image (`contributors.png`) | ||
| - Creates/updates a branch | ||
| - Commits the image | ||
| - Opens a pull request | ||
|
|
||
| ## Usage | ||
|
|
||
| ```yaml | ||
| name: Generate Contributors Image | ||
|
|
||
| on: | ||
| push: | ||
| paths: | ||
| - .all-contributorsrc | ||
|
|
||
| jobs: | ||
| generate: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Generate contributors image on main branch | ||
| uses: processing/all-contributors-image@v1 | ||
| with: | ||
| token: ${{ secrets.GITHUB_TOKEN }} | ||
| ``` | ||
|
|
||
| ## Inputs | ||
|
|
||
| | name | required | description | | ||
| | ------ | -------- | ------------------------------------------------------------------------------------ | | ||
| | token | yes | github token used for api calls | | ||
|
|
||
| ## Requirements | ||
|
|
||
| Repository must have: | ||
|
|
||
| - A valid `.all-contributorsrc` file | ||
| - `GITHUB_TOKEN` must be configured with the appropriate workflow permissions: | ||
| - enable read and write permissions | ||
| - enable allow github actions to create and approve pull requests | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| name: Generate Contributors PNG | ||
| description: Github action to generate an image of the icons of all users in all-contributors list | ||
| author: p5.js contributors & The Processing Foundation | ||
|
|
||
| inputs: | ||
| token: | ||
| description: GitHub token | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - run: npm ci | ||
| shell: bash | ||
| working-directory: ${{ github.action_path }} | ||
|
|
||
| - run: node index.js | ||
| shell: bash | ||
| working-directory: ${{ github.action_path }} | ||
| env: | ||
| INPUT_TOKEN: ${{ inputs.token }} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { createCanvas, loadImage } from "canvas"; | ||
|
|
||
| export async function generateImage(contributors) { | ||
| const AVATAR_SIZE = 50; | ||
| const GAP = 4; | ||
| const COLS = 40; | ||
| const ROWS = Math.ceil(contributors.length / COLS); | ||
|
|
||
| const width = COLS * AVATAR_SIZE + (COLS - 1) * GAP; | ||
| const height = ROWS * AVATAR_SIZE + (ROWS - 1) * GAP; | ||
|
|
||
| const canvas = createCanvas(width, height); | ||
| const ctx = canvas.getContext("2d"); | ||
|
|
||
| for (let i = 0; i < contributors.length; i++) { | ||
| const c = contributors[i]; | ||
|
|
||
| const col = i % COLS; | ||
| const row = Math.floor(i / COLS); | ||
|
|
||
| const x = col * (AVATAR_SIZE + GAP); | ||
| const y = row * (AVATAR_SIZE + GAP); | ||
|
|
||
| const img = await loadAvatar(c.avatar_url); | ||
|
skyash-dev marked this conversation as resolved.
|
||
|
|
||
| ctx.save(); | ||
| ctx.beginPath(); | ||
| ctx.arc( | ||
| x + AVATAR_SIZE / 2, | ||
| y + AVATAR_SIZE / 2, | ||
| AVATAR_SIZE / 2, | ||
| 0, | ||
| Math.PI * 2, | ||
| ); | ||
| ctx.clip(); | ||
|
|
||
| if (img) { | ||
| ctx.drawImage(img, x, y, AVATAR_SIZE, AVATAR_SIZE); | ||
| } | ||
| ctx.restore(); | ||
| } | ||
| return canvas.toBuffer("image/png"); | ||
| } | ||
|
|
||
| async function loadAvatar(url) { | ||
| try { | ||
| const res = await fetch(url); | ||
| if (!res.ok) throw new Error(`HTTP ${res.status}`); | ||
|
|
||
| const buffer = Buffer.from(await res.arrayBuffer()); | ||
| return await loadImage(buffer); | ||
| } catch (err) { | ||
| return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent failure (
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have tested the workflow multiple times and all images are successfully fetched consistently. and if in some case any fails, keeping that blank is a good default i think? |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import core from "@actions/core"; | ||
| import github from "@actions/github"; | ||
| import { generateImage } from "./generateImage.js"; | ||
|
|
||
| const context = github.context; | ||
| const owner = context.repo.owner; | ||
| const repo = context.repo.repo; | ||
|
|
||
| const token = core.getInput("token", { required: true }); | ||
| const baseBranch = "main"; | ||
|
|
||
| const octokit = github.getOctokit(token); | ||
| const newBranch = "update-contributors-png"; | ||
| const filePath = "contributors.png"; | ||
|
|
||
| const res = await fetch( | ||
| `https://raw.githubusercontent.com/${owner}/${repo}/${baseBranch}/.all-contributorsrc`, | ||
| ); | ||
| if (!res.ok) { | ||
| throw new Error("failed to fetch .all-contributorsrc"); | ||
| } | ||
| const data = await res.json(); | ||
| const contributors = data.contributors; | ||
|
|
||
| const contentBuffer = await generateImage(contributors); | ||
|
skyash-dev marked this conversation as resolved.
|
||
| let existingBuffer = null; | ||
|
|
||
| try { | ||
| const existing = await fetch( | ||
| `https://raw.githubusercontent.com/${owner}/${repo}/${baseBranch}/contributors.png`, | ||
| ); | ||
|
|
||
| if (existing.ok) { | ||
| const arrayBuffer = await existing.arrayBuffer(); | ||
| existingBuffer = Buffer.from(arrayBuffer); | ||
| } | ||
| } catch { | ||
| // file doesn't exist | ||
| } | ||
|
|
||
| if(existingBuffer && contentBuffer.equals(existingBuffer)) process.exit(0); | ||
|
|
||
| const content = contentBuffer.toString("base64"); | ||
| // get base branch sha | ||
| const { data: baseRef } = await octokit.rest.git.getRef({ | ||
| owner, | ||
| repo, | ||
| ref: `heads/${baseBranch}`, | ||
| }); | ||
| const baseSha = baseRef.object.sha; | ||
|
|
||
| // create branch (or reuse if exists) | ||
| try { | ||
| await octokit.rest.git.createRef({ | ||
| owner, | ||
| repo, | ||
| ref: `refs/heads/${newBranch}`, | ||
| sha: baseSha, | ||
| }); | ||
| } catch (e) { | ||
| // update branch, if already exists | ||
| await octokit.rest.git.updateRef({ | ||
| owner, | ||
| repo, | ||
| ref: `heads/${newBranch}`, | ||
| sha: baseSha, | ||
| force: true, | ||
|
skyash-dev marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
|
||
| // check if file already exists on branch | ||
| let existingSha = undefined; | ||
|
skyash-dev marked this conversation as resolved.
|
||
| try { | ||
| const { data } = await octokit.rest.repos.getContent({ | ||
| owner, | ||
| repo, | ||
| path: filePath, | ||
| ref: newBranch, | ||
| }); | ||
|
|
||
| if (!Array.isArray(data) && data.sha) { | ||
| existingSha = data.sha; | ||
| } | ||
| } catch { | ||
| // file does not exist | ||
| } | ||
|
|
||
| // create/update file and commit | ||
| try { | ||
| await octokit.rest.repos.createOrUpdateFileContents({ | ||
|
skyash-dev marked this conversation as resolved.
|
||
| owner, | ||
| repo, | ||
| path: filePath, | ||
| message: "Update contributors.png from .all-contributorsrc", | ||
| content, | ||
| branch: newBranch, | ||
| ...(existingSha && { sha: existingSha }), | ||
| }); | ||
| } catch (e) { | ||
| core.setFailed(e.message); | ||
| } | ||
|
|
||
| // create pull request if not exists | ||
| const { data: prs } = await octokit.rest.pulls.list({ | ||
| owner, | ||
| repo, | ||
| head: `${owner}:${newBranch}`, | ||
| base: baseBranch, | ||
| state: "open", | ||
| }); | ||
| if (prs.length === 0) { | ||
| await octokit.rest.pulls.create({ | ||
| owner, | ||
| repo, | ||
| title: "chore: update contributors.png from .all-contributorsrc", | ||
| head: newBranch, | ||
| base: baseBranch, | ||
| body: "This PR updates the contributors.png to reflect changes in .all-contributorsrc", | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.