-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvalidate-pr.js
More file actions
172 lines (151 loc) · 5.12 KB
/
Copy pathvalidate-pr.js
File metadata and controls
172 lines (151 loc) · 5.12 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
#!/usr/bin/env node
/**
* Learning Room PR Validation Script
* Validates student pull requests and provides educational feedback
* Focused on learning outcomes, not gatekeeping
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const PR_NUMBER = process.env.PR_NUMBER;
const PR_TITLE = process.env.PR_TITLE || '';
const PR_BODY = process.env.PR_BODY || '';
const PR_AUTHOR = process.env.PR_AUTHOR || '';
const results = {
passed: true,
required: [],
suggestions: [],
accessibility: [],
resources: []
};
/**
* Extract closing issue reference from PR body
*/
function extractClosingIssueReference(text) {
if (!text) return null;
const closingMatch = text.match(/(?:closes|fixes|resolves|fix|close|resolve)\s+#(\d+)/i);
if (!closingMatch) return null;
const issueNumber = Number(closingMatch[1]);
return Number.isInteger(issueNumber) && issueNumber > 0 ? issueNumber : null;
}
/**
* Validate basic PR requirements
*/
function validateBasicRequirements() {
// Check for issue reference
const hasIssueReference = extractClosingIssueReference(`${PR_TITLE}\n${PR_BODY}`) !== null;
results.required.push({
name: 'Issue Reference',
passed: hasIssueReference,
message: hasIssueReference
? 'PR is correctly linked to an issue'
: 'PR should reference an issue (use "Closes #123" in the description)',
help: !hasIssueReference
? 'Add `Closes #ISSUE_NUMBER` to your PR description to link this PR to its challenge issue.'
: null
});
if (!hasIssueReference) {
results.passed = false;
results.resources.push({
title: 'Opening a Pull Request (Chapter 06)',
url: 'https://github.com/Community-Access/git-going-with-github/blob/main/docs/06-working-with-pull-requests.md'
});
}
// Check for meaningful description
const hasDescription = PR_BODY && PR_BODY.trim().length >= 20;
results.required.push({
name: 'PR Description',
passed: hasDescription,
message: hasDescription
? 'PR has a clear description'
: 'Provide a short description of what you changed and why (at least 20 characters)',
help: !hasDescription
? 'Explain your changes in the PR description. This helps reviewers understand what you did.'
: null
});
if (!hasDescription) {
results.passed = false;
results.resources.push({
title: 'Writing a PR Description',
url: 'https://github.com/Community-Access/git-going-with-github/blob/main/docs/06-working-with-pull-requests.md'
});
}
}
/**
* Check for poor link text patterns
*/
function checkLinkQuality() {
const poorPatterns = [
{ pattern: /\[click here\]/gi, name: 'click here' },
{ pattern: /\[read more\]/gi, name: 'read more' },
{ pattern: /\[here\]/gi, name: 'here' },
{ pattern: /\[link\]/gi, name: 'link' },
{ pattern: /\[more\]/gi, name: 'more' }
];
poorPatterns.forEach(({ pattern, name }) => {
if (pattern.test(PR_BODY)) {
results.accessibility.push({
type: 'warning',
title: 'Link Text Clarity',
message: `Your PR uses vague link text ("${name}"). Screen reader users won't know where the link goes.`,
fix: `Replace "[${name}](...)" with descriptive text like "[Topic Name](URL)" so the link purpose is clear.`
});
}
});
}
/**
* Provide learning suggestions based on PR content
*/
function provideSuggestions() {
if (PR_BODY.length < 100) {
results.suggestions.push({
title: 'More Detail',
message: 'Adding more context in your PR description helps reviewers understand your thinking.',
help: 'Try explaining what problem this solves or what you learned while making the change.'
});
}
// Encourage using proper formatting in description
if (!PR_BODY.includes('##') && !PR_BODY.includes('-')) {
results.suggestions.push({
title: 'Formatting',
message: 'Using markdown formatting (like headings and lists) makes your PR easier to read.',
help: 'Try using `## Heading` or `- bullet points` in your description.'
});
}
}
/**
* Main validation orchestration
*/
function runValidation() {
validateBasicRequirements();
checkLinkQuality();
provideSuggestions();
// Add default resources
if (results.resources.length === 0) {
results.resources.push(
{
title: 'Markdown Reference (Appendix C)',
url: 'https://github.com/Community-Access/git-going-with-github/blob/main/docs/appendix-c-markdown-reference.md'
},
{
title: 'Accessibility Standards (Appendix M)',
url: 'https://github.com/Community-Access/git-going-with-github/blob/main/docs/appendix-m-accessibility-standards.md'
},
{
title: 'Challenge Hub',
url: './docs/CHALLENGES.md'
}
);
}
// Write results so the workflow can read them
fs.writeFileSync('validation-results.json', JSON.stringify(results, null, 2));
console.log('Validation complete. Results written to validation-results.json');
process.exit(results.passed ? 0 : 1);
}
// Run validation
runValidation();
module.exports = {
extractClosingIssueReference,
validateBasicRequirements,
checkLinkQuality
};