forked from rinafcode/teachLink_contract
-
Notifications
You must be signed in to change notification settings - Fork 0
235 lines (203 loc) · 8.33 KB
/
pr-labeler.yml
File metadata and controls
235 lines (203 loc) · 8.33 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
223
224
225
226
227
228
229
230
231
232
233
234
235
name: PR Labeler
on:
pull_request_target:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
issues: write
actions: read # Add this for better token access
jobs:
label:
runs-on: ubuntu-latest
# if: false # Temporarily disabled to fix CI
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Label PR based on changed files
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const labels = new Set();
for (const file of files) {
const filename = file.filename;
// Contract labels
if (filename.startsWith('contracts/teachlink/')) {
labels.add('contract: teachlink');
}
if (filename.startsWith('contracts/insurance/')) {
labels.add('contract: insurance');
}
// Module labels
if (filename.includes('bridge')) {
labels.add('module: bridge');
}
if (filename.includes('escrow')) {
labels.add('module: escrow');
}
// Type labels based on file type
if (filename.endsWith('.md')) {
labels.add('documentation');
}
if (filename.includes('test') || filename.includes('spec')) {
labels.add('testing');
}
if (filename.startsWith('scripts/') || filename.startsWith('.github/')) {
labels.add('tooling');
}
if (filename.startsWith('config/')) {
labels.add('configuration');
}
}
// Size labels based on changes
const additions = files.reduce((sum, f) => sum + f.additions, 0);
const deletions = files.reduce((sum, f) => sum + f.deletions, 0);
const changes = additions + deletions;
if (changes < 10) {
labels.add('size: xs');
} else if (changes < 50) {
labels.add('size: s');
} else if (changes < 200) {
labels.add('size: m');
} else if (changes < 500) {
labels.add('size: l');
} else {
labels.add('size: xl');
}
// Apply labels with error handling
if (labels.size > 0) {
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: Array.from(labels)
});
} catch (error) {
console.log('Failed to add labels:', error.message);
// Try to create labels that don't exist
for (const label of labels) {
try {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label,
color: '0366d6'
});
} catch (labelError) {
console.log(`Label ${label} already exists or failed to create:`, labelError.message);
}
}
// Try adding labels again
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: Array.from(labels)
});
} catch (retryError) {
console.log('Failed to add labels after creation:', retryError.message);
}
}
}
# Check PR title format
title-check:
runs-on: ubuntu-latest
# if: false # Temporarily disabled to fix CI
permissions:
pull-requests: write
issues: write
steps:
- name: Check PR title
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
const title = context.payload.pull_request.title;
// Expected format: type(scope): description
// e.g., feat(contract): add reward distribution
const conventionalCommitRegex = /^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\(.+\))?: .+/;
if (!conventionalCommitRegex.test(title)) {
const body = `
⚠️ **PR Title Format**
Please update your PR title to follow the conventional commit format:
\`\`\`
type(scope): description
\`\`\`
**Types:** feat, fix, docs, style, refactor, test, chore, perf, ci, build, revert
**Examples:**
- \`feat(contract): add learning reward distribution\`
- \`fix(escrow): resolve timeout calculation bug\`
- \`docs: update contributing guidelines\`
See [CONTRIBUTING.md](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/CONTRIBUTING.md) for details.
`;
// Check if we already commented about this
try {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const hasWarning = comments.some(c => c.body.includes('PR Title Format'));
if (!hasWarning) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});
}
} catch (error) {
console.log('Failed to check/create title format comment:', error.message);
}
}
# Welcome first-time contributors
welcome:
runs-on: ubuntu-latest
if: github.event.action == 'opened'
steps:
- name: Check if first-time contributor
id: check
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
const creator = context.payload.pull_request.user.login;
// Check for previous PRs
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all',
per_page: 100
});
const userPRs = prs.filter(pr => pr.user.login === creator);
return userPRs.length === 1;
result-encoding: string
- name: Welcome message
if: steps.check.outputs.result == 'true'
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
const message = `
🎉 **Welcome to TeachLink, @${context.payload.pull_request.user.login}!**
Thank you for your first contribution! A maintainer will review your PR soon.
**While you wait:**
- Make sure all CI checks pass ✅
- Review the [PR checklist](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/.github/PULL_REQUEST_TEMPLATE.md)
- Join our [Discord](https://discord.gg/teachlink) to connect with the community
We appreciate your contribution to decentralized education! 🎓
`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: message
});