Skip to content
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

season 1 day... 3? i forgor #387

Merged
merged 12 commits into from
Jan 7, 2025
143 changes: 143 additions & 0 deletions .github/workflows/ensure-valid-html-css.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
name: HTML and CSS Validation

on:
pull_request:
paths:
- '**/*.html'
- '**/*.htm'
- '**/*.xhtml'
- '**/*.xht'

jobs:
validate-html:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get changed files
id: changed-files
run: |
FILES=$(git diff --name-only origin/${{ github.base_ref }} ${{ github.sha }} | grep -E '\.(html|htm|xhtml|xht|css)$' || true)
echo "$FILES" > changed_files.txt
if [ -s changed_files.txt ]; then
echo "has_changes=true" >> $GITHUB_OUTPUT
else
echo "has_changes=false" >> $GITHUB_OUTPUT
fi

- name: Set up Java
if: steps.changed-files.outputs.has_changes == 'true'
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'

- name: Download vnu.jar
if: steps.changed-files.outputs.has_changes == 'true'
run: |
wget -O vnu.jar https://github.com/validator/validator/releases/download/latest/vnu.jar

- name: Validate HTML and CSS
if: steps.changed-files.outputs.has_changes == 'true'
id: validation
continue-on-error: true
run: |
mkdir -p validation
ERROR_LOG="validation/errors.txt"
touch $ERROR_LOG

while IFS= read -r file; do
if [ -f "$file" ]; then
echo "Validating file: $file"
java -jar vnu.jar --format json --also-check-css "$file" >> $ERROR_LOG 2>&1 || true
fi
done < changed_files.txt

if [ -s "$ERROR_LOG" ]; then
echo "has_errors=true" >> $GITHUB_OUTPUT
else
echo "has_errors=false" >> $GITHUB_OUTPUT
fi

- name: Post validation results
if: steps.changed-files.outputs.has_changes == 'true'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const hasErrors = '${{ steps.validation.outputs.has_errors }}' === 'true';
let commentBody = '## HTML Validation Results\n\n';

if (hasErrors) {
const rawErrors = fs.readFileSync('validation/errors.txt', 'utf8');
let errors;
try {
errors = JSON.parse(rawErrors);
} catch (e) {
// Fallback for non-JSON output
commentBody += '❌ Validation found the following issues:\n\n```\n' + rawErrors + '\n```\n';
}

if (errors && errors.messages) {
commentBody += '❌ Validation found the following issues:\n\n';

// Group errors by file
const errorsByFile = {};
errors.messages.forEach(msg => {
const file = msg.url.split('/').pop();
if (!errorsByFile[file]) {
errorsByFile[file] = [];
}
errorsByFile[file].push(msg);
});

// Format errors by file
for (const [file, messages] of Object.entries(errorsByFile)) {
commentBody += `### ${file}\n\n`;
messages.forEach(msg => {
const type = msg.type === 'error' ? '🔴' : '⚠️';
const location = `Line ${msg.lastLine}, Column ${msg.lastColumn}`;
commentBody += `${type} **${location}:** ${msg.message}\n\n`;
});
}
}

commentBody += '\nPlease fix these issues before merging.';
} else {
commentBody += '✅ All HTML and CSS validates successfully!';
}

const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});

const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('HTML Validation Results')
);

if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentBody
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody
});
}

- name: Exit with error if validation failed
if: steps.validation.outputs.has_errors == 'true'
run: exit 1
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"[html]": {
"editor.suggest.insertMode": "replace",
"editor.defaultFormatter": "vscode.html-language-features",
}
}
1 change: 1 addition & 0 deletions LIVE/.vscode
Loading
Loading