Skip to content

Commit

Permalink
Split workflows (#16)
Browse files Browse the repository at this point in the history
Previously I had in the readme instructions `Make sure to check "Allow edits by maintainers" on your fork PR`, but I forgot that [this option is disabled for organizations](https://github.com/orgs/community/discussions/5634). (We can't have users create branches directly because then every time a new person submits a PR they'd have to request to become a collaborator in the repo, which will get tiring.) Without the "Allow edits" checkbox, Actions workflows do not have permissions to make commits to the fork (by default; I think a user could enable it in their fork settings). As such, this PR splits workflows into read (check-only) and write (auto-fix). The former runs on PRs, the latter runs on `main` where it has the permissions to do so. This PR also switches from the exiftool CLI to the npm package wrapper for the CLI tool for a bit nicer ergonomics.
  • Loading branch information
vincerubinetti authored Jun 11, 2024
1 parent e323d75 commit e507976
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 40 deletions.
47 changes: 19 additions & 28 deletions .github/check.js → .github/check.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { readdirSync, readFileSync, existsSync } = require("fs");
const { execSync } = require("child_process");
import { readdirSync, readFileSync, existsSync } from "fs";
import { ExifTool } from "exiftool-vendored";

// strip all metadata to ensure consistency
exec("exiftool -All= -r -overwrite_original ./images");
exec("exiftool -All= -r -overwrite_original ./print");
const exiftool = new ExifTool();

checkDimensions("./images", 600, 600);
checkDimensions("./print", 1200, 1200);
await checkDimensions("./images", 600, 600);
await checkDimensions("./print", 1200, 1200);

checkList("software.json");
checkList("groups.json");

await exiftool.end();

// check list of entries in json file
function checkList(filename) {
console.info(`Checking list "${filename}"`);
Expand Down Expand Up @@ -41,29 +41,27 @@ function checkList(filename) {
}
}

// check dimensions of images in directory
function checkDimensions(
directory,
// check dimensions of images in folder
async function checkDimensions(
folder,
expectedWidth,
expectedHeight,
extension = "png"
) {
// get all images matching extension in directory
const paths = readdirSync(directory)
// get all images matching extension in folder
const paths = readdirSync(folder)
.filter((filename) => filename.endsWith(`.${extension}`))
.map((filename) => `${directory}/${filename}`);
.map((filename) => `${folder}/${filename}`);

for (const [index, path] of Object.entries(paths)) {
console.info(`Checking "${path}" (${+index + 1} of ${paths.length})`);

// extract dimensions
const [width, height] = exec(
`exiftool -s3 -ImageWidth -ImageHeight ${path}`,
false
)
.split(/\s/)
.filter(Boolean)
.map(Number);
// extract metadata
const {
ImageWidth: width,
ImageHeight: height,
ColorType: colorspace,
} = await exiftool.read(path);

// check dimensions
if (width !== expectedWidth || height !== expectedHeight)
Expand All @@ -72,14 +70,7 @@ function checkDimensions(
);

// check color space
const colorspace = exec(`exiftool -s3 -ColorType ${path}`, false);
if (!colorspace.toLowerCase().includes("rgb"))
throw Error(`Colorspace "${colorspace}", expected RGB`);
}
}

function exec(command, print = true) {
const result = execSync(command).toString().trim();
if (print) console.log(result);
return result;
}
17 changes: 17 additions & 0 deletions .github/clean.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ExifTool } from "exiftool-vendored";

const exiftool = new ExifTool();

await stripMeta("./images");
await stripMeta("./print");

await exiftool.end();

// strip all metadata from all images in folder to ensure consistency
async function stripMeta(folder) {
await exiftool.write(folder, {}, [
"-All=",
"-recurse",
"-overwrite_original",
]);
}
21 changes: 11 additions & 10 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Check and clean files
name: Check files

on: pull_request
on:
pull_request:
branches:
- main

jobs:
check:
Expand All @@ -11,13 +14,11 @@ jobs:
with:
ref: ${{ github.head_ref }}

- name: Install exiftool
run: sudo apt install exiftool
- name: Install packages
run: npm install exiftool-vendored@26

- name: Run script
run: node ./.github/check.js
- if: runner.debug == '1'
uses: mxschmitt/action-tmate@v3

- name: Commit changed files
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Update files"
- name: Run script
run: node ./.github/check.mjs
27 changes: 27 additions & 0 deletions .github/workflows/clean.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Clean files

on:
push:
branches:
- main

jobs:
clean:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install packages
run: npm install exiftool-vendored@26

- if: runner.debug == '1'
uses: mxschmitt/action-tmate@v3

- name: Run script
run: node ./.github/clean.mjs

- name: Commit changed files
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Clean files"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
package.json
package-lock.json
.DS_STORE
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ The order of entries on the website is shuffled randomly on each page visit to n

1. [Fork this repo](https://github.dev/CU-DBMI/wall-of-software) and make one or more changes.
1. Open a pull request (PR).
Check "Allow edits by maintainers".
Name the PR the canonical name(s) of the software/group(s) you're changing, using commas for multiple, e.g. "Word Lapse, Preprint Similarity Search".
1. Shortly after opening the PR, a link will appear that shows a preview of the website with your changes (if any).
Add `?print` at the end of the url to view the print versions of images instead.
Expand Down Expand Up @@ -59,7 +58,7 @@ To remove/change entries, edit the above files as needed.

## Images

Images should meet the following standards:
Images must meet the following standards:

- Unique, visually appealing logo or other graphical representation of your software/group.
- Not up-scaled, stretched, or otherwise blurry/noisy/distorted/etc.
Expand Down

0 comments on commit e507976

Please sign in to comment.