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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/**
* ProjT Launcher - Commit Validation for Pull Requests
* Validates commit messages, structure, and conventions
*/
const { classify } = require('../supportedBranches.js')
const withRateLimit = require('./withRateLimit.js')
const { dismissReviews, postReview } = require('./reviews.js')
const commitTypeConfig = (() => {
try {
return require('./commit-types.json')
} catch (error) {
console.warn(`commit validator: could not load commit-types.json (${error.message})`)
return {}
}
})()
const parseCommitTypeList = (value) => {
if (!value) {
return []
}
return value
.split(',')
.map((entry) => entry.trim().toLowerCase())
.filter(Boolean)
}
const DEFAULT_COMMIT_TYPES = [
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test',
'deps',
]
const EXTENDED_COMMIT_TYPES = [
...(commitTypeConfig.types ?? []),
]
const ENV_COMMIT_TYPES = parseCommitTypeList(
process.env.COMMIT_TYPES ?? process.env.ADDITIONAL_COMMIT_TYPES ?? ''
)
const COMMIT_TYPES = Array.from(
new Set([...DEFAULT_COMMIT_TYPES, ...EXTENDED_COMMIT_TYPES, ...ENV_COMMIT_TYPES])
)
const COMMIT_TYPE_SET = new Set(COMMIT_TYPES)
// Component scopes for ProjT Launcher
const VALID_SCOPES = [
'core',
'ui',
'minecraft',
'modplatform',
'modrinth',
'curseforge',
'ftb',
'technic',
'atlauncher',
'auth',
'java',
'news',
'settings',
'skins',
'translations',
'build',
'ci',
'nix',
'vcpkg',
'deps',
]
/**
* Validate commit message format
* Expected format: type(scope): description
* @param {string} message - Commit message
* @returns {object} Validation result
*/
function normalizeCommitType(type) {
if (!type) {
return ''
}
const trimmed = type.toLowerCase()
const legacyMatch = trimmed.match(/^\d+\.(.+)$/)
return legacyMatch ? legacyMatch[1] : trimmed
}
function validateCommitMessage(message) {
const firstLine = message.split('\n')[0]
// Check for conventional commit format
const conventionalMatch = firstLine.match(
/^(?<type>[\w.-]+)(?:\((?<scope>[\w-]+)\))?!?:\s*(?<description>.+)$/
)
if (!conventionalMatch) {
return {
valid: false,
severity: 'warning',
message: `Commit message doesn't follow conventional format: "${firstLine.substring(0, 50)}..."`,
}
}
const { type, scope, description } = conventionalMatch.groups
const normalizedType = normalizeCommitType(type)
// Validate type
if (!COMMIT_TYPE_SET.has(normalizedType)) {
return {
valid: false,
severity: 'warning',
message: `Unknown commit type "${type}". Valid types: ${COMMIT_TYPES.join(', ')}`,
}
}
// Validate scope if present
if (scope && !VALID_SCOPES.includes(scope.toLowerCase())) {
return {
valid: false,
severity: 'info',
message: `Unknown scope "${scope}". Consider using: ${VALID_SCOPES.slice(0, 5).join(', ')}...`,
}
}
// Check description length
if (description.length < 10) {
return {
valid: false,
severity: 'warning',
message: 'Commit description too short (min 10 chars)',
}
}
if (firstLine.length > 140) {
return {
valid: false,
severity: 'info',
message: 'First line exceeds 140 characters',
}
}
return { valid: true }
}
/**
* Check commit for specific patterns
* @param {object} commit - Commit object
* @returns {object} Check result
*/
function checkCommitPatterns(commit) {
const message = commit.message
const issues = []
// Check for WIP markers
if (message.match(/\bWIP\b/i)) {
issues.push({
severity: 'warning',
message: 'Commit contains WIP marker',
})
}
// Check for fixup/squash commits
if (message.match(/^(fixup|squash)!/i)) {
issues.push({
severity: 'info',
message: 'Commit is a fixup/squash commit - remember to rebase before merge',
})
}
// Check for merge commits
if (message.startsWith('Merge ')) {
issues.push({
severity: 'info',
message: 'Merge commit detected - consider rebasing instead',
})
}
// Check for large descriptions without body
if (message.split('\n').length === 1 && message.length > 100) {
issues.push({
severity: 'info',
message: 'Long commit message without body - consider adding details in commit body',
})
}
return issues
}
/**
* Validate all commits in a PR
*/
async function run({ github, context, core, dry }) {
await withRateLimit({ github, core }, async (stats) => {
stats.prs = 1
const pull_number = context.payload.pull_request.number
const base = context.payload.pull_request.base.ref
const baseClassification = classify(base)
// Get all commits in the PR
const commits = await github.paginate(github.rest.pulls.listCommits, {
...context.repo,
pull_number,
})
core.info(`Validating ${commits.length} commits for PR #${pull_number}`)
const results = []
for (const { sha, commit } of commits) {
const commitResults = {
sha: sha.substring(0, 7),
fullSha: sha,
author: commit.author.name,
message: commit.message.split('\n')[0],
issues: [],
}
// Validate commit message format
const formatValidation = validateCommitMessage(commit.message)
if (!formatValidation.valid) {
commitResults.issues.push({
severity: formatValidation.severity,
message: formatValidation.message,
})
}
// Check for commit patterns
const patternIssues = checkCommitPatterns(commit)
commitResults.issues.push(...patternIssues)
results.push(commitResults)
}
// Log results
let hasErrors = false
let hasWarnings = false
for (const result of results) {
core.startGroup(`Commit ${result.sha}`)
core.info(`Author: ${result.author}`)
core.info(`Message: ${result.message}`)
if (result.issues.length === 0) {
core.info('✓ No issues found')
} else {
for (const issue of result.issues) {
switch (issue.severity) {
case 'error':
core.error(issue.message)
hasErrors = true
break
case 'warning':
core.warning(issue.message)
hasWarnings = true
break
default:
core.info(`ℹ ${issue.message}`)
}
}
}
core.endGroup()
}
// If all commits are valid, dismiss any previous reviews
if (!hasErrors && !hasWarnings) {
await dismissReviews({ github, context, dry })
core.info('✓ All commits passed validation')
return
}
// Generate summary for issues
const issueCommits = results.filter(r => r.issues.length > 0)
if (issueCommits.length > 0) {
const body = [
'## Commit Validation Issues',
'',
'The following commits have issues that should be addressed:',
'',
...issueCommits.flatMap(commit => [
`### \`${commit.sha}\`: ${commit.message}`,
'',
...commit.issues.map(issue => `- **${issue.severity}**: ${issue.message}`),
'',
]),
'---',
'',
'### Commit Message Guidelines',
'',
'ProjT Launcher uses [Conventional Commits](https://www.conventionalcommits.org/):',
'',
'```',
'type(scope): description',
'',
'[optional body]',
'',
'[optional footer]',
'```',
'',
`**Types**: ${COMMIT_TYPES.join(', ')}`,
'',
`**Scopes**: ${VALID_SCOPES.slice(0, 8).join(', ')}, ...`,
].join('\n')
// Post review only for errors/warnings, not info
if (hasErrors || hasWarnings) {
await postReview({ github, context, core, dry, body })
}
// Write step summary
const fs = require('node:fs')
if (process.env.GITHUB_STEP_SUMMARY) {
fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, body)
}
}
if (hasErrors) {
throw new Error('Commit validation failed with errors')
}
})
}
module.exports = run
module.exports.validateCommitMessage = validateCommitMessage
module.exports.checkCommitPatterns = checkCommitPatterns
module.exports.normalizeCommitType = normalizeCommitType
|