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