Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/scripts/validate-pr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Octokit } from "@octokit/rest";
import {
isCowJsonFile,
isValidatorMaintenancePullRequest
} from "./validation-helpers.js";

// Environment variables
const token = process.env.GITHUB_TOKEN;
Expand Down Expand Up @@ -45,7 +49,12 @@ async function run() {
pull_number: pr_number
});

const jsonFiles = files.filter(f => f.filename.endsWith(".json"));
if (isValidatorMaintenancePullRequest(files)) {
console.log("✅ Validator maintenance PR detected; cow submission checks skipped.");
process.exit(0);
}

const jsonFiles = files.filter(f => isCowJsonFile(f.filename));
const imageFiles = files.filter(f => f.filename.startsWith("images/"));
const requiredKeys = ["name", "breed", "image"];

Expand Down
15 changes: 15 additions & 0 deletions .github/scripts/validation-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function isCowJsonFile(filename) {
return filename.startsWith("cows/") && filename.endsWith(".json");
}

export function isValidatorMaintenancePullRequest(files) {
const allowedFiles = new Set([
".github/workflows/validate-pr.yml",
"package-lock.json",
"package.json"
]);

return files.length > 0 && files.every(({ filename }) =>
filename.startsWith(".github/scripts/") || allowedFiles.has(filename)
);
}
43 changes: 43 additions & 0 deletions .github/scripts/validation-helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from "node:assert/strict";
import test from "node:test";

import {
isCowJsonFile,
isValidatorMaintenancePullRequest
} from "./validation-helpers.js";

test("accepts JSON files in the cows directory", () => {
assert.equal(isCowJsonFile("cows/moonlight.json"), true);
});

test("rejects JSON files outside the cows directory", () => {
assert.equal(isCowJsonFile("moonlight.json"), false);
assert.equal(isCowJsonFile("examples/moonlight.json"), false);
});

test("rejects non-JSON files", () => {
assert.equal(isCowJsonFile("cows/moonlight.png"), false);
});

test("identifies validator maintenance pull requests", () => {
const files = [
{ filename: ".github/scripts/validate-pr.js" },
{ filename: ".github/scripts/validation-helpers.test.js" },
{ filename: ".github/workflows/validate-pr.yml" },
{ filename: "package.json" }
];

assert.equal(isValidatorMaintenancePullRequest(files), true);
});

test("does not skip cow submissions or unrelated changes", () => {
assert.equal(isValidatorMaintenancePullRequest([]), false);
assert.equal(
isValidatorMaintenancePullRequest([{ filename: "cows/moonlight.json" }]),
false
);
assert.equal(
isValidatorMaintenancePullRequest([{ filename: "README.md" }]),
false
);
});
3 changes: 3 additions & 0 deletions .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Run validator tests
run: npm test

- name: Validate pull request
run: node .github/scripts/validate-pr.js
env:
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "fantasy-cow",
"version": "1.0.0",
"description": "A small project that teaches how to create a first Pull Request.",
"type": "module",
"scripts": {
"test": "node --test .github/scripts/*.test.js"
},
"dependencies": {
"@octokit/rest": "^22.0.0"
}
Expand Down
Loading