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 22 days ago ↶
34
35
36
#[sqlx(default)]
37
pub diffusion_land : String,
d3b048b2 Andy McCall 22 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
ff656953 Andy McCall 1 month ago ↶
120
121
122
d23464c0 Andy McCall 1 month ago ↶
123
pub async fn create ( pool : & Pool, owner : & str , name : & str , description : & str , visibility : Visibility) -> sqlx:: Result< RepoRow> {
124
let now = now_ms ( ) ;
125
sqlx:: query_as(
462f88d1 Andy McCall 1 month ago ↶
126
" INSERT INTO repositories (owner, name, description, visibility, created_ms, updated_ms, required_approvals) \
ff656953 Andy McCall 1 month ago ↶
127
VALUES (?, ?, ?, ?, ?, ?, (SELECT COUNT(*) >= 2 FROM org_members m JOIN organisations o ON o.id = m.org_id WHERE o.name = ?)) RETURNING *" ,
d23464c0 Andy McCall 1 month ago ↶
128
)
129
.bind ( owner)
130
.bind ( name)
131
.bind ( description)
132
.bind ( visibility.as_str ( ) )
133
.bind ( now)
134
.bind ( now)
462f88d1 Andy McCall 1 month ago ↶
135
.bind ( owner)
d23464c0 Andy McCall 1 month ago ↶
136
.fetch_one ( pool)
137
.await
d0fbea2e Andy McCall 1 month ago ↶
138
}
139
140
141
pub async fn moved_to ( pool : & Pool, owner : & str , name : & str ) -> sqlx:: Result< Option< RepoRow> > {
142
let id: Option< i64 > = sqlx:: query_scalar( " SELECT repo_id FROM repo_redirects WHERE old_owner = ? AND old_name = ?" )
143
.bind ( owner)
144
.bind ( name)
145
.fetch_optional ( pool)
146
.await? ;
147
match id {
148
Some ( id) => get_by_id ( pool, id) .await,
149
None => Ok ( None ) ,
150
}
151
}
152
153
154
155
156
pub async fn transfer ( pool : & Pool, id : i64 , old_owner : & str , name : & str , new_owner : & str ) -> sqlx:: Result< ( ) > {
157
sqlx:: query( " UPDATE repositories SET owner = ?, updated_ms = ? WHERE id = ?" )
158
.bind ( new_owner)
159
.bind ( now_ms ( ) )
160
.bind ( id)
161
.execute ( pool)
162
.await? ;
163
sqlx:: query( " DELETE FROM repo_redirects WHERE old_owner = ? AND old_name = ?" ) .bind ( new_owner) .bind ( name) .execute ( pool) .await? ;
164
sqlx:: query( " INSERT OR REPLACE INTO repo_redirects (old_owner, old_name, repo_id, moved_ms) VALUES (?, ?, ?, ?)" )
165
.bind ( old_owner)
166
.bind ( name)
167
.bind ( id)
168
.bind ( now_ms ( ) )
169
.execute ( pool)
170
.await? ;
171
Ok ( ( ) )
172
}
173
174
175
pub async fn set_archived ( pool : & Pool, id : i64 , archived : bool , reason : & str ) -> sqlx:: Result< ( ) > {
176
sqlx:: query( " UPDATE repositories SET archived = ?, archived_reason = ?, updated_ms = ? WHERE id = ?" )
177
.bind ( archived)
178
.bind ( if archived { reason } else { " " } )
179
.bind ( now_ms ( ) )
180
.bind ( id)
181
.execute ( pool)
182
.await? ;
183
Ok ( ( ) )
d23464c0 Andy McCall 1 month ago ↶
184
}
185
186
pub async fn delete ( pool : & Pool, id : i64 ) -> sqlx:: Result< ( ) > {
187
sqlx:: query( " DELETE FROM repositories WHERE id = ?" ) .bind ( id) .execute ( pool) .await? ;
188
Ok ( ( ) )
189
}
190
191
pub async fn set_description ( pool : & Pool, id : i64 , description : & str ) -> sqlx:: Result< ( ) > {
192
sqlx:: query( " UPDATE repositories SET description = ? WHERE id = ?" ) .bind ( description) .bind ( id) .execute ( pool) .await? ;
193
Ok ( ( ) )
194
}
195
196
pub async fn set_default_bookmark ( pool : & Pool, id : i64 , bookmark : & str ) -> sqlx:: Result< ( ) > {
197
sqlx:: query( " UPDATE repositories SET default_bookmark = ? WHERE id = ?" ) .bind ( bookmark) .bind ( id) .execute ( pool) .await? ;
198
Ok ( ( ) )
199
}
200
201
pub async fn set_visibility ( pool : & Pool, id : i64 , visibility : Visibility) -> sqlx:: Result< ( ) > {
202
sqlx:: query( " UPDATE repositories SET visibility = ? WHERE id = ?" ) .bind ( visibility.as_str ( ) ) .bind ( id) .execute ( pool) .await? ;
203
Ok ( ( ) )
204
}
205
206
19bb31ff Andy McCall 1 month ago ↶
207
pub async fn record_push ( pool : & Pool, id : i64 , pusher : Option< ( i64 , & str ) > , results : & [PushOutcome]) -> sqlx:: Result< ( ) > {
2a0c09c0 Andy McCall 21 days ago ↶
208
record_push_via ( pool, id, pusher, " " , results) .await
209
}
210
211
212
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 ↶
213
let now = now_ms ( ) ;
941abdd6 Andy McCall 1 month ago ↶
214
let mut tx = super :: begin_write( pool) .await? ;
62d6e969 Andy McCall 1 month ago ↶
215
for r in results {
2a0c09c0 Andy McCall 21 days ago ↶
216
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 ↶
217
.bind ( id)
218
.bind ( now)
62d6e969 Andy McCall 1 month ago ↶
219
.bind ( & r.reference)
220
.bind ( r.ok)
221
.bind ( & r.reason)
222
.bind ( & r.old_state)
223
.bind ( & r.new_state)
224
.bind ( r.heads)
225
.bind ( r.changes)
19bb31ff Andy McCall 1 month ago ↶
226
.bind ( pusher.map ( | p | p.0 ) .filter ( | id | * id > 0 ) )
227
.bind ( pusher.map ( | p | p.1 ) .unwrap_or ( " " ) )
2a0c09c0 Andy McCall 21 days ago ↶
228
.bind ( via)
d23464c0 Andy McCall 1 month ago ↶
229
.execute ( & mut * tx)
230
.await? ;
231
}
62d6e969 Andy McCall 1 month ago ↶
232
if results.iter ( ) .any ( | r | r.ok ) {
d23464c0 Andy McCall 1 month ago ↶
233
sqlx:: query( " UPDATE repositories SET updated_ms = ? WHERE id = ?" ) .bind ( now) .bind ( id) .execute ( & mut * tx) .await? ;
234
}
235
tx.commit ( ) .await
236
}
237
238
pub async fn pushes ( pool : & Pool, id : i64 , limit : i64 ) -> sqlx:: Result< Vec< PushRow> > {
19bb31ff Andy McCall 1 month ago ↶
239
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 ↶
240
.bind ( id)
241
.bind ( limit)
242
.fetch_all ( pool)
243
.await
244
}
3f4b0ea6 Andy McCall 1 month ago ↶
245
1553bfc9 Andy McCall 1 month ago ↶
246
247
248
249
pub fn parse_topics ( input : & str ) -> Result< String, String> {
250
let mut out: Vec< String> = Vec :: new( ) ;
251
for t in input.split ( [ ' ,' , ' ' , ' \n ' , ' \t ' ] ) .map ( | t | t.trim ( ) .to_ascii_lowercase ( ) ) .filter ( | t | ! t.is_empty ( ) ) {
252
if t.len ( ) > 35 | | ! t.chars ( ) .all ( | c | c.is_ascii_alphanumeric ( ) | | c = = ' -' ) | | t.starts_with ( ' -' ) {
253
return Err ( format! ( " \" {t} \" is not a topic: use letters, digits and dashes, up to 35 characters" ) ) ;
254
}
255
if ! out.contains ( & t) {
256
out.push ( t) ;
257
}
258
}
259
if out.len ( ) > 20 {
260
return Err ( " At most 20 topics." .into ( ) ) ;
261
}
262
Ok ( out.join ( " " ) )
263
}
264
265
pub async fn set_topics ( pool : & Pool, id : i64 , topics : & str ) -> sqlx:: Result< ( ) > {
266
sqlx:: query( " UPDATE repositories SET topics = ? WHERE id = ?" ) .bind ( topics) .bind ( id) .execute ( pool) .await? ;
267
Ok ( ( ) )
268
}
269
270
271
pub async fn toggle_star ( pool : & Pool, account_id : i64 , repo_id : i64 ) -> sqlx:: Result< bool > {
272
let gone = sqlx:: query( " DELETE FROM stars WHERE account_id = ? AND repo_id = ?" ) .bind ( account_id) .bind ( repo_id) .execute ( pool) .await? ;
273
if gone.rows_affected ( ) > 0 {
274
return Ok ( false ) ;
275
}
276
sqlx:: query( " INSERT INTO stars (account_id, repo_id, at_ms) VALUES (?, ?, ?)" )
277
.bind ( account_id)
278
.bind ( repo_id)
279
.bind ( now_ms ( ) )
280
.execute ( pool)
281
.await? ;
282
Ok ( true )
283
}
284
285
pub async fn starred ( pool : & Pool, account_id : i64 , repo_id : i64 ) -> sqlx:: Result< bool > {
286
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM stars WHERE account_id = ? AND repo_id = ?" )
287
.bind ( account_id)
288
.bind ( repo_id)
289
.fetch_one ( pool)
290
.await? ;
291
Ok ( n > 0 )
292
}
293
294
295
pub async fn counts ( pool : & Pool, repo_id : i64 ) -> sqlx:: Result< ( i64 , i64 ) > {
296
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 ↶
297
let watchers = super :: notify:: watchers( pool, repo_id) .await? .len ( ) as i64 ;
1553bfc9 Andy McCall 1 month ago ↶
298
Ok ( ( stars, watchers) )
299
}
300
301
302
pub async fn star_counts ( pool : & Pool) -> sqlx:: Result< std:: collections:: HashMap< i64 , i64 > > {
303
let rows: Vec< ( i64 , i64 ) > = sqlx:: query_as( " SELECT repo_id, COUNT(*) FROM stars GROUP BY repo_id" ) .fetch_all ( pool) .await? ;
304
Ok ( rows.into_iter ( ) .collect ( ) )
305
}
306
07b1a5bb Andy McCall 1 month ago ↶
307
308
pub async fn set_forked_from ( pool : & Pool, id : i64 , from : i64 ) -> sqlx:: Result< ( ) > {
309
sqlx:: query( " UPDATE repositories SET forked_from = ? WHERE id = ?" ) .bind ( from) .bind ( id) .execute ( pool) .await? ;
310
Ok ( ( ) )
311
}
312
313
314
pub async fn forks_of ( pool : & Pool, id : i64 ) -> sqlx:: Result< Vec< RepoRow> > {
315
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 ↶
316
}
317
318
319
pub async fn set_flow ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
320
sqlx:: query( " UPDATE repositories SET flow = ? WHERE id = ?" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
d3b048b2 Andy McCall 22 days ago ↶
321
Ok ( ( ) )
322
}
323
324
pub async fn set_backup ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
325
sqlx:: query( " UPDATE repositories SET backup = ? WHERE id = ?" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
f4d6ef41 Andy McCall 22 days ago ↶
326
Ok ( ( ) )
327
}
328
329
pub async fn set_diffusion_land ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
330
sqlx:: query( " UPDATE repositories SET diffusion_land = ? WHERE id = ?" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
e7158350 Andy McCall 24 days ago ↶
331
Ok ( ( ) )
332
}
333
334
pub async fn set_diffusion ( pool : & Pool, id : i64 , setting : & str ) -> sqlx:: Result< ( ) > {
335
sqlx:: query( " UPDATE repositories SET diffusion = ? WHERE id = ?" ) .bind ( setting) .bind ( id) .execute ( pool) .await? ;
9008bf6c Andy McCall 1 month ago ↶
336
Ok ( ( ) )
07b1a5bb Andy McCall 1 month ago ↶
337
}
338
3f4b0ea6 Andy McCall 1 month ago ↶
339
pub async fn set_git_mirror ( pool : & Pool, id : i64 , on : bool ) -> sqlx:: Result< ( ) > {
340
sqlx:: query( " UPDATE repositories SET git_mirror = ? WHERE id = ?" ) .bind ( on) .bind ( id) .execute ( pool) .await? ;
341
Ok ( ( ) )
342
}
343
344
pub async fn set_git_mirror_state ( pool : & Pool, id : i64 , state : & str ) -> sqlx:: Result< ( ) > {
345
sqlx:: query( " UPDATE repositories SET git_mirror_state = ? WHERE id = ?" ) .bind ( state) .bind ( id) .execute ( pool) .await? ;
346
Ok ( ( ) )
347
}
1553bfc9 Andy McCall 1 month ago ↶
348
349
# [ cfg ( test ) ]
350
mod topic_tests {
351
use super :: parse_topics;
352
353
# [ test ]
354
fn topics_are_normalised_and_checked ( ) {
355
assert_eq! ( parse_topics ( " Rust, vcs retro-computing,rust" ) .unwrap ( ) , " rust vcs retro-computing" ) ;
356
assert_eq! ( parse_topics ( " " ) .unwrap ( ) , " " ) ;
357
assert! ( parse_topics ( " c++" ) .is_err ( ) ) ;
358
assert! ( parse_topics ( " -lead" ) .is_err ( ) ) ;
359
assert! ( parse_topics ( & " x" .repeat ( 36 ) ) .is_err ( ) ) ;
360
assert! ( parse_topics ( & ( 0 .. 21 ) .map ( | i | format! ( " t{i} " ) ) .collect:: < Vec< _ > > ( ) .join ( " " ) ) .is_err ( ) ) ;
361
}
362
}