-
Notifications
You must be signed in to change notification settings - Fork 15
112 lines (96 loc) · 4.16 KB
/
check-html-metadata.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
name: Validate HTML Metadata on PRs
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
validate-metadata:
runs-on: ubuntu-latest
steps:
# Checkout the PR branch
- name: Checkout PR branch
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Validate all HTML files in the PR branch
id: validate
run: |
ERROR_MESSAGE=""
VALIDATION_PASSED=false
# Find all HTML files in the public/submissions/ directory
HTML_FILES=$(find public/submissions -name "*.html" || true)
# If no HTML files are found, skip validation
if [ -z "$HTML_FILES" ]; then
echo "No HTML files found in public/submissions/. Skipping validation."
exit 0
else
echo "Validating the following HTML files:"
echo "$HTML_FILES"
fi
# Loop through all HTML files and validate them
for file in $HTML_FILES; do
echo "Processing file: $file"
# Check for <meta name="author"> tag
AUTHOR=$(grep -Poz '(?s)<meta[^>]*name=["'\'']author["'\''][^>]*>' "$file" || true)
if [ -z "$AUTHOR" ]; then
ERROR_MESSAGE+="File $file is missing <meta name='author'> tag.\n"
fi
# Check for <title> tag
TITLE=$(grep -Poz '(?s)<title[^>]*>.*?</title>' "$file" || true)
if [ -z "$TITLE" ]; then
ERROR_MESSAGE+="File $file is missing <title> tag.\n"
fi
done
# Pass the error message to the next step
echo "ERROR_MESSAGE=$ERROR_MESSAGE" >> $GITHUB_ENV
# If there are no errors, mark validation as passed
if [ -z "$ERROR_MESSAGE" ]; then
echo "VALIDATION_PASSED=true" >> $GITHUB_ENV
else
echo "VALIDATION_PASSED=false" >> $GITHUB_ENV
exit 1 # Fail the step if validation fails
fi
# Always delete the existing validation comment (whether it passed or failed)
- name: Delete existing validation comment
uses: actions/github-script@v5
with:
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const validationComment = comments.find(comment => comment.user.login === 'github-actions[bot]' && (comment.body.includes('HTML Metadata Validation Failed') || comment.body.includes('HTML Validation Passed')));
if (validationComment) {
console.log("Deleting previous comment...");
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: validationComment.id
});
} else {
console.log("No existing comment found.");
}
# Post a new validation comment (either pass or fail)
- name: Post validation comment
uses: actions/github-script@v5
env:
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
ERROR_MESSAGE: ${{ env.ERROR_MESSAGE }}
VALIDATION_PASSED: ${{ env.VALIDATION_PASSED }}
with:
script: |
const issueNumber = context.payload.pull_request.number;
const prAuthor = `@${process.env.PR_AUTHOR}`;
let message;
if (process.env.VALIDATION_PASSED === 'true') {
message = `**HTML Validation Passed**\n\nGreat job, ${prAuthor}! All required metadata is present.`;
} else {
message = `**HTML Metadata Validation Failed:**\n\n${prAuthor}, the following issues were found:\n\n${process.env.ERROR_MESSAGE}`;
}
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
})