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
|
/**
* ProjT Launcher - Merge Handler
* Handles PR merge operations with validation and queue management
*/
const { classify } = require('../supportedBranches.js')
// Component definitions for ProjT Launcher
const COMPONENTS = {
core: ['launcher/', 'systeminfo/', 'katabasis/', 'libnbtplusplus/', 'launcherjava/'],
ui: ['launcher/ui/', 'launcher/resources/', 'launcher/ui/'],
minecraft: ['launcher/minecraft/', 'tomlplusplus/', 'qdcss/'],
modplatform: ['launcher/modplatform/'],
build: ['CMakeLists.txt', 'cmake/', 'vcpkg.json', 'CMakePresets.json'],
docs: ['docs/', 'README.md', 'CONTRIBUTING.md'],
ci: ['.github/', 'ci/'],
}
/**
* Get component owners for changed files
* @param {Array} files - Changed files
* @returns {Set} Component owners
*/
function getComponentOwners(files) {
const owners = new Set()
for (const { filename } of files) {
for (const [component, paths] of Object.entries(COMPONENTS)) {
if (paths.some(path => filename.startsWith(path) || filename === path)) {
owners.add(component)
}
}
}
return owners
}
/**
* Run merge checklist for ProjT Launcher PRs
*/
function runChecklist({
committers,
events,
files,
pull_request,
log,
maintainers,
user,
userIsMaintainer,
}) {
// Check what components are touched
const components = getComponentOwners(files)
// Get eligible reviewers from maintainers
const eligible = maintainers && maintainers.length > 0
? new Set(maintainers)
: new Set()
// Get current approvals
const approvals = new Set(
events
.filter(
({ event, state, commit_id }) =>
event === 'reviewed' &&
state === 'approved' &&
// Only approvals for the current head SHA count
commit_id === pull_request.head.sha,
)
.map(({ user }) => user?.id)
.filter(Boolean),
)
const checklist = {
'PR targets a development branch (develop, master)':
classify(pull_request.base.ref).type.includes('development'),
'PR has passing CI checks':
pull_request.mergeable_state !== 'blocked',
'PR is at least one of:': {
'Approved by a maintainer': committers.intersection(approvals).size > 0,
'Opened by a maintainer': committers.has(pull_request.user.id),
'Part of a backport':
pull_request.head.ref.startsWith('backport-') ||
pull_request.labels?.some(l => l.name === 'backport'),
},
'PR has no merge conflicts':
pull_request.mergeable === true,
}
if (user) {
checklist[`${user.login} is a project maintainer`] = userIsMaintainer
if (components.size > 0) {
checklist[`${user.login} owns touched components (${Array.from(components).join(', ')})`] =
eligible.has(user.id)
}
} else {
checklist['PR has eligible reviewers'] = eligible.size > 0
}
const result = Object.values(checklist).every((v) =>
typeof v === 'boolean' ? v : Object.values(v).some(Boolean),
)
log('checklist', JSON.stringify(checklist))
log('components', JSON.stringify(Array.from(components)))
log('eligible', JSON.stringify(Array.from(eligible)))
log('result', result)
return {
checklist,
eligible,
components,
result,
}
}
/**
* Check for merge command in comment
* Format: @projt-launcher-bot merge
*/
function hasMergeCommand(body) {
return (body ?? '')
.replace(/<!--.*?-->/gms, '')
.replace(/(^`{3,})[^`].*?\1/gms, '')
.match(/^@projt-launcher-bot\s+merge\s*$/im)
}
/**
* Handle merge comment reaction
*/
async function handleMergeComment({ github, body, node_id, reaction }) {
if (!hasMergeCommand(body)) return
await github.graphql(
`mutation($node_id: ID!, $reaction: ReactionContent!) {
addReaction(input: {
content: $reaction,
subjectId: $node_id
})
{ clientMutationId }
}`,
{ node_id, reaction },
)
}
/**
* Handle merge request for a PR
*/
async function handleMerge({
github,
context,
core,
log,
dry,
pull_request,
events,
maintainers,
getTeamMembers,
getUser,
}) {
const pull_number = pull_request.number
// Get list of maintainers (project committers)
const committers = new Set(
(await getTeamMembers('projt-maintainers')).map(({ id }) => id),
)
// Get changed files
const files = (
await github.rest.pulls.listFiles({
...context.repo,
pull_number,
per_page: 100,
})
).data
// Early exit for large PRs
if (files.length >= 100) {
core.warning('PR touches 100+ files, manual merge required')
return false
}
// Only look through comments after the latest push
const lastPush = events.findLastIndex(
({ event, sha, commit_id }) =>
['committed', 'head_ref_force_pushed'].includes(event) &&
(sha ?? commit_id) === pull_request.head.sha,
)
const comments = events.slice(lastPush + 1).filter(
({ event, body, user, node_id }) =>
['commented', 'reviewed'].includes(event) &&
hasMergeCommand(body) &&
user &&
(dry ||
!events.some(
({ event, body }) =>
['commented'].includes(event) &&
body.match(new RegExp(`^<!-- comment: ${node_id} -->$`, 'm')),
)),
)
/**
* Perform the merge
*/
async function merge() {
if (dry) {
core.info(`Would merge #${pull_number}... (dry run)`)
return 'Merge completed (dry run)'
}
// Use merge queue if available, otherwise regular merge
try {
const resp = await github.graphql(
`mutation($node_id: ID!, $sha: GitObjectID) {
enqueuePullRequest(input: {
expectedHeadOid: $sha,
pullRequestId: $node_id
})
{
clientMutationId,
mergeQueueEntry { mergeQueue { url } }
}
}`,
{ node_id: pull_request.node_id, sha: pull_request.head.sha },
)
return [
`:heavy_check_mark: [Queued](${resp.enqueuePullRequest.mergeQueueEntry.mergeQueue.url}) for merge`,
]
} catch (e) {
log('Queue merge failed, trying direct merge', e.response?.errors?.[0]?.message)
}
// Fallback to direct merge
try {
await github.rest.pulls.merge({
...context.repo,
pull_number,
merge_method: 'squash',
sha: pull_request.head.sha,
})
return [':heavy_check_mark: Merged successfully']
} catch (e) {
return [`:x: Merge failed: ${e.message}`]
}
}
// Process merge commands
for (const comment of comments) {
const user = await getUser(comment.user.id)
const { checklist, result } = runChecklist({
committers,
events,
files,
pull_request,
log,
maintainers: maintainers || [],
user,
userIsMaintainer: committers.has(user.id),
})
const response = []
if (result) {
response.push(...(await merge()))
} else {
response.push(':x: Cannot merge - checklist not satisfied:')
response.push('')
response.push('```')
response.push(JSON.stringify(checklist, null, 2))
response.push('```')
}
if (!dry) {
await github.rest.issues.createComment({
...context.repo,
issue_number: pull_number,
body: [
`<!-- comment: ${comment.node_id} -->`,
'',
...response,
].join('\n'),
})
await handleMergeComment({
github,
body: comment.body,
node_id: comment.node_id,
reaction: result ? 'ROCKET' : 'CONFUSED',
})
} else {
core.info(`Response: ${response.join('\n')}`)
}
}
return comments.length > 0
}
module.exports = {
runChecklist,
hasMergeCommand,
handleMergeComment,
handleMerge,
getComponentOwners,
}
|