Repository navigation
Expand file tree
/
Copy pathissue_routes.v
More file actions
459 lines (434 loc) · 16.1 KB
/
Copy pathissue_routes.v
File metadata and controls
459 lines (434 loc) · 16.1 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
module main
import veb
import api
import time
struct ItemWithUser[T] {
item T
user User
}
type IssueWithUser = ItemWithUser[Issue]
type CommentWithUser = ItemWithUser[Comment]
// GitLab-style issue management is intentionally broader than repository
// write access: Reporters, the author, and a current assignee can maintain an
// issue without receiving permission to push code. Callers must separately
// verify that the user can still read the target repository.
fn (app &App) issue_user_can_manage(user_id int, repo Repo, issue Issue) bool {
if user_id <= 0 {
return false
}
return issue.author_id == user_id || user_id in issue.assigned || app.repo_access_level(user_id, repo) >= project_access_reporter
}
fn (app &App) can_manage_issue(ctx Context, repo Repo, issue Issue) bool {
return ctx.logged_in && app.can_read_repo(ctx, repo) && app.issue_user_can_manage(ctx.user.id, repo, issue)
}
@['/api/v1/:username/:repo_name/issues/count']
fn (mut app App) handle_issues_count(username string, repo_name string) veb.Result {
repo := app.find_repo_by_name_and_username(repo_name, username) or {
return ctx.api_not_found()
}
caller := app.api_user_from_ctx(ctx) or { User{} }
if !app.user_has_repo_read_access(caller.id, repo) {
return ctx.api_not_found()
}
count := app.get_repo_issue_count(repo.id)
return ctx.json(api.ApiIssueCount{
success: true
result: count
})
}
@['/:username/:repo_name/issues/new']
pub fn (mut app App) new_issue(mut ctx Context, username string, repo_name string) veb.Result {
if !ctx.logged_in {
return ctx.not_found()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if !app.can_read_repo(ctx, repo) {
return ctx.not_found()
}
ctx.set_page_title(['New issue', '${repo.user_name}/${repo.name}'])
return $veb.html()
}
@['/:username/issues']
pub fn (mut app App) handle_get_user_issues(mut ctx Context, username string) veb.Result {
return app.user_issues(mut ctx, username, 'created')
}
@['/:username/:repo_name/issues'; post]
pub fn (mut app App) handle_add_repo_issue(mut ctx Context, username string, repo_name string) veb.Result {
// TODO: use captcha instead of user restrictions
if !ctx.logged_in || user_reached_post_limit(ctx.user, int(time.now().unix())) {
return ctx.redirect_to_index()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if !app.can_read_repo(ctx, repo) {
return ctx.not_found()
}
title := ctx.form['title']
text := ctx.form['text']
if !valid_title(title) || !valid_body(text) {
ctx.error('Issue title is missing or the description is too long')
return ctx.redirect('/${username}/${repo_name}/issues/new')
}
app.add_issue(repo.id, ctx.user.id, title, text) or {
app.info(err.str())
return ctx.redirect('/${username}/${repo_name}/issues/new')
}
app.increment_user_post(mut ctx.user) or { app.info(err.str()) }
app.sync_repo_open_issue_count(repo.id) or { app.info(err.str()) }
app.dispatch_webhook(repo.id, 'issue', WebhookIssuePayload{
action: 'opened'
repo: '${username}/${repo_name}'
title: title
author: ctx.user.username
})
has_first_issue_activity := app.has_activity(ctx.user.id, 'first_issue')
if !has_first_issue_activity {
app.add_activity(ctx.user.id, 'first_issue') or { app.info(err.str()) }
}
return ctx.redirect('/${username}/${repo_name}/issues')
}
@['/:username/:repo_name/issues']
pub fn (mut app App) handle_get_repo_issues(mut ctx Context, username string, repo_name string) veb.Result {
return app.issues(mut ctx, username, repo_name, '0')
}
@['/:username/:repo_name/issues/:page']
pub fn (mut app App) issues(mut ctx Context, username string, repo_name string, page string) veb.Result {
mut repo := app.find_repo_by_name_and_username(repo_name, username) or {
return ctx.not_found()
}
if !app.can_read_repo(ctx, repo) {
return ctx.not_found()
}
mut page_i := page.int()
if page_i < 0 {
page_i = 0
}
open_issue_count := app.get_repo_issue_count(repo.id)
if repo.nr_open_issues != open_issue_count {
app.sync_repo_open_issue_count(repo.id) or { app.info(err.str()) }
repo.nr_open_issues = open_issue_count
}
current_state := normalize_issue_state(ctx.query['state'] or { 'open' })
closed_issue_count := app.get_repo_closed_issue_count(repo.id)
all_issue_count := open_issue_count + closed_issue_count
open_tab_class := if current_state == 'open' {
'issue-state-tab issue-state-tab--active'
} else {
'issue-state-tab'
}
closed_tab_class := if current_state == 'closed' {
'issue-state-tab issue-state-tab--active'
} else {
'issue-state-tab'
}
all_tab_class := if current_state == 'all' {
'issue-state-tab issue-state-tab--active'
} else {
'issue-state-tab'
}
issue_count := match current_state {
'all' { all_issue_count }
'closed' { closed_issue_count }
else { open_issue_count }
}
page_count := calculate_pages(issue_count, commits_per_page)
if page_i > page_count {
if page_count == 0 {
return ctx.redirect('/${repo.user_name}/${repo.name}/issues')
}
return ctx.redirect('/${repo.user_name}/${repo.name}/issues/${page_count}')
}
mut issues_with_users := []IssueWithUser{}
mut issue := Issue{}
mut user := User{}
repo_issues := app.find_repo_issues_as_page_by_state(repo.id, page_i, current_state)
mut i := 0
for i = 0; i < repo_issues.len; i++ {
issue = repo_issues[i]
user = app.get_user_by_id(issue.author_id) or { placeholder_user(issue.author_id) }
issue.labels = app.get_issue_labels(issue.id)
issue.repo_author = repo.user_name
issue.repo_name = repo.name
issues_with_users << IssueWithUser{
item: issue
user: user
}
}
show_repo_link := false
first := page_i == 0
last := page_i >= page_count
prev_page, next_page := generate_prev_next_pages(page_i)
ctx.set_page_title(['Issues', '${repo.user_name}/${repo.name}'])
return $veb.html()
}
@['/:username/:repo_name/issue/:id']
pub fn (mut app App) issue(mut ctx Context, username string, repo_name string, id string) veb.Result {
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if !app.can_read_repo(ctx, repo) {
return ctx.not_found()
}
mut issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
if issue.repo_id != repo.id || issue.is_pr {
return ctx.not_found()
}
issue_author := app.get_user_by_id(issue.author_id) or { placeholder_user(issue.author_id) }
assignees := app.find_issue_assignees(issue)
issue.assigned = assignees.map(it.id)
issue.labels = app.get_issue_labels(issue.id)
planning := app.issue_planning_summary(issue)
mut iterations := []Iteration{}
if org := app.get_org_by_name(repo.user_name) {
iterations = app.find_group_iterations(org.id)
}
can_manage := app.can_manage_issue(ctx, repo, issue)
can_manage_assignees := can_manage
mut assignable_users := []User{}
if can_manage_assignees {
for candidate in app.find_issue_assignable_users(repo) {
if candidate.id !in issue.assigned {
assignable_users << candidate
}
}
}
mut available_labels := []Label{}
if can_manage {
for label in app.list_repo_labels(repo.id) {
if !issue.labels.any(it.id == label.id) {
available_labels << label
}
}
}
ctx.set_page_title(['${issue.title} #${issue.id}', '${repo.user_name}/${repo.name}'])
mut comments_with_users := []CommentWithUser{}
mut comment := Comment{}
mut comment_author := User{}
issue_comments := app.get_all_issue_comments(issue.id)
mut i := 0
for i = 0; i < issue_comments.len; i++ {
comment = issue_comments[i]
comment_author = app.get_user_by_id(comment.author_id) or {
placeholder_user(comment.author_id)
}
comments_with_users << CommentWithUser{
item: comment
user: comment_author
}
}
return $veb.html()
}
@['/:username/:repo_name/issue/:id/edit']
pub fn (mut app App) edit_issue(mut ctx Context, username string, repo_name string, id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
if issue.repo_id != repo.id || issue.is_pr || !app.can_manage_issue(ctx, repo, issue) {
return ctx.not_found()
}
ctx.set_page_title(['Edit ${issue.title} #${issue.id}', '${repo.user_name}/${repo.name}'])
return $veb.html('templates/edit_issue.html')
}
@['/:username/:repo_name/issue/:id/edit'; post]
pub fn (mut app App) handle_edit_issue(mut ctx Context, username string, repo_name string, id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
if issue.repo_id != repo.id || issue.is_pr || !app.can_manage_issue(ctx, repo, issue) {
return ctx.not_found()
}
title := ctx.form['title']
body := ctx.form['text']
app.update_issue(issue.id, title, body) or {
ctx.error('Issue title is required and content must be within size limits')
return app.edit_issue(mut ctx, username, repo_name, id)
}
app.dispatch_webhook(repo.id, 'issue', WebhookIssuePayload{
action: 'updated'
repo: '${username}/${repo_name}'
title: title.trim_space()
author: ctx.user.username
})
return ctx.redirect('/${username}/${repo_name}/issue/${issue.id}')
}
@['/:username/:repo_name/issue/:id/labels'; post]
pub fn (mut app App) handle_add_issue_label(mut ctx Context, username string, repo_name string, id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
label := app.find_repo_label_by_id(repo.id, ctx.form['label_id'].int()) or {
return ctx.not_found()
}
if issue.repo_id != repo.id || issue.is_pr || !app.can_manage_issue(ctx, repo, issue) {
return ctx.not_found()
}
app.add_issue_label(issue.id, label.id) or {
ctx.error('Could not add that label')
return app.issue(mut ctx, username, repo_name, id)
}
return ctx.redirect('/${username}/${repo_name}/issue/${issue.id}')
}
@['/:username/:repo_name/issue/:id/labels/:label_id/delete'; post]
pub fn (mut app App) handle_remove_issue_label(mut ctx Context, username string, repo_name string, id string, label_id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
label := app.find_repo_label_by_id(repo.id, label_id.int()) or { return ctx.not_found() }
if issue.repo_id != repo.id || issue.is_pr || !app.can_manage_issue(ctx, repo, issue) {
return ctx.not_found()
}
app.remove_issue_label(issue.id, label.id) or {
ctx.error('Could not remove that label')
return app.issue(mut ctx, username, repo_name, id)
}
return ctx.redirect('/${username}/${repo_name}/issue/${issue.id}')
}
@['/:username/:repo_name/issue/:id/assign'; post]
pub fn (mut app App) handle_assign_issue(mut ctx Context, username string, repo_name string, id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
if issue.repo_id != repo.id || issue.is_pr || !app.can_manage_issue(ctx, repo, issue) {
return ctx.not_found()
}
app.assign_issue(issue.id, ctx.form['assignee_id'].int()) or {
ctx.error('Could not assign that project member')
return app.issue(mut ctx, username, repo_name, id)
}
return ctx.redirect('/${username}/${repo_name}/issue/${issue.id}')
}
@['/:username/:repo_name/issue/:id/unassign'; post]
pub fn (mut app App) handle_unassign_issue(mut ctx Context, username string, repo_name string, id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
if issue.repo_id != repo.id || issue.is_pr || !app.can_manage_issue(ctx, repo, issue) {
return ctx.not_found()
}
app.unassign_issue(issue.id, ctx.form['assignee_id'].int()) or {
ctx.error('Could not remove that assignee')
return app.issue(mut ctx, username, repo_name, id)
}
return ctx.redirect('/${username}/${repo_name}/issue/${issue.id}')
}
@['/:username/:repo_name/issue/:id/close'; post]
pub fn (mut app App) handle_close_issue(mut ctx Context, username string, repo_name string, id string) veb.Result {
return change_issue_status(mut app, mut ctx, username, repo_name, id, .closed)
}
@['/:username/:repo_name/issue/:id/reopen'; post]
pub fn (mut app App) handle_reopen_issue(mut ctx Context, username string, repo_name string, id string) veb.Result {
return change_issue_status(mut app, mut ctx, username, repo_name, id, .open)
}
fn change_issue_status(mut app App, mut ctx Context, username string, repo_name string, id string, status IssueStatus) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if !app.can_read_repo(ctx, repo) {
return ctx.not_found()
}
issue := app.find_issue_by_id(id.int()) or { return ctx.not_found() }
if issue.repo_id != repo.id || issue.is_pr {
return ctx.not_found()
}
if !app.issue_user_can_manage(ctx.user.id, repo, issue) {
return ctx.not_found()
}
if issue.status == status {
return ctx.redirect('/${username}/${repo_name}/issue/${issue.id}')
}
app.set_issue_status(issue.id, status) or {
ctx.error('Could not update issue status')
return app.issue(mut ctx, username, repo_name, id)
}
app.sync_repo_open_issue_count(repo.id) or { app.info(err.str()) }
action := if status == .closed { 'closed' } else { 'reopened' }
app.dispatch_webhook(repo.id, 'issue', WebhookIssuePayload{
action: action
repo: '${username}/${repo_name}'
title: issue.title
author: ctx.user.username
})
return ctx.redirect('/${username}/${repo_name}/issue/${issue.id}')
}
@['/:username/issues/:tab']
pub fn (mut app App) user_issues(mut ctx Context, username string, tab string) veb.Result {
if !ctx.logged_in {
return ctx.not_found()
}
if ctx.user.username != username {
return ctx.not_found()
}
exists, user := app.check_username(username)
if !exists {
return ctx.not_found()
}
current_tab := if tab in ['assigned', 'created', 'mentioned', 'activity'] {
tab
} else {
'created'
}
mut issues := match current_tab {
'assigned' { app.find_user_assigned_issues(user.id) }
'mentioned' { app.find_user_mentioned_issues(user.username) }
'activity' { app.find_user_recent_issues(user.id) }
else { app.find_user_issues(user.id) }
}
mut visible_issues := []Issue{cap: issues.len}
for mut issue in issues {
issue_repo := app.find_repo_by_id(issue.repo_id) or { continue }
if !app.can_read_repo(ctx, issue_repo) {
continue
}
issue.repo_author = issue_repo.user_name
issue.repo_name = issue_repo.name
issue.labels = app.get_issue_labels(issue.id)
visible_issues << issue
}
mut issues_with_users := []IssueWithUser{}
for issue in visible_issues {
issue_author := app.get_user_by_id(issue.author_id) or { placeholder_user(issue.author_id) }
issues_with_users << IssueWithUser{
item: issue
user: issue_author
}
}
tab_assigned_class := if current_tab == 'assigned' {
'user-issues-sidebar__item user-issues-sidebar__item--active'
} else {
'user-issues-sidebar__item'
}
tab_created_class := if current_tab == 'created' {
'user-issues-sidebar__item user-issues-sidebar__item--active'
} else {
'user-issues-sidebar__item'
}
tab_mentioned_class := if current_tab == 'mentioned' {
'user-issues-sidebar__item user-issues-sidebar__item--active'
} else {
'user-issues-sidebar__item'
}
tab_activity_class := if current_tab == 'activity' {
'user-issues-sidebar__item user-issues-sidebar__item--active'
} else {
'user-issues-sidebar__item'
}
show_repo_link := true
tab_title := match current_tab {
'assigned' { 'Assigned issues' }
'mentioned' { 'Mentioned issues' }
'activity' { 'Issue activity' }
else { 'Created issues' }
}
ctx.set_page_title([tab_title, user.username])
return $veb.html()
}