-
Notifications
You must be signed in to change notification settings - Fork 10
197 lines (168 loc) · 7.24 KB
/
check_pr_template.yml
File metadata and controls
197 lines (168 loc) · 7.24 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
name: Check if PR Template Is Complete
on:
pull_request:
types: [opened, edited, synchronize]
jobs:
check_template:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm install js-yaml
- name: Check PR template and create submission file
id: check_template
uses: actions/github-script@v6
with:
script: |
const fs = require('fs').promises;
const yaml = require('js-yaml');
const prBody = context.payload.pull_request.body || '';
// Normalize line endings
const normalizedBody = prBody.replace(/\r\n/g, '\n');
// Extract and parse YAML
const startMarker = '```yaml\n';
const endMarker = '\n```';
const startIndex = normalizedBody.indexOf(startMarker);
if (startIndex === -1) {
console.log('Could not find start marker');
core.setFailed('Start marker not found');
return;
}
const contentStart = startIndex + startMarker.length;
const endIndex = normalizedBody.indexOf(endMarker, contentStart);
if (endIndex === -1) {
console.log('Could not find end marker');
core.setFailed('End marker not found');
return;
}
const yamlContent = normalizedBody.slice(contentStart, endIndex);
console.log('Extracted YAML content:', yamlContent);
let data;
try {
// Remove comments from YAML content
const cleanYaml = yamlContent
.split('\n')
.map(line => line.split('#')[0].trim())
.join('\n');
console.log('Cleaned YAML content:', cleanYaml);
data = yaml.load(cleanYaml);
console.log('Parsed YAML data:', data);
} catch (error) {
console.log('YAML parsing error:', error);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: `:warning: Error parsing YAML: ${error.message}`
});
core.setFailed(`Invalid YAML: ${error.message}`);
return;
}
// Validate required fields
const requiredFields = [
'submission_name',
'submission_folder',
'authors',
'affiliations',
'ruleset',
'framework',
'description'
];
const emptyFields = requiredFields.filter(field => {
const value = data?.[field];
return !value ||
value.toString().trim() === '' ||
value === '""' ||
value === '\"\"';
});
if (emptyFields.length > 0) {
const emptyFieldsList = emptyFields
.map(field => ` - ${field} is empty`)
.join('\n');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: `:warning: Please fill out all required fields in the PR template:\n\n${emptyFieldsList}`
});
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
labels: ['🚧 Incomplete']
});
core.setFailed('Empty fields found');
return;
}
// Remove incomplete label if present
try {
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number
});
if (labels.some(label => label.name === '🚧 Incomplete')) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
name: '🚧 Incomplete'
});
}
} catch (error) {
console.log('Error handling labels:', error);
}
core.setOutput('filled_out', 'true');
core.setOutput('submission_data', data);
// Create submission_info.yml
try {
let submissionPath;
// Remove any leading/trailing slashes and potential duplicate paths
const cleanFolder = data.submission_folder.replace(/^\/+|\/+$/g, '').replace(/^(external_tuning|self_tuning)\//, '');
if (data.ruleset === 'external') {
submissionPath = `submissions/external_tuning/${cleanFolder}`;
} else if (data.ruleset === 'self-tuning') {
submissionPath = `submissions/self_tuning/${cleanFolder}`;
} else {
core.setFailed(`Invalid ruleset value: ${data.ruleset}. Must be "external" or "self-tuning".`);
return;
}
// Check if directory exists, create if it doesn't
try {
await fs.mkdir(submissionPath, { recursive: true });
} catch (error) {
console.log('Error creating directory:', error);
core.setFailed(`Failed to create directory: ${error.message}`);
return;
}
// Write the YAML file
const yamlStr = yaml.dump(data);
const filePath = `${submissionPath}/submission_info.yml`;
await fs.writeFile(filePath, yamlStr);
console.log('Created submission_info.yml');
} catch (error) {
console.log('Error creating submission file:', error);
core.setFailed(`Failed to create submission file: ${error.message}`);
return;
}
- name: Commit and push if changed
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add submissions/*/*/*
# Check if there are changes to commit
git diff --staged --quiet || (
git commit -m "Add/Update submission_info.yml" -m "Automated commit by GitHub Action"
git push
)