-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathpr.v
More file actions
493 lines (435 loc) · 14.2 KB
/
Copy pathpr.v
File metadata and controls
493 lines (435 loc) · 14.2 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
import time
import veb
enum PrStatus {
open = 0
closed = 1
merged = 2
}
struct PullRequest {
id int @[primary; sql: serial]
mut:
repo_id int
head_repo_id int
author_id int
title string
description string
head_branch string
base_branch string
status int
comments_count int
created_at int
merged_at int
merge_commit_hash string
repo_author string @[skip]
repo_name string @[skip]
}
struct PrComment {
id int @[primary; sql: serial]
mut:
pr_id int
author_id int
created_at int
text string
}
struct PrReview {
id int @[primary; sql: serial]
mut:
pr_id int
author_id int
state int // 0 comment, 1 approved, 2 changes requested
body string
created_at int
}
struct PrReviewComment {
id int @[primary; sql: serial]
mut:
pr_id int
author_id int
review_id int // 0 if standalone (not part of a submitted review)
file_path string
line_number int
side string // 'old' or 'new'
text string
created_at int
}
// An approval is current merge-gating state, separate from the immutable
// review timeline. The unique pair ensures one person can contribute at most
// one approval regardless of how many reviews they submit.
struct PrApproval {
id int @[primary; sql: serial]
pr_id int @[unique: 'pr_approval']
user_id int @[unique: 'pr_approval']
approved_head_oid string
created_at int
}
struct PrApprovalView {
approval PrApproval
user User
}
fn (p &PullRequest) is_open() bool {
return p.status == int(PrStatus.open)
}
fn (p &PullRequest) is_merged() bool {
return p.status == int(PrStatus.merged)
}
fn (p &PullRequest) is_closed() bool {
return p.status == int(PrStatus.closed)
}
fn (p &PullRequest) status_label() string {
return match unsafe { PrStatus(p.status) } {
.open { 'Open' }
.closed { 'Closed' }
.merged { 'Merged' }
}
}
fn (p &PullRequest) status_class() string {
return match unsafe { PrStatus(p.status) } {
.open { 'pr-status--open' }
.closed { 'pr-status--closed' }
.merged { 'pr-status--merged' }
}
}
fn (p &PullRequest) relative_time() string {
return time.unix(p.created_at).relative()
}
fn (p &PullRequest) head_ref() string {
if p.head_repo_id > 0 && p.head_repo_id != p.repo_id {
return 'refs/merge-requests/${p.id}/head'
}
return p.head_branch
}
fn (p &PullRequest) formatted_title() veb.RawHtml {
parts := p.title.split('`')
mut out := ''
for idx, part in parts {
if idx % 2 == 0 {
out += html_escape_text(part)
} else if idx == parts.len - 1 {
out += '`' + html_escape_text(part)
} else {
out += '<code>' + html_escape_text(part) + '</code>'
}
}
return out
}
fn (c &PrComment) relative() string {
return time.unix(c.created_at).relative()
}
fn (r &PrReview) relative() string {
return time.unix(r.created_at).relative()
}
fn (r &PrReview) state_label() string {
return match r.state {
1 { 'approved' }
2 { 'requested changes' }
else { 'commented' }
}
}
fn (r &PrReview) state_class() string {
return match r.state {
1 { 'pr-review--approved' }
2 { 'pr-review--changes' }
else { 'pr-review--comment' }
}
}
fn (rc &PrReviewComment) relative() string {
return time.unix(rc.created_at).relative()
}
fn (mut app App) add_pull_request_with_created_at(repo_id int, author_id int, title string, description string, head string, base string, created_at int) !int {
return app.add_pull_request_from_repo_with_created_at(repo_id, 0, author_id, title, description, head, base, created_at)
}
fn (mut app App) add_pull_request_from_repo_with_created_at(repo_id int, head_repo_id int,
author_id int, title string, description string, head string, base string, created_at int) !int {
return db_insert_returning_id(mut app.db, 'PullRequest', ['repo_id', 'head_repo_id', 'author_id',
'title', 'description', 'head_branch', 'base_branch', 'status', 'comments_count', 'created_at',
'merged_at', 'merge_commit_hash'], [repo_id.str(), head_repo_id.str(), author_id.str(),
title, description, head, base, int(PrStatus.open).str(), '0', created_at.str(), '0', ''])
}
fn (mut app App) add_pull_request_from_repo(repo_id int, head_repo_id int, author_id int,
title string, description string, head string, base string) !int {
return app.add_pull_request_from_repo_with_created_at(repo_id, head_repo_id, author_id, title, description, head, base, int(time.now().unix()))
}
fn (mut app App) add_pull_request(repo_id int, author_id int, title string, description string, head string, base string) !int {
return app.add_pull_request_with_created_at(repo_id, author_id, title, description, head, base, int(time.now().unix()))
}
fn (mut app App) add_imported_pull_request(repo_id int, author_id int, title string, description string, head string, base string, created_at int) !int {
return app.add_pull_request_with_created_at(repo_id, author_id, title, description, head, base, created_at)
}
fn (mut app App) pull_request_exists_for_head(repo_id int, head string) bool {
rows := sql app.db {
select count from PullRequest where repo_id == repo_id && head_branch == head
} or { 0 }
return rows > 0
}
fn (mut app App) pull_request_exists_for_source(repo_id int, head_repo_id int, head string) bool {
wanted := int(PrStatus.open)
return sql app.db {
select count from PullRequest where repo_id == repo_id && head_repo_id == head_repo_id
&& head_branch == head && status == wanted
} or { 0 } > 0
}
fn (mut app App) find_pull_request_by_id(pr_id int) ?PullRequest {
rows := sql app.db {
select from PullRequest where id == pr_id limit 1
} or { []PullRequest{} }
if rows.len == 0 {
return none
}
return rows.first()
}
fn (mut app App) find_repo_pull_requests(repo_id int, pr_status PrStatus) []PullRequest {
wanted := int(pr_status)
return sql app.db {
select from PullRequest where repo_id == repo_id && status == wanted order by created_at desc
} or { []PullRequest{} }
}
fn (mut app App) find_user_pull_requests(user_id int) []PullRequest {
return sql app.db {
select from PullRequest where author_id == user_id order by created_at desc
} or { []PullRequest{} }
}
fn (mut app App) get_repo_open_pr_count(repo_id int) int {
wanted := int(PrStatus.open)
return sql app.db {
select count from PullRequest where repo_id == repo_id && status == wanted
} or { 0 }
}
fn (mut app App) sync_repo_open_pr_count(repo_id int) ! {
open_prs_count := app.get_repo_open_pr_count(repo_id)
sql app.db {
update Repo set nr_open_prs = open_prs_count where id == repo_id
}!
}
fn (mut app App) set_pr_status(pr_id int, new_status PrStatus) ! {
wanted := int(new_status)
sql app.db {
update PullRequest set status = wanted where id == pr_id
}!
}
fn (mut app App) set_pr_merged(pr_id int, merge_hash string) ! {
wanted := int(PrStatus.merged)
merged_at := int(time.now().unix())
sql app.db {
update PullRequest set status = wanted, merge_commit_hash = merge_hash, merged_at = merged_at
where id == pr_id
}!
}
fn (mut app App) increment_pr_comments(pr_id int) ! {
sql app.db {
update PullRequest set comments_count = comments_count + 1 where id == pr_id
}!
}
fn (mut app App) increment_repo_open_prs(repo_id int) ! {
sql app.db {
update Repo set nr_open_prs = nr_open_prs + 1 where id == repo_id
}!
}
fn (mut app App) decrement_repo_open_prs(repo_id int) ! {
sql app.db {
update Repo set nr_open_prs = nr_open_prs - 1 where id == repo_id
}!
}
fn (mut app App) add_pr_comment(pr_id int, author_id int, text string) ! {
comment := PrComment{
pr_id: pr_id
author_id: author_id
created_at: int(time.now().unix())
text: text
}
sql app.db {
insert comment into PrComment
}!
}
fn (mut app App) get_pr_comments(pr_id int) []PrComment {
return sql app.db {
select from PrComment where pr_id == pr_id order by created_at
} or { []PrComment{} }
}
fn (mut app App) add_pr_review(pr_id int, author_id int, state int, body string) !int {
return db_insert_returning_id(mut app.db, 'PrReview', ['pr_id', 'author_id', 'state', 'body',
'created_at'], [pr_id.str(), author_id.str(), state.str(), body,
int(time.now().unix()).str()])
}
fn (mut app App) get_pr_reviews(pr_id int) []PrReview {
return sql app.db {
select from PrReview where pr_id == pr_id order by created_at
} or { []PrReview{} }
}
fn (mut app App) approve_pull_request(pr_id int, user_id int) ! {
if pr_id <= 0 || user_id <= 0 {
return error('invalid approval')
}
pr := app.find_pull_request_by_id(pr_id) or { return error('merge request not found') }
if !pr.is_open() {
return error('merge request is not open')
}
repo := app.find_repo_by_id(pr.repo_id) or { return error('repository not found') }
// Imported fork refs can lag their source. Refresh before resolving the OID
// so the approval records the exact candidate the reviewer is approving.
app.refresh_cross_fork_pr_head(repo, pr)!
head_oid := app.pull_request_head_oid(pr)!
now := int(time.now().unix())
existing := sql app.db {
select count from PrApproval where pr_id == pr_id && user_id == user_id
}!
if existing > 0 {
sql app.db {
update PrApproval set approved_head_oid = head_oid, created_at = now where pr_id == pr_id
&& user_id == user_id
}!
return
}
approval := PrApproval{
pr_id: pr_id
user_id: user_id
approved_head_oid: head_oid
created_at: now
}
sql app.db {
insert approval into PrApproval
}!
}
fn (mut app App) revoke_pull_request_approval(pr_id int, user_id int) ! {
sql app.db {
delete from PrApproval where pr_id == pr_id && user_id == user_id
}!
}
fn (mut app App) pull_request_approval_count(pr_id int) int {
pr := app.find_pull_request_by_id(pr_id) or { return 0 }
head_oid := app.pull_request_head_oid(pr) or { return 0 }
return app.find_pull_request_approvals_for_head(pr, head_oid).len
}
fn (mut app App) user_approved_pull_request(pr_id int, user_id int) bool {
pr := app.find_pull_request_by_id(pr_id) or { return false }
head_oid := app.pull_request_head_oid(pr) or { return false }
return app.user_approved_pull_request_at_head(pr_id, user_id, head_oid)
}
fn (app &App) user_approved_pull_request_at_head(pr_id int, user_id int, head_oid string) bool {
if !is_full_git_oid(head_oid) {
return false
}
count := sql app.db {
select count from PrApproval where pr_id == pr_id && user_id == user_id
&& approved_head_oid == head_oid
} or { 0 }
return count > 0
}
fn (mut app App) find_pull_request_approvals(pr_id int) []PrApprovalView {
pr := app.find_pull_request_by_id(pr_id) or { return []PrApprovalView{} }
head_oid := app.pull_request_head_oid(pr) or { return []PrApprovalView{} }
return app.find_pull_request_approvals_for_head(pr, head_oid)
}
fn (mut app App) find_pull_request_approvals_for_head(pr PullRequest, head_oid string) []PrApprovalView {
if !is_full_git_oid(head_oid) {
return []PrApprovalView{}
}
repo := app.find_repo_by_id(pr.repo_id) or { return []PrApprovalView{} }
pr_id := pr.id
approvals := sql app.db {
select from PrApproval where pr_id == pr_id && approved_head_oid == head_oid order by created_at
} or { []PrApproval{} }
mut result := []PrApprovalView{cap: approvals.len}
for approval in approvals {
user := app.get_user_by_id(approval.user_id) or { continue }
if !user.is_registered || user.is_blocked || user.id == pr.author_id
|| app.repo_access_level(user.id, repo) < project_access_developer {
continue
}
result << PrApprovalView{
approval: approval
user: user
}
}
return result
}
fn (mut app App) pull_request_approvals_satisfied(pr PullRequest, repo Repo) bool {
head_oid := app.pull_request_head_oid(pr) or { return false }
return app.pull_request_approvals_satisfied_at_head(pr, repo, head_oid)
}
fn (mut app App) pull_request_approvals_satisfied_at_head(pr PullRequest, repo Repo, head_oid string) bool {
return app.approval_policies_satisfied_at_head(pr, repo, head_oid)
}
fn (app &App) pull_request_head_oid(pr PullRequest) !string {
repo := app.find_repo_by_id(pr.repo_id) or { return error('repository not found') }
head_oid := git_rev_parse(repo.git_dir, pr.head_ref())!
if !is_full_git_oid(head_oid) {
return error('merge request head is not a full object id')
}
return head_oid
}
fn is_full_git_oid(oid string) bool {
if oid.len !in [40, 64] {
return false
}
for ch in oid.to_lower() {
if !ch.is_digit() && (ch < `a` || ch > `f`) {
return false
}
}
return true
}
fn (mut app App) clear_pull_request_approvals(pr_id int) ! {
sql app.db {
delete from PrApproval where pr_id == pr_id
}!
}
// New commits invalidate approvals for every open merge request sourced from
// the updated branch. This mirrors GitLab's safe default and prevents approval
// of one revision from silently authorizing a different one.
fn (mut app App) clear_open_pr_approvals_for_head(repo_id int, branch string) ! {
wanted := int(PrStatus.open)
prs := sql app.db {
select from PullRequest where
(head_repo_id == repo_id || (head_repo_id == 0 && repo_id == repo_id))
&& head_branch == branch && status == wanted
} or { []PullRequest{} }
for pr in prs {
app.clear_pull_request_approvals(pr.id)!
}
}
fn (mut app App) add_pr_review_comment(pr_id int, author_id int, review_id int, file_path string, line_number int, side string, text string) ! {
c := PrReviewComment{
pr_id: pr_id
author_id: author_id
review_id: review_id
file_path: file_path
line_number: line_number
side: side
text: text
created_at: int(time.now().unix())
}
sql app.db {
insert c into PrReviewComment
}!
}
fn (mut app App) get_pr_review_comments(pr_id int) []PrReviewComment {
return sql app.db {
select from PrReviewComment where pr_id == pr_id order by created_at
} or { []PrReviewComment{} }
}
fn (mut app App) delete_repo_pull_requests(repo_id int) ! {
prs := sql app.db {
select from PullRequest where repo_id == repo_id
} or { []PullRequest{} }
for pr in prs {
pr_id := pr.id
sql app.db {
delete from PrApproval where pr_id == pr_id
}!
sql app.db {
delete from PrComment where pr_id == pr_id
}!
sql app.db {
delete from PrReview where pr_id == pr_id
}!
sql app.db {
delete from PrReviewComment where pr_id == pr_id
}!
}
sql app.db {
delete from PullRequest where repo_id == repo_id
}!
}