d23464c0 Andy McCall 1 month ago ↶
1
2
3
use super :: { now_ms, Pool} ;
4
use hub_api:: Visibility;
5
use sqlx:: FromRow;
6
e7158350 Andy McCall 23 days ago ↶
7
# [ derive ( Clone, Debug, Default, FromRow ) ]
d23464c0 Andy McCall 1 month ago ↶
8
pub struct RepoRow {
9
pub id : i64 ,
10
pub owner : String,
11
pub name : String,
12
pub description : String,
13
pub default_bookmark : String,
14
pub visibility : String,
15
pub created_ms : i64 ,
16
pub updated_ms : i64 ,
3f4b0ea6 Andy McCall 1 month ago ↶
17
#[sqlx(default)]
18
pub git_mirror : bool ,
19
#[sqlx(default)]
20
pub git_mirror_state : String,
07b1a5bb Andy McCall 1 month ago ↶
21
22
#[sqlx(default)]
23
pub forked_from : Option< i64 > ,
1553bfc9 Andy McCall 1 month ago ↶
24
25
#[sqlx(default)]
26
pub topics : String,
9008bf6c Andy McCall 1 month ago ↶
27
28
#[sqlx(default)]
29
pub flow : String,
107eaba5 Andy McCall 19 days ago ↶
30
31
32
#[sqlx(default)]
33
pub flow_priority : String,
e7158350 Andy McCall 23 days ago ↶
34
35
36
#[sqlx(default)]
37
pub diffusion : String,
f4d6ef41 Andy McCall 22 days ago ↶
38
39
40
#[sqlx(default)]
41
pub diffusion_land : String,
d3b048b2 Andy McCall 22 days ago ↶
42
43
44
#[sqlx(default)]
45
pub backup : String,
d0fbea2e Andy McCall 1 month ago ↶
46
47
#[sqlx(default)]
48
pub archived : bool ,
49
#[sqlx(default)]
50
pub archived_reason : String,
4a68f106 Andy McCall 18 days ago ↶
51
52
#[sqlx(default)]
53
pub agents_decide_gates : bool ,
a9f1c0bf Andy McCall 17 days ago ↶
54
55
56
#[sqlx(default)]
57
pub flow_keep_running : bool ,
367f34b8 Andy McCall 16 days ago ↶
58
59
60
#[sqlx(default)]
61
pub flow_skip : String,
5588bb36 Andy McCall 15 days ago ↶
62
63
64
#[sqlx(default)]
65
pub flow_reuse : bool ,
0ee5c3fb Andy McCall 17 days ago ↶
66
67
68
#[sqlx(default)]
69
pub push_protection : bool ,
166f6e9b Andy McCall 16 days ago
70
71
72
#[sqlx(default)]
73
pub kind : String,
74
75
#[sqlx(default)]
76
pub title : String,
1ac84c62 Andy McCall 1 month ago ↶
77
78
#[sqlx(default)]
79
pub open_issues : i64 ,
80
#[sqlx(default)]
81
pub open_proposals : i64 ,
d23464c0 Andy McCall 1 month ago ↶
82
}
83
84
impl RepoRow {
85
pub fn visibility ( & self ) -> Visibility {
86
Visibility:: parse( & self .visibility) .unwrap_or ( Visibility:: Private)
87
}
88
1553bfc9 Andy McCall 1 month ago ↶
89
pub fn topic_list ( & self ) -> Vec< & str > {
90
self .topics.split_whitespace ( ) .collect ( )
91
}
92
d23464c0 Andy McCall 1 month ago ↶
93
pub fn slug ( & self ) -> String {
94
format! ( " {} /{} " , self .owner, self .name)
166f6e9b Andy McCall 16 days ago
95
}
96
97
98
pub fn is_drop ( & self ) -> bool {
99
self .kind = = DROP
d23464c0 Andy McCall 1 month ago ↶
100
}
101
}
166f6e9b Andy McCall 16 days ago
102
103
104
pub const DROP : & str = " drop" ;
d23464c0 Andy McCall 1 month ago ↶
105
106
# [ derive ( Clone, Debug, FromRow ) ]
107
pub struct PushRow {
108
pub id : i64 ,
109
pub at_ms : i64 ,
110
pub reference : String,
111
pub ok : bool ,
112
pub reason : String,
62d6e969 Andy McCall 1 month ago ↶
113
pub old_state : String,
114
pub new_state : String,
115
pub heads : i64 ,
116
pub changes : i64 ,
19bb31ff Andy McCall 1 month ago ↶
117
pub pusher : String,
62d6e969 Andy McCall 1 month ago ↶
118
}
119
120
121
# [ derive ( Clone, Debug ) ]
122
pub struct PushOutcome {
123
pub reference : String,
124
pub ok : bool ,
125
pub reason : String,
126
pub old_state : String,
127
pub new_state : String,
128
pub heads : i64 ,
129
pub changes : i64 ,
d23464c0 Andy McCall 1 month ago ↶
130
}
131
166f6e9b Andy McCall 16 days ago
132
133
d23464c0 Andy McCall 1 month ago ↶
134
pub async fn list ( pool : & Pool) -> sqlx:: Result< Vec< RepoRow> > {
166f6e9b Andy McCall 16 days ago
135
sqlx:: query_as( " SELECT * FROM repositories WHERE kind = 'repo' ORDER BY updated_ms DESC, owner, name" ) .fetch_all ( pool) .await
136
}
137
138
139
140
141
pub async fn list_with_drops ( pool : & Pool) -> sqlx:: Result< Vec< RepoRow> > {
d23464c0 Andy McCall 1 month ago ↶
142
sqlx:: query_as( " SELECT * FROM repositories ORDER BY updated_ms DESC, owner, name" ) .fetch_all ( pool) .await
143
}
144
19bb31ff Andy McCall 1 month ago ↶
145
pub async fn get_by_id ( pool : & Pool, id : i64 ) -> sqlx:: Result< Option< RepoRow> > {
5d90e0e0 Andy McCall 20 days ago
146
sqlx:: query_as( " SELECT * FROM repositories WHERE id = $1" ) .bind ( id) .fetch_optional ( pool) .await
19bb31ff Andy McCall 1 month ago ↶
147
}
148
d23464c0 Andy McCall 1 month ago ↶
149
pub async fn list_by_owner ( pool : & Pool, owner : & str ) -> sqlx:: Result< Vec< RepoRow> > {
166f6e9b Andy McCall 16 days ago
150
sqlx:: query_as( " SELECT * FROM repositories WHERE owner = $1 AND kind = 'repo' ORDER BY updated_ms DESC, name" )
151
.bind ( owner)
152
.fetch_all ( pool)
153
.await
d23464c0 Andy McCall 1 month ago ↶
154
}
166f6e9b Andy McCall 16 days ago
155
156
d23464c0 Andy McCall 1 month ago ↶
157
166f6e9b Andy McCall 16 days ago
158
159
160
161
pub async fn create_drop (
162
pool : & Pool,
163
owner : & str ,
164
id : & str ,
165
title : & str ,
166
description : & str ,
167
visibility : Visibility,
168
) -> sqlx:: Result< RepoRow> {
169
let now = now_ms ( ) ;
170
sqlx:: query_as(
171
" INSERT INTO repositories (owner, name, description, visibility, created_ms, updated_ms, kind, title, flow) \
172
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'off') RETURNING *" ,
173
)
174
.bind ( owner)
175
.bind ( format! ( " {} /{id} " , crate :: repos:: names:: DROPS ) )
176
.bind ( description)
177
.bind ( visibility.as_str ( ) )
178
.bind ( now)
179
.bind ( now)
180
.bind ( DROP )
181
.bind ( title)
182
.fetch_one ( pool)
183
.await
184
}
185
ca0c443c Andy McCall 15 days ago ↶
186
187
# [ derive ( Clone, Debug, FromRow ) ]
188
pub struct DropComment {
189
pub id : i64 ,
190
pub repo_id : i64 ,
191
pub author_id : Option< i64 > ,
192
pub author : String,
193
pub body : String,
194
pub created_ms : i64 ,
195
}
196
197
pub async fn add_drop_comment ( pool : & Pool, repo_id : i64 , author_id : i64 , author : & str , body : & str ) -> sqlx:: Result< i64 > {
198
sqlx:: query_scalar( " INSERT INTO drop_comments (repo_id, author_id, author, body, created_ms) VALUES ($1, $2, $3, $4, $5) RETURNING id" )
199
.bind ( repo_id)
200
.bind ( author_id)
201
.bind ( author)
202
.bind ( body)
203
.bind ( now_ms ( ) )
204
.fetch_one ( pool)
205
.await
206
}
207
208
209
pub async fn drop_comments ( pool : & Pool, repo_id : i64 ) -> sqlx:: Result< Vec< DropComment> > {
210
sqlx:: query_as( " SELECT * FROM drop_comments WHERE repo_id = $1 ORDER BY created_ms, id" ) .bind ( repo_id) .fetch_all ( pool) .await
211
}
212
213
pub async fn drop_comment ( pool : & Pool, repo_id : i64 , id : i64 ) -> sqlx:: Result< Option< DropComment> > {
214
sqlx:: query_as( " SELECT * FROM drop_comments WHERE repo_id = $1 AND id = $2" ) .bind ( repo_id) .bind ( id) .fetch_optional ( pool) .await
215
}
216
217
pub async fn delete_drop_comment ( pool : & Pool, repo_id : i64 , id : i64 ) -> sqlx:: Result< ( ) > {
218
sqlx:: query( " DELETE FROM drop_comments WHERE repo_id = $1 AND id = $2" ) .bind ( repo_id) .bind ( id) .execute ( pool) .await? ;
219
Ok ( ( ) )
220
}
221
166f6e9b Andy McCall 16 days ago
222
223
pub async fn drops_of ( pool : & Pool, owner : & str ) -> sqlx:: Result< Vec< RepoRow> > {
224
sqlx:: query_as( " SELECT * FROM repositories WHERE kind = 'drop' AND owner = $1 ORDER BY updated_ms DESC, id DESC" )
225
.bind ( owner)
226
.fetch_all ( pool)
227
.await
228
}
229
230
231
232
pub async fn drop_by_id ( pool : & Pool, id : & str ) -> sqlx:: Result< Option< RepoRow> > {
233
sqlx:: query_as( " SELECT * FROM repositories WHERE kind = 'drop' AND name = $1" )
234
.bind ( format! ( " {} /{id} " , crate :: repos:: names:: DROPS ) )
235
.fetch_optional ( pool)
236
.await
237
}
238
239
240
pub async fn set_drop ( pool : & Pool, row_id : i64 , title : & str , description : & str , visibility : Visibility) -> sqlx:: Result< ( ) > {
241
sqlx:: query( " UPDATE repositories SET title = $1, description = $2, visibility = $3, updated_ms = $4 WHERE id = $5 AND kind = 'drop'" )
242
.bind ( title)
243
.bind ( description)
244
.bind ( visibility.as_str ( ) )
245
.bind ( now_ms ( ) )
246
.bind ( row_id)
247
.execute ( pool)
248
.await? ;
249
Ok ( ( ) )
250
}
251
1ac84c62 Andy McCall 1 month ago ↶
252
d23464c0 Andy McCall 1 month ago ↶
253
pub async fn get ( pool : & Pool, owner : & str , name : & str ) -> sqlx:: Result< Option< RepoRow> > {
1ac84c62 Andy McCall 1 month ago ↶
254
sqlx:: query_as(
255
" SELECT r.*, \
256
(SELECT COUNT(*) FROM issues i WHERE i.repo_id = r.id AND i.state = 'open') AS open_issues, \
257
(SELECT COUNT(*) FROM proposals p WHERE p.repo_id = r.id AND p.state IN ('open', 'draft')) AS open_proposals \
5d90e0e0 Andy McCall 20 days ago
258
FROM repositories r WHERE r.owner = $1 AND r.name = $2" ,
1ac84c62 Andy McCall 1 month ago ↶
259
)
260
.bind ( owner)
261
.bind ( name)
262
.fetch_optional ( pool)
263
.await
d23464c0 Andy McCall 1 month ago ↶
264
}
265
ff656953 Andy McCall 1 month ago ↶
266
267
268
d23464c0 Andy McCall 1 month ago ↶
269
pub async fn create ( pool : & Pool, owner : & str , name : & str , description : & str , visibility : Visibility) -> sqlx:: Result< RepoRow> {
270
let now = now_ms ( ) ;
271
sqlx:: query_as(
462f88d1 Andy McCall 1 month ago ↶
272
" INSERT INTO repositories (owner, name, description, visibility, created_ms, updated_ms, required_approvals) \
3b1bc27f Andy McCall 20 days ago
273
VALUES ($1, $2, $3, $4, $5, $6, (SELECT CASE WHEN COUNT(*) >= 2 THEN 1 ELSE 0 END FROM org_members m JOIN organisations o ON o.id = m.org_id WHERE o.name = $7)) RETURNING *" ,
d23464c0 Andy McCall 1 month ago ↶
274
)
275
.bind ( owner)
276
.bind ( name)
277
.bind ( description)
278
.bind ( visibility.as_str ( ) )
279
.bind ( now)
280
.bind ( now)
462f88d1 Andy McCall 1 month ago ↶
281
.bind ( owner)
d23464c0 Andy McCall 1 month ago ↶
282
.fetch_one ( pool)
283
.await
d0fbea2e Andy McCall 1 month ago ↶
284
}
285
286
287
pub async fn moved_to ( pool : & Pool, owner : & str , name : & str ) -> sqlx:: Result< Option< RepoRow> > {
5d90e0e0 Andy McCall 20 days ago
288
let id: Option< i64 > = sqlx:: query_scalar( " SELECT repo_id FROM repo_redirects WHERE old_owner = $1 AND old_name = $2" )
d0fbea2e Andy McCall 1 month ago ↶
289
.bind ( owner)
290
.bind ( name)
291
.fetch_optional ( pool)
292
.await? ;
293
match id {
294
Some ( id) => get_by_id ( pool, id) .await,
295
None => Ok ( None ) ,
296
}
297
}
298
299
300
301
302
pub async fn transfer ( pool : & Pool, id : i64 , old_owner : & str , name : & str , new_owner : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
303
sqlx:: query( " UPDATE repositories SET owner = $1, updated_ms = $2 WHERE id = $3" )
d0fbea2e Andy McCall 1 month ago ↶
304
.bind ( new_owner)
305
.bind ( now_ms ( ) )
306
.bind ( id)
307
.execute ( pool)
308
.await? ;
5d90e0e0 Andy McCall 20 days ago
309
sqlx:: query( " DELETE FROM repo_redirects WHERE old_owner = $1 AND old_name = $2" ) .bind ( new_owner) .bind ( name) .execute ( pool) .await? ;
d1bf2d69 Andy McCall 20 days ago ↶
310
sqlx:: query( " INSERT INTO repo_redirects (old_owner, old_name, repo_id, moved_ms) VALUES ($1, $2, $3, $4) ON CONFLICT (old_owner, old_name) DO UPDATE SET repo_id = excluded.repo_id, moved_ms = excluded.moved_ms" )
d0fbea2e Andy McCall 1 month ago ↶
311
.bind ( old_owner)
312
.bind ( name)
313
.bind ( id)
314
.bind ( now_ms ( ) )
315
.execute ( pool)
316
.await? ;
317
Ok ( ( ) )
318
}
319
320
321
pub async fn set_archived ( pool : & Pool, id : i64 , archived : bool , reason : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
322
sqlx:: query( " UPDATE repositories SET archived = $1, archived_reason = $2, updated_ms = $3 WHERE id = $4" )
d0fbea2e Andy McCall 1 month ago ↶
323
.bind ( archived)
324
.bind ( if archived { reason } else { " " } )
325
.bind ( now_ms ( ) )
326
.bind ( id)
327
.execute ( pool)
328
.await? ;
329
Ok ( ( ) )
d23464c0 Andy McCall 1 month ago ↶
330
}
331
332
pub async fn delete ( pool : & Pool, id : i64 ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
333
sqlx:: query( " DELETE FROM repositories WHERE id = $1" ) .bind ( id) .execute ( pool) .await? ;
d23464c0 Andy McCall 1 month ago ↶
334
Ok ( ( ) )
335
}
336
337
pub async fn set_description ( pool : & Pool, id : i64 , description : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
338
sqlx:: query( " UPDATE repositories SET description = $1 WHERE id = $2" ) .bind ( description) .bind ( id) .execute ( pool) .await? ;
d23464c0 Andy McCall 1 month ago ↶
339
Ok ( ( ) )
340
}
341
342
pub async fn set_default_bookmark ( pool : & Pool, id : i64 , bookmark : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
343
sqlx:: query( " UPDATE repositories SET default_bookmark = $1 WHERE id = $2" ) .bind ( bookmark) .bind ( id) .execute ( pool) .await? ;
d23464c0 Andy McCall 1 month ago ↶
344
Ok ( ( ) )
345
}
346
347
pub async fn set_visibility ( pool : & Pool, id : i64 , visibility : Visibility) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
348
sqlx:: query( " UPDATE repositories SET visibility = $1 WHERE id = $2" ) .bind ( visibility.as_str ( ) ) .bind ( id) .execute ( pool) .await? ;
d23464c0 Andy McCall 1 month ago ↶
349
Ok ( ( ) )
350
}
351
352
19bb31ff Andy McCall 1 month ago ↶
353
pub async fn record_push ( pool : & Pool, id : i64 , pusher : Option< ( i64 , & str ) > , results : & [PushOutcome]) -> sqlx:: Result< ( ) > {
2a0c09c0 Andy McCall 21 days ago ↶
354
record_push_via ( pool, id, pusher, " " , results) .await
355
}
356
357
358
pub async fn record_push_via ( pool : & Pool, id : i64 , pusher : Option< ( i64 , & str ) > , via : & str , results : & [PushOutcome]) -> sqlx:: Result< ( ) > {
d23464c0 Andy McCall 1 month ago ↶
359
let now = now_ms ( ) ;
941abdd6 Andy McCall 1 month ago ↶
360
let mut tx = super :: begin_write( pool) .await? ;
62d6e969 Andy McCall 1 month ago ↶
361
for r in results {
5d90e0e0 Andy McCall 20 days ago
362
sqlx:: query( " INSERT INTO pushes (repo_id, at_ms, reference, ok, reason, old_state, new_state, heads, changes, account_id, pusher, via) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)" )
d23464c0 Andy McCall 1 month ago ↶
363
.bind ( id)
364
.bind ( now)
62d6e969 Andy McCall 1 month ago ↶
365
.bind ( & r.reference)
366
.bind ( r.ok)
367
.bind ( & r.reason)
368
.bind ( & r.old_state)
369
.bind ( & r.new_state)
370
.bind ( r.heads)
371
.bind ( r.changes)
19bb31ff Andy McCall 1 month ago ↶
372
.bind ( pusher.map ( | p | p.0 ) .filter ( | id | * id > 0 ) )
373
.bind ( pusher.map ( | p | p.1 ) .unwrap_or ( " " ) )
2a0c09c0 Andy McCall 21 days ago ↶
374
.bind ( via)
d23464c0 Andy McCall 1 month ago ↶
375
.execute ( & mut * tx)
376
.await? ;
377
}
62d6e969 Andy McCall 1 month ago ↶
378
if results.iter ( ) .any ( | r | r.ok ) {
5d90e0e0 Andy McCall 20 days ago
379
sqlx:: query( " UPDATE repositories SET updated_ms = $1 WHERE id = $2" ) .bind ( now) .bind ( id) .execute ( & mut * tx) .await? ;
d23464c0 Andy McCall 1 month ago ↶
380
}
381
tx.commit ( ) .await
382
}
383
384
pub async fn pushes ( pool : & Pool, id : i64 , limit : i64 ) -> sqlx:: Result< Vec< PushRow> > {
5d90e0e0 Andy McCall 20 days ago
385
sqlx:: query_as( " SELECT id, at_ms, reference, ok, reason, old_state, new_state, heads, changes, pusher FROM pushes WHERE repo_id = $1 ORDER BY at_ms DESC, id DESC LIMIT $2" )
d23464c0 Andy McCall 1 month ago ↶
386
.bind ( id)
387
.bind ( limit)
388
.fetch_all ( pool)
389
.await
390
}
3f4b0ea6 Andy McCall 1 month ago ↶
391
1553bfc9 Andy McCall 1 month ago ↶
392
393
394
395
pub fn parse_topics ( input : & str ) -> Result< String, String> {
396
let mut out: Vec< String> = Vec :: new( ) ;
397
for t in input.split ( [ ' ,' , ' ' , ' \n ' , ' \t ' ] ) .map ( | t | t.trim ( ) .to_ascii_lowercase ( ) ) .filter ( | t | ! t.is_empty ( ) ) {
398
if t.len ( ) > 35 | | ! t.chars ( ) .all ( | c | c.is_ascii_alphanumeric ( ) | | c = = ' -' ) | | t.starts_with ( ' -' ) {
399
return Err ( format! ( " \" {t} \" is not a topic: use letters, digits and dashes, up to 35 characters" ) ) ;
400
}
401
if ! out.contains ( & t) {
402
out.push ( t) ;
403
}
404
}
405
if out.len ( ) > 20 {
406
return Err ( " At most 20 topics." .into ( ) ) ;
407
}
408
Ok ( out.join ( " " ) )
409
}
410
411
pub async fn set_topics ( pool : & Pool, id : i64 , topics : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
412
sqlx:: query( " UPDATE repositories SET topics = $1 WHERE id = $2" ) .bind ( topics) .bind ( id) .execute ( pool) .await? ;
1553bfc9 Andy McCall 1 month ago ↶
413
Ok ( ( ) )
414
}
415
416
417
pub async fn toggle_star ( pool : & Pool, account_id : i64 , repo_id : i64 ) -> sqlx:: Result< bool > {
5d90e0e0 Andy McCall 20 days ago
418
let gone = sqlx:: query( " DELETE FROM stars WHERE account_id = $1 AND repo_id = $2" ) .bind ( account_id) .bind ( repo_id) .execute ( pool) .await? ;
1553bfc9 Andy McCall 1 month ago ↶
419
if gone.rows_affected ( ) > 0 {
420
return Ok ( false ) ;
421
}
5d90e0e0 Andy McCall 20 days ago
422
sqlx:: query( " INSERT INTO stars (account_id, repo_id, at_ms) VALUES ($1, $2, $3)" )
1553bfc9 Andy McCall 1 month ago ↶
423
.bind ( account_id)
424
.bind ( repo_id)
425
.bind ( now_ms ( ) )
426
.execute ( pool)
427
.await? ;
428
Ok ( true )
429
}
430
431
pub async fn starred ( pool : & Pool, account_id : i64 , repo_id : i64 ) -> sqlx:: Result< bool > {
5d90e0e0 Andy McCall 20 days ago
432
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM stars WHERE account_id = $1 AND repo_id = $2" )
1553bfc9 Andy McCall 1 month ago ↶
433
.bind ( account_id)
434
.bind ( repo_id)
435
.fetch_one ( pool)
436
.await? ;
437
Ok ( n > 0 )
438
}
439
440
441
pub async fn counts ( pool : & Pool, repo_id : i64 ) -> sqlx:: Result< ( i64 , i64 ) > {
5d90e0e0 Andy McCall 20 days ago
442
let stars: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM stars WHERE repo_id = $1" ) .bind ( repo_id) .fetch_one ( pool) .await? ;
bff9106e Andy McCall 22 days ago ↶
443
let watchers = super :: notify:: watchers( pool, repo_id) .await? .len ( ) as i64 ;
1553bfc9 Andy McCall 1 month ago ↶
444
Ok ( ( stars, watchers) )
445
}
446
447
448
pub async fn star_counts ( pool : & Pool) -> sqlx:: Result< std:: collections:: HashMap< i64 , i64 > > {
449
let rows: Vec< ( i64 , i64 ) > = sqlx:: query_as( " SELECT repo_id, COUNT(*) FROM stars GROUP BY repo_id" ) .fetch_all ( pool) .await? ;
450
Ok ( rows.into_iter ( ) .collect ( ) )
451
}
452
07b1a5bb Andy McCall 1 month ago ↶
453
454
pub async fn set_forked_from ( pool : & Pool, id : i64 , from : i64 ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
455
sqlx:: query( " UPDATE repositories SET forked_from = $1 WHERE id = $2" ) .bind ( from) .bind ( id) .execute ( pool) .await? ;
07b1a5bb Andy McCall 1 month ago ↶
456
Ok ( ( ) )
457
}
458
459
460
pub async fn forks_of ( pool : & Pool, id : i64 ) -> sqlx:: Result< Vec< RepoRow> > {
166f6e9b Andy McCall 16 days ago
461
sqlx:: query_as( " SELECT * FROM repositories WHERE forked_from = $1 AND kind = 'repo' ORDER BY updated_ms DESC" )
462
.bind ( id)
463
.fetch_all ( pool)
464
.await
9008bf6c Andy McCall 1 month ago ↶
465
}
466
467
468
pub async fn set_flow ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
469
sqlx:: query( " UPDATE repositories SET flow = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
107eaba5 Andy McCall 19 days ago ↶
470
Ok ( ( ) )
471
}
472
473
pub async fn set_flow_priority ( pool : & Pool, id : i64 , priority : & str ) -> sqlx:: Result< ( ) > {
474
sqlx:: query( " UPDATE repositories SET flow_priority = $1 WHERE id = $2" ) .bind ( priority) .bind ( id) .execute ( pool) .await? ;
4a68f106 Andy McCall 18 days ago ↶
475
Ok ( ( ) )
476
}
477
478
479
pub async fn set_agents_decide_gates ( pool : & Pool, id : i64 , on : bool ) -> sqlx:: Result< ( ) > {
480
sqlx:: query( " UPDATE repositories SET agents_decide_gates = $1 WHERE id = $2" ) .bind ( on) .bind ( id) .execute ( pool) .await? ;
a9f1c0bf Andy McCall 17 days ago ↶
481
Ok ( ( ) )
482
}
483
484
485
5588bb36 Andy McCall 15 days ago ↶
486
pub async fn set_flow_reuse ( pool : & Pool, id : i64 , on : bool ) -> sqlx:: Result< ( ) > {
487
sqlx:: query( " UPDATE repositories SET flow_reuse = $1 WHERE id = $2" ) .bind ( on) .bind ( id) .execute ( pool) .await? ;
488
Ok ( ( ) )
489
}
490
367f34b8 Andy McCall 16 days ago ↶
491
pub async fn set_flow_skip ( pool : & Pool, id : i64 , skip : & str ) -> sqlx:: Result< ( ) > {
492
sqlx:: query( " UPDATE repositories SET flow_skip = $1 WHERE id = $2" ) .bind ( skip) .bind ( id) .execute ( pool) .await? ;
493
Ok ( ( ) )
494
}
495
a9f1c0bf Andy McCall 17 days ago ↶
496
pub async fn set_flow_keep_running ( pool : & Pool, id : i64 , on : bool ) -> sqlx:: Result< ( ) > {
497
sqlx:: query( " UPDATE repositories SET flow_keep_running = $1 WHERE id = $2" ) .bind ( on) .bind ( id) .execute ( pool) .await? ;
d3b048b2 Andy McCall 22 days ago ↶
498
Ok ( ( ) )
499
}
500
501
pub async fn set_backup ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
502
sqlx:: query( " UPDATE repositories SET backup = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
f4d6ef41 Andy McCall 22 days ago ↶
503
Ok ( ( ) )
504
}
505
506
pub async fn set_diffusion_land ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
507
sqlx:: query( " UPDATE repositories SET diffusion_land = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
e7158350 Andy McCall 23 days ago ↶
508
Ok ( ( ) )
509
}
510
511
pub async fn set_diffusion ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
512
sqlx:: query( " UPDATE repositories SET diffusion = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
9008bf6c Andy McCall 1 month ago ↶
513
Ok ( ( ) )
07b1a5bb Andy McCall 1 month ago ↶
514
}
515
3f4b0ea6 Andy McCall 1 month ago ↶
516
pub async fn set_git_mirror ( pool : & Pool, id : i64 , on : bool ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
517
sqlx:: query( " UPDATE repositories SET git_mirror = $1 WHERE id = $2" ) .bind ( on) .bind ( id) .execute ( pool) .await? ;
3f4b0ea6 Andy McCall 1 month ago ↶
518
Ok ( ( ) )
519
}
520
521
pub async fn set_git_mirror_state ( pool : & Pool, id : i64 , state : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
522
sqlx:: query( " UPDATE repositories SET git_mirror_state = $1 WHERE id = $2" ) .bind ( state) .bind ( id) .execute ( pool) .await? ;
3f4b0ea6 Andy McCall 1 month ago ↶
523
Ok ( ( ) )
524
}
1553bfc9 Andy McCall 1 month ago ↶
525
526
# [ cfg ( test ) ]
527
mod topic_tests {
528
use super :: parse_topics;
529
530
# [ test ]
531
fn topics_are_normalised_and_checked ( ) {
532
assert_eq! ( parse_topics ( " Rust, vcs retro-computing,rust" ) .unwrap ( ) , " rust vcs retro-computing" ) ;
533
assert_eq! ( parse_topics ( " " ) .unwrap ( ) , " " ) ;
534
assert! ( parse_topics ( " c++" ) .is_err ( ) ) ;
535
assert! ( parse_topics ( " -lead" ) .is_err ( ) ) ;
536
assert! ( parse_topics ( & " x" .repeat ( 36 ) ) .is_err ( ) ) ;
537
assert! ( parse_topics ( & ( 0 .. 21 ) .map ( | i | format! ( " t{i} " ) ) .collect:: < Vec< _ > > ( ) .join ( " " ) ) .is_err ( ) ) ;
538
}
539
}