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 24 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 20 days ago ↶
30
31
32
#[sqlx(default)]
33
pub flow_priority : String,
e7158350 Andy McCall 24 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 ,
1ac84c62 Andy McCall 1 month ago ↶
54
55
#[sqlx(default)]
56
pub open_issues : i64 ,
57
#[sqlx(default)]
58
pub open_proposals : i64 ,
d23464c0 Andy McCall 1 month ago ↶
59
}
60
61
impl RepoRow {
62
pub fn visibility ( & self ) -> Visibility {
63
Visibility:: parse( & self .visibility) .unwrap_or ( Visibility:: Private)
64
}
65
1553bfc9 Andy McCall 1 month ago ↶
66
pub fn topic_list ( & self ) -> Vec< & str > {
67
self .topics.split_whitespace ( ) .collect ( )
68
}
69
d23464c0 Andy McCall 1 month ago ↶
70
pub fn slug ( & self ) -> String {
71
format! ( " {} /{} " , self .owner, self .name)
72
}
73
}
74
75
# [ derive ( Clone, Debug, FromRow ) ]
76
pub struct PushRow {
77
pub id : i64 ,
78
pub at_ms : i64 ,
79
pub reference : String,
80
pub ok : bool ,
81
pub reason : String,
62d6e969 Andy McCall 1 month ago ↶
82
pub old_state : String,
83
pub new_state : String,
84
pub heads : i64 ,
85
pub changes : i64 ,
19bb31ff Andy McCall 1 month ago ↶
86
pub pusher : String,
62d6e969 Andy McCall 1 month ago ↶
87
}
88
89
90
# [ derive ( Clone, Debug ) ]
91
pub struct PushOutcome {
92
pub reference : String,
93
pub ok : bool ,
94
pub reason : String,
95
pub old_state : String,
96
pub new_state : String,
97
pub heads : i64 ,
98
pub changes : i64 ,
d23464c0 Andy McCall 1 month ago ↶
99
}
100
101
pub async fn list ( pool : & Pool) -> sqlx:: Result< Vec< RepoRow> > {
102
sqlx:: query_as( " SELECT * FROM repositories ORDER BY updated_ms DESC, owner, name" ) .fetch_all ( pool) .await
103
}
104
19bb31ff Andy McCall 1 month ago ↶
105
pub async fn get_by_id ( pool : & Pool, id : i64 ) -> sqlx:: Result< Option< RepoRow> > {
5d90e0e0 Andy McCall 20 days ago
106
sqlx:: query_as( " SELECT * FROM repositories WHERE id = $1" ) .bind ( id) .fetch_optional ( pool) .await
19bb31ff Andy McCall 1 month ago ↶
107
}
108
d23464c0 Andy McCall 1 month ago ↶
109
pub async fn list_by_owner ( pool : & Pool, owner : & str ) -> sqlx:: Result< Vec< RepoRow> > {
5d90e0e0 Andy McCall 20 days ago
110
sqlx:: query_as( " SELECT * FROM repositories WHERE owner = $1 ORDER BY updated_ms DESC, name" ) .bind ( owner) .fetch_all ( pool) .await
d23464c0 Andy McCall 1 month ago ↶
111
}
112
1ac84c62 Andy McCall 1 month ago ↶
113
d23464c0 Andy McCall 1 month ago ↶
114
pub async fn get ( pool : & Pool, owner : & str , name : & str ) -> sqlx:: Result< Option< RepoRow> > {
1ac84c62 Andy McCall 1 month ago ↶
115
sqlx:: query_as(
116
" SELECT r.*, \
117
(SELECT COUNT(*) FROM issues i WHERE i.repo_id = r.id AND i.state = 'open') AS open_issues, \
118
(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
119
FROM repositories r WHERE r.owner = $1 AND r.name = $2" ,
1ac84c62 Andy McCall 1 month ago ↶
120
)
121
.bind ( owner)
122
.bind ( name)
123
.fetch_optional ( pool)
124
.await
d23464c0 Andy McCall 1 month ago ↶
125
}
126
ff656953 Andy McCall 1 month ago ↶
127
128
129
d23464c0 Andy McCall 1 month ago ↶
130
pub async fn create ( pool : & Pool, owner : & str , name : & str , description : & str , visibility : Visibility) -> sqlx:: Result< RepoRow> {
131
let now = now_ms ( ) ;
132
sqlx:: query_as(
462f88d1 Andy McCall 1 month ago ↶
133
" INSERT INTO repositories (owner, name, description, visibility, created_ms, updated_ms, required_approvals) \
3b1bc27f Andy McCall 20 days ago
134
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 ↶
135
)
136
.bind ( owner)
137
.bind ( name)
138
.bind ( description)
139
.bind ( visibility.as_str ( ) )
140
.bind ( now)
141
.bind ( now)
462f88d1 Andy McCall 1 month ago ↶
142
.bind ( owner)
d23464c0 Andy McCall 1 month ago ↶
143
.fetch_one ( pool)
144
.await
d0fbea2e Andy McCall 1 month ago ↶
145
}
146
147
148
pub async fn moved_to ( pool : & Pool, owner : & str , name : & str ) -> sqlx:: Result< Option< RepoRow> > {
5d90e0e0 Andy McCall 20 days ago
149
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 ↶
150
.bind ( owner)
151
.bind ( name)
152
.fetch_optional ( pool)
153
.await? ;
154
match id {
155
Some ( id) => get_by_id ( pool, id) .await,
156
None => Ok ( None ) ,
157
}
158
}
159
160
161
162
163
pub async fn transfer ( pool : & Pool, id : i64 , old_owner : & str , name : & str , new_owner : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
164
sqlx:: query( " UPDATE repositories SET owner = $1, updated_ms = $2 WHERE id = $3" )
d0fbea2e Andy McCall 1 month ago ↶
165
.bind ( new_owner)
166
.bind ( now_ms ( ) )
167
.bind ( id)
168
.execute ( pool)
169
.await? ;
5d90e0e0 Andy McCall 20 days ago
170
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 ↶
171
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 ↶
172
.bind ( old_owner)
173
.bind ( name)
174
.bind ( id)
175
.bind ( now_ms ( ) )
176
.execute ( pool)
177
.await? ;
178
Ok ( ( ) )
179
}
180
181
182
pub async fn set_archived ( pool : & Pool, id : i64 , archived : bool , reason : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
183
sqlx:: query( " UPDATE repositories SET archived = $1, archived_reason = $2, updated_ms = $3 WHERE id = $4" )
d0fbea2e Andy McCall 1 month ago ↶
184
.bind ( archived)
185
.bind ( if archived { reason } else { " " } )
186
.bind ( now_ms ( ) )
187
.bind ( id)
188
.execute ( pool)
189
.await? ;
190
Ok ( ( ) )
d23464c0 Andy McCall 1 month ago ↶
191
}
192
193
pub async fn delete ( pool : & Pool, id : i64 ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
194
sqlx:: query( " DELETE FROM repositories WHERE id = $1" ) .bind ( id) .execute ( pool) .await? ;
d23464c0 Andy McCall 1 month ago ↶
195
Ok ( ( ) )
196
}
197
198
pub async fn set_description ( pool : & Pool, id : i64 , description : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
199
sqlx:: query( " UPDATE repositories SET description = $1 WHERE id = $2" ) .bind ( description) .bind ( id) .execute ( pool) .await? ;
d23464c0 Andy McCall 1 month ago ↶
200
Ok ( ( ) )
201
}
202
203
pub async fn set_default_bookmark ( pool : & Pool, id : i64 , bookmark : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
204
sqlx:: query( " UPDATE repositories SET default_bookmark = $1 WHERE id = $2" ) .bind ( bookmark) .bind ( id) .execute ( pool) .await? ;
d23464c0 Andy McCall 1 month ago ↶
205
Ok ( ( ) )
206
}
207
208
pub async fn set_visibility ( pool : & Pool, id : i64 , visibility : Visibility) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
209
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 ↶
210
Ok ( ( ) )
211
}
212
213
19bb31ff Andy McCall 1 month ago ↶
214
pub async fn record_push ( pool : & Pool, id : i64 , pusher : Option< ( i64 , & str ) > , results : & [PushOutcome]) -> sqlx:: Result< ( ) > {
2a0c09c0 Andy McCall 21 days ago ↶
215
record_push_via ( pool, id, pusher, " " , results) .await
216
}
217
218
219
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 ↶
220
let now = now_ms ( ) ;
941abdd6 Andy McCall 1 month ago ↶
221
let mut tx = super :: begin_write( pool) .await? ;
62d6e969 Andy McCall 1 month ago ↶
222
for r in results {
5d90e0e0 Andy McCall 20 days ago
223
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 ↶
224
.bind ( id)
225
.bind ( now)
62d6e969 Andy McCall 1 month ago ↶
226
.bind ( & r.reference)
227
.bind ( r.ok)
228
.bind ( & r.reason)
229
.bind ( & r.old_state)
230
.bind ( & r.new_state)
231
.bind ( r.heads)
232
.bind ( r.changes)
19bb31ff Andy McCall 1 month ago ↶
233
.bind ( pusher.map ( | p | p.0 ) .filter ( | id | * id > 0 ) )
234
.bind ( pusher.map ( | p | p.1 ) .unwrap_or ( " " ) )
2a0c09c0 Andy McCall 21 days ago ↶
235
.bind ( via)
d23464c0 Andy McCall 1 month ago ↶
236
.execute ( & mut * tx)
237
.await? ;
238
}
62d6e969 Andy McCall 1 month ago ↶
239
if results.iter ( ) .any ( | r | r.ok ) {
5d90e0e0 Andy McCall 20 days ago
240
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 ↶
241
}
242
tx.commit ( ) .await
243
}
244
245
pub async fn pushes ( pool : & Pool, id : i64 , limit : i64 ) -> sqlx:: Result< Vec< PushRow> > {
5d90e0e0 Andy McCall 20 days ago
246
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 ↶
247
.bind ( id)
248
.bind ( limit)
249
.fetch_all ( pool)
250
.await
251
}
3f4b0ea6 Andy McCall 1 month ago ↶
252
1553bfc9 Andy McCall 1 month ago ↶
253
254
255
256
pub fn parse_topics ( input : & str ) -> Result< String, String> {
257
let mut out: Vec< String> = Vec :: new( ) ;
258
for t in input.split ( [ ' ,' , ' ' , ' \n ' , ' \t ' ] ) .map ( | t | t.trim ( ) .to_ascii_lowercase ( ) ) .filter ( | t | ! t.is_empty ( ) ) {
259
if t.len ( ) > 35 | | ! t.chars ( ) .all ( | c | c.is_ascii_alphanumeric ( ) | | c = = ' -' ) | | t.starts_with ( ' -' ) {
260
return Err ( format! ( " \" {t} \" is not a topic: use letters, digits and dashes, up to 35 characters" ) ) ;
261
}
262
if ! out.contains ( & t) {
263
out.push ( t) ;
264
}
265
}
266
if out.len ( ) > 20 {
267
return Err ( " At most 20 topics." .into ( ) ) ;
268
}
269
Ok ( out.join ( " " ) )
270
}
271
272
pub async fn set_topics ( pool : & Pool, id : i64 , topics : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
273
sqlx:: query( " UPDATE repositories SET topics = $1 WHERE id = $2" ) .bind ( topics) .bind ( id) .execute ( pool) .await? ;
1553bfc9 Andy McCall 1 month ago ↶
274
Ok ( ( ) )
275
}
276
277
278
pub async fn toggle_star ( pool : & Pool, account_id : i64 , repo_id : i64 ) -> sqlx:: Result< bool > {
5d90e0e0 Andy McCall 20 days ago
279
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 ↶
280
if gone.rows_affected ( ) > 0 {
281
return Ok ( false ) ;
282
}
5d90e0e0 Andy McCall 20 days ago
283
sqlx:: query( " INSERT INTO stars (account_id, repo_id, at_ms) VALUES ($1, $2, $3)" )
1553bfc9 Andy McCall 1 month ago ↶
284
.bind ( account_id)
285
.bind ( repo_id)
286
.bind ( now_ms ( ) )
287
.execute ( pool)
288
.await? ;
289
Ok ( true )
290
}
291
292
pub async fn starred ( pool : & Pool, account_id : i64 , repo_id : i64 ) -> sqlx:: Result< bool > {
5d90e0e0 Andy McCall 20 days ago
293
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM stars WHERE account_id = $1 AND repo_id = $2" )
1553bfc9 Andy McCall 1 month ago ↶
294
.bind ( account_id)
295
.bind ( repo_id)
296
.fetch_one ( pool)
297
.await? ;
298
Ok ( n > 0 )
299
}
300
301
302
pub async fn counts ( pool : & Pool, repo_id : i64 ) -> sqlx:: Result< ( i64 , i64 ) > {
5d90e0e0 Andy McCall 20 days ago
303
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 ↶
304
let watchers = super :: notify:: watchers( pool, repo_id) .await? .len ( ) as i64 ;
1553bfc9 Andy McCall 1 month ago ↶
305
Ok ( ( stars, watchers) )
306
}
307
308
309
pub async fn star_counts ( pool : & Pool) -> sqlx:: Result< std:: collections:: HashMap< i64 , i64 > > {
310
let rows: Vec< ( i64 , i64 ) > = sqlx:: query_as( " SELECT repo_id, COUNT(*) FROM stars GROUP BY repo_id" ) .fetch_all ( pool) .await? ;
311
Ok ( rows.into_iter ( ) .collect ( ) )
312
}
313
07b1a5bb Andy McCall 1 month ago ↶
314
315
pub async fn set_forked_from ( pool : & Pool, id : i64 , from : i64 ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
316
sqlx:: query( " UPDATE repositories SET forked_from = $1 WHERE id = $2" ) .bind ( from) .bind ( id) .execute ( pool) .await? ;
07b1a5bb Andy McCall 1 month ago ↶
317
Ok ( ( ) )
318
}
319
320
321
pub async fn forks_of ( pool : & Pool, id : i64 ) -> sqlx:: Result< Vec< RepoRow> > {
5d90e0e0 Andy McCall 20 days ago
322
sqlx:: query_as( " SELECT * FROM repositories WHERE forked_from = $1 ORDER BY updated_ms DESC" ) .bind ( id) .fetch_all ( pool) .await
9008bf6c Andy McCall 1 month ago ↶
323
}
324
325
326
pub async fn set_flow ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
327
sqlx:: query( " UPDATE repositories SET flow = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
107eaba5 Andy McCall 20 days ago ↶
328
Ok ( ( ) )
329
}
330
331
pub async fn set_flow_priority ( pool : & Pool, id : i64 , priority : & str ) -> sqlx:: Result< ( ) > {
332
sqlx:: query( " UPDATE repositories SET flow_priority = $1 WHERE id = $2" ) .bind ( priority) .bind ( id) .execute ( pool) .await? ;
4a68f106 Andy McCall 18 days ago ↶
333
Ok ( ( ) )
334
}
335
336
337
pub async fn set_agents_decide_gates ( pool : & Pool, id : i64 , on : bool ) -> sqlx:: Result< ( ) > {
338
sqlx:: query( " UPDATE repositories SET agents_decide_gates = $1 WHERE id = $2" ) .bind ( on) .bind ( id) .execute ( pool) .await? ;
d3b048b2 Andy McCall 22 days ago ↶
339
Ok ( ( ) )
340
}
341
342
pub async fn set_backup ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
343
sqlx:: query( " UPDATE repositories SET backup = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
f4d6ef41 Andy McCall 22 days ago ↶
344
Ok ( ( ) )
345
}
346
347
pub async fn set_diffusion_land ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
348
sqlx:: query( " UPDATE repositories SET diffusion_land = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
e7158350 Andy McCall 24 days ago ↶
349
Ok ( ( ) )
350
}
351
352
pub async fn set_diffusion ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
353
sqlx:: query( " UPDATE repositories SET diffusion = $1 WHERE id = $2" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
9008bf6c Andy McCall 1 month ago ↶
354
Ok ( ( ) )
07b1a5bb Andy McCall 1 month ago ↶
355
}
356
3f4b0ea6 Andy McCall 1 month ago ↶
357
pub async fn set_git_mirror ( pool : & Pool, id : i64 , on : bool ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
358
sqlx:: query( " UPDATE repositories SET git_mirror = $1 WHERE id = $2" ) .bind ( on) .bind ( id) .execute ( pool) .await? ;
3f4b0ea6 Andy McCall 1 month ago ↶
359
Ok ( ( ) )
360
}
361
362
pub async fn set_git_mirror_state ( pool : & Pool, id : i64 , state : & str ) -> sqlx:: Result< ( ) > {
5d90e0e0 Andy McCall 20 days ago
363
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 ↶
364
Ok ( ( ) )
365
}
1553bfc9 Andy McCall 1 month ago ↶
366
367
# [ cfg ( test ) ]
368
mod topic_tests {
369
use super :: parse_topics;
370
371
# [ test ]
372
fn topics_are_normalised_and_checked ( ) {
373
assert_eq! ( parse_topics ( " Rust, vcs retro-computing,rust" ) .unwrap ( ) , " rust vcs retro-computing" ) ;
374
assert_eq! ( parse_topics ( " " ) .unwrap ( ) , " " ) ;
375
assert! ( parse_topics ( " c++" ) .is_err ( ) ) ;
376
assert! ( parse_topics ( " -lead" ) .is_err ( ) ) ;
377
assert! ( parse_topics ( & " x" .repeat ( 36 ) ) .is_err ( ) ) ;
378
assert! ( parse_topics ( & ( 0 .. 21 ) .map ( | i | format! ( " t{i} " ) ) .collect:: < Vec< _ > > ( ) .join ( " " ) ) .is_err ( ) ) ;
379
}
380
}