Repository navigation
74 lines (60 loc) · 2.56 KB
/
Copy pathvalidate_pr.yml
File metadata and controls
74 lines (60 loc) · 2.56 KB
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
name: Validate PR Template
on:
pull_request:
types: [opened, edited, synchronize]
jobs:
validate:
# This ensures the script ONLY runs if "[submission]" is in the title
if: contains(github.event.pull_request.title, '[submission]')
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm install js-yaml
- name: Validate YAML
uses: actions/github-script@v6
with:
script: |
const yaml = require('js-yaml');
const prBody = context.payload.pull_request.body || '';
// 1. Extraction
const normalizedBody = prBody.replace(/\r\n/g, '\n');
const startMarker = '```yaml\n';
const endMarker = '\n```';
const startIndex = normalizedBody.indexOf(startMarker);
const contentStart = startIndex + startMarker.length;
const endIndex = normalizedBody.indexOf(endMarker, contentStart);
if (startIndex === -1 || endIndex === -1) {
core.setFailed('No YAML block found. Please use the [submission] template.');
return;
}
// 2. Parsing & Field Check
let data;
try {
const yamlContent = normalizedBody.slice(contentStart, endIndex);
const cleanYaml = yamlContent.split('\n').map(l => l.split('#')[0].trim()).join('\n');
data = yaml.load(cleanYaml);
} catch (e) {
core.setFailed(`YAML Syntax Error: ${e.message}`);
return;
}
const requiredFields = ['submission_name', 'submission_folder', 'authors', 'ruleset'];
const emptyFields = requiredFields.filter(f => !data?.[f] || data[f].toString().trim() === '');
if (emptyFields.length > 0) {
const errorMsg = `⚠️ Missing required fields: ${emptyFields.join(', ')}`;
// Post a comment so the user knows exactly what to fix
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: errorMsg
});
core.setFailed(errorMsg);
}