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