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