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