-
Notifications
You must be signed in to change notification settings - Fork 81
222 lines (202 loc) · 9.96 KB
/
Copy pathcheck-contributor-format.yml
File metadata and controls
222 lines (202 loc) · 9.96 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
name: Check Contributor Format
on:
pull_request:
types: [opened, synchronize, reopened, edited]
paths:
- 'CONTRIBUTORS.md'
permissions:
pull-requests: write
contents: read
issues: write
jobs:
check-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check contributor format
uses: actions/github-script@v7
env:
BASE_REF: ${{ github.base_ref }}
with:
script: |
const fs = require('fs');
const { execSync } = require('child_process');
const prTitle = context.payload.pull_request.title;
const prNumber = context.payload.pull_request.number;
const prAuthor = context.payload.pull_request.user.login;
// ── Read file ──────────────────────────────────────────────────
const content = fs.readFileSync('CONTRIBUTORS.md', 'utf8');
// ── Get diff (lines added by this PR) ──────────────────────────
let addedLines = '';
try {
execSync(`git fetch origin ${process.env.BASE_REF}`, { stdio: 'pipe' });
const raw = execSync(
`git diff origin/${process.env.BASE_REF} -- CONTRIBUTORS.md`,
{ stdio: 'pipe' }
).toString();
addedLines = raw.split('\n').filter(l => l.startsWith('+')).join('\n');
} catch (_) { /* fall back to whole-file check */ }
const errors = [];
// ── 1. PR title ────────────────────────────────────────────────
const titleOk = /^docs:\s+add\s+.+\s+to\s+contributors$/i.test(prTitle);
if (!titleOk) {
errors.push(
`**PR Title**: Must follow \`docs: add [your-name] to contributors\`. ` +
`Current title: \`${prTitle}\``
);
}
// ── 2. CONTRIBUTORS-START / END markers present ────────────────
const sectionMatch = content.match(
/<!-- CONTRIBUTORS-START -->([\s\S]*?)<!-- CONTRIBUTORS-END -->/
);
if (!sectionMatch) {
errors.push(
'**Missing markers**: `<!-- CONTRIBUTORS-START -->` or ' +
'`<!-- CONTRIBUTORS-END -->` markers are missing. Do not remove them.'
);
} else {
const section = sectionMatch[1];
// ── 3. No placeholder values left ─────────────────────────
if (section.includes('YOUR_GITHUB_USERNAME')) {
errors.push(
'**Placeholder not replaced**: Replace `YOUR_GITHUB_USERNAME` ' +
'with your actual GitHub username.'
);
}
if (section.includes('YOUR_X_HANDLE')) {
errors.push(
'**Placeholder not replaced**: Replace `YOUR_X_HANDLE` with your ' +
'X / Twitter handle, or remove the X badge if you don\'t have one.'
);
}
if (section.includes('>Your Name<')) {
errors.push(
'**Placeholder not replaced**: Replace `Your Name` with your actual name.'
);
}
if (/Role — Project 1/.test(section)) {
errors.push(
'**Placeholder not replaced**: Replace `Role — Project 1, Project 2` ' +
'with your real role and project names.'
);
}
// ── 4. At least one div block present ─────────────────────
const divBlocks = section.match(
/<div[^>]*style="display:inline-block[^"]*"[\s\S]*?<\/div>/g
) || [];
if (divBlocks.length === 0) {
errors.push(
'**Missing contributor block**: No `<div>` block was found inside ' +
'the `<!-- CONTRIBUTORS-START -->` section. ' +
'Use the template in the file.'
);
}
// ── 5. Structural checks on the added lines ────────────────
const checkIn = addedLines || section; // fallback to whole section
if (!/https:\/\/github\.com\/[A-Za-z0-9_-]+/.test(checkIn)) {
errors.push(
'**Missing GitHub link**: Your block must link to your GitHub profile ' +
'(`https://github.com/your-username`).'
);
}
if (!/<img\s+[^>]*src="https:\/\/github\.com\/[A-Za-z0-9_-]+\.png/.test(checkIn) && !/avatars\.githubusercontent\.com/.test(checkIn)) {
errors.push(
'**Missing avatar image**: Your block must include your GitHub avatar ' +
'(`https://github.com/your-username.png` or an avatars.githubusercontent.com link).'
);
}
if (!/<sub><b>[^<]+<\/b><\/sub>/.test(checkIn)) {
errors.push(
'**Missing name**: Include your display name as `<sub><b>Your Name</b></sub>`.'
);
}
if (!/<img\s+[^>]*src="https:\/\/img\.shields\.io\/badge\/-GitHub/.test(checkIn)) {
errors.push(
'**Missing GitHub badge**: Include the GitHub badge from the template.'
);
}
if (!/<sub>[A-Za-z][\w\s]*\s*[\-—]\s*.+<\/sub>/.test(checkIn)) {
errors.push(
'**Missing role/projects line**: Include a role line such as ' +
'`<sub>Researcher — StellarPay, LumenSwap</sub>`.'
);
}
}
// ── Post or update PR comment ──────────────────────────────────
const MARKER = '<!-- contributor-format-check -->';
const allComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = allComments.data.find(c => c.body.includes(MARKER));
if (errors.length > 0) {
const body = [
MARKER,
'## ❌ Contributor Format Check Failed',
'',
`Hi @${prAuthor}! Your \`CONTRIBUTORS.md\` changes don't follow the required format. Please fix the issues below, then push again.`,
'',
'### Issues found',
errors.map(e => `- ${e}`).join('\n'),
'',
'---',
'### Required format',
'',
'**PR title:** `docs: add [your-name] to contributors`',
'',
'**Contributor block template:**',
'```html',
'<div style="display:inline-block;width:130px;vertical-align:top;text-align:center;margin:8px">',
' <a href="https://github.com/YOUR_GITHUB_USERNAME">',
' <img src="https://github.com/YOUR_GITHUB_USERNAME.png" width="80" style="border-radius:50%" alt="Your Name" />',
' <br />',
' <sub><b>Your Name</b></sub>',
' </a>',
' <br />',
' <a href="https://github.com/YOUR_GITHUB_USERNAME"><img src="https://img.shields.io/badge/-GitHub-181717?logo=github&logoColor=white&style=flat-square" alt="GitHub" /></a>',
' <a href="https://x.com/YOUR_X_HANDLE"><img src="https://img.shields.io/badge/-X-000000?logo=x&logoColor=white&style=flat-square" alt="X" /></a>',
' <br />',
' <sub>Role — Project 1, Project 2</sub>',
'</div>',
'```',
'',
'> Remove the X badge line if you don\'t have an X account.',
].join('\n');
if (context.payload.pull_request.head.repo?.fork) {
console.log('Skipping PR comment on fork-based PR (token limitation)');
core.setFailed('Contributor format check failed. See logs for details.');
} else {
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
// CHANGED: Use pulls API instead of issues API for better fork support
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
body: body,
event: 'COMMENT',
});
}
core.setFailed('Contributor format check failed. See the PR comment for details.');
}
} else {
// All checks passed – update any previous failure comment
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: `${MARKER}\n## ✅ Contributor Format Check Passed\n\nGreat job, @${prAuthor}! Your \`CONTRIBUTORS.md\` entry follows the required format.`,
});
}
console.log('Contributor format check passed!');
}