GildedRose-Refactoring-Kata/.github/workflows/pr-validation.yml
2026-02-05 21:26:45 +01:00

68 lines
2.6 KiB
YAML

name: "PR Tasks Completed Check"
on:
pull_request:
types: opened
jobs:
task-check:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
# github.event.repository.name only contains 'GildedRose-Refactoring-Kata' (no owner)!
# we could check owner and name if: github.repository == 'emilybache/GildedRose-Refactoring-Kata'
# but checking owner is enough/better/more stable
# Only run from the base repository
- name: Skip if not main repo or fork testing
if: ${{ github.repository_owner != 'emilybache' && env.PR_TESTING != 'true' }}
run: |
echo "Skipping workflow on fork without PR_TESTING"
exit 0
- name: Comment if checkmark is missing
if: ${{ !contains(github.event.pull_request.body, '[X] I acknowledge') }}
uses: actions/github-script@v6
with:
script: |
- uses: actions/checkout@v3
- name: Check PR template via diff
id: check
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
const diff = require('diff');
const prBody = context.payload.pull_request.body || "";
// Read the PR template
const templatePath = path.join(process.env.GITHUB_WORKSPACE, ".github/pull_request_template.md");
const template = fs.readFileSync(templatePath, "utf8");
// Transform unchecked boxes to checked boxes
const transformedTemplate = template.replace(/- \[ \]/g, '- [x]');
const normalize = text => text.replace(/\r\n/g, '\n').trim();
const bodyNormalized = normalize(prBody);
const templateNormalized = normalize(transformedTemplate);
// Compute line diff
const changes = diff.diffLines(templateNormalized, bodyNormalized);
// Filter out lines that are identical
const diffLines = changes.filter(c => c.added || c.removed).map(c => c.value.trim()).filter(Boolean);
// Check if diff is empty or only contains the last line of the template
const lastLine = normalize(transformedTemplate.split('\n').slice(-1)[0]);
const isSuspect = diffLines.length === 0 || (diffLines.length === 1 && diffLines[0] === lastLine);
return { is_suspect: isSuspect };
- name: Add solution label
if: steps.check.outputs.is_suspect == 'true'
uses: actions-ecosystem/action-add-labels@v1
with:
labels: solution