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