OXYGENHUB
◆ v0.7.0 oxygenhub / crates / hub / src / db / repos.rs
18.9 KiB · Rust · last changed A push reuses a job's pass on the same state (closes #265) 15 days ago source blame history raw
1
//! The `repositories` and `pushes` tables.
2
3
use super::{now_ms, Pool};
4
use hub_api::Visibility;
5
use sqlx::FromRow;
6
7
#[derive(Clone, Debug, Default, 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,
17
    #[sqlx(default)]
18
    pub git_mirror: bool,
19
    #[sqlx(default)]
20
    pub git_mirror_state: String,
21
    /// The repository this one was forked from (migration 0009).
22
    #[sqlx(default)]
23
    pub forked_from: Option<i64>,
24
    /// Space-separated topics (migration 0010).
25
    #[sqlx(default)]
26
    pub topics: String,
27
    /// OxygenFlow: `on`, `off`, or empty to follow hub.toml (migration 0018).
28
    #[sqlx(default)]
29
    pub flow: String,
30
    /// Its jobs' priority: `low`, `normal`, `high`, or empty to follow its
31
    /// organisation's and the site's (oxygenhub#244, migration 0073).
32
    #[sqlx(default)]
33
    pub flow_priority: String,
34
    /// Diffusion: `off`, `green`, `any`, or empty to follow hub.toml
35
    /// (migration 0058, #148).
36
    #[sqlx(default)]
37
    pub diffusion: String,
38
    /// Diffusion auto-land: `auto`, `propose`, or empty for propose
39
    /// (migration 0061).
40
    #[sqlx(default)]
41
    pub diffusion_land: String,
42
    /// Backups (migration 0062): empty with its owner's, `off`, or its own
43
    /// `hourly` or `daily`.
44
    #[sqlx(default)]
45
    pub backup: String,
46
    /// Read-only, with why (migration 0040, #108).
47
    #[sqlx(default)]
48
    pub archived: bool,
49
    #[sqlx(default)]
50
    pub archived_reason: String,
51
    /// Agents' tokens may approve or reject its approval gates (#258).
52
    #[sqlx(default)]
53
    pub agents_decide_gates: bool,
54
    /// Newest run wins (#267): a superseded run already running finishes;
55
    /// only queued ones are cancelled. Off by default.
56
    #[sqlx(default)]
57
    pub flow_keep_running: bool,
58
    /// Where a push may skip its run with `[skip flow]` (#284, migration
59
    /// 0088): `any` bookmark, `others` than the default one, or `never`.
60
    #[sqlx(default)]
61
    pub flow_skip: String,
62
    /// A push run reuses a job's pass on the same state (#265, migration
63
    /// 0090). On by default.
64
    #[sqlx(default)]
65
    pub flow_reuse: bool,
66
    /// Pushes that add possible secrets are refused (#268, migration
67
    /// 0085). On by default.
68
    #[sqlx(default)]
69
    pub push_protection: bool,
70
    /// `repo`, or `drop` for a drop (#279, migration 0087): a small
71
    /// repository named `drops/ID` that no repository list shows.
72
    #[sqlx(default)]
73
    pub kind: String,
74
    /// A drop's title.
75
    #[sqlx(default)]
76
    pub title: String,
77
    /// Counts for the tabs; filled by `get` only.
78
    #[sqlx(default)]
79
    pub open_issues: i64,
80
    #[sqlx(default)]
81
    pub open_proposals: i64,
82
}
83
84
impl RepoRow {
85
    pub fn visibility(&self) -> Visibility {
86
        Visibility::parse(&self.visibility).unwrap_or(Visibility::Private)
87
    }
88
89
    pub fn topic_list(&self) -> Vec<&str> {
90
        self.topics.split_whitespace().collect()
91
    }
92
93
    pub fn slug(&self) -> String {
94
        format!("{}/{}", self.owner, self.name)
95
    }
96
97
    /// A drop (#279), not a repository.
98
    pub fn is_drop(&self) -> bool {
99
        self.kind == DROP
100
    }
101
}
102
103
/// `repositories.kind` of a drop (#279).
104
pub const DROP: &str = "drop";
105
106
#[derive(Clone, Debug, FromRow)]
107
pub struct PushRow {
108
    pub id: i64,
109
    pub at_ms: i64,
110
    pub reference: String,
111
    pub ok: bool,
112
    pub reason: String,
113
    pub old_state: String,
114
    pub new_state: String,
115
    pub heads: i64,
116
    pub changes: i64,
117
    pub pusher: String,
118
}
119
120
/// One reference's outcome in a push, as the protocol backend reports it.
121
#[derive(Clone, Debug)]
122
pub struct PushOutcome {
123
    pub reference: String,
124
    pub ok: bool,
125
    pub reason: String,
126
    pub old_state: String,
127
    pub new_state: String,
128
    pub heads: i64,
129
    pub changes: i64,
130
}
131
132
/// Every repository, drops left out: what every list, search and count
133
/// of repositories starts from (#279).
134
pub async fn list(pool: &Pool) -> sqlx::Result<Vec<RepoRow>> {
135
    sqlx::query_as("SELECT * FROM repositories WHERE kind = 'repo' ORDER BY updated_ms DESC, owner, name").fetch_all(pool).await
136
}
137
138
/// Every repository and every drop: for upkeep that covers all stored
139
/// repositories (gc, the history scan, `oxygenhub check`), never a list
140
/// people see unless it labels the drops.
141
pub async fn list_with_drops(pool: &Pool) -> sqlx::Result<Vec<RepoRow>> {
142
    sqlx::query_as("SELECT * FROM repositories ORDER BY updated_ms DESC, owner, name").fetch_all(pool).await
143
}
144
145
pub async fn get_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<RepoRow>> {
146
    sqlx::query_as("SELECT * FROM repositories WHERE id = $1").bind(id).fetch_optional(pool).await
147
}
148
149
pub async fn list_by_owner(pool: &Pool, owner: &str) -> sqlx::Result<Vec<RepoRow>> {
150
    sqlx::query_as("SELECT * FROM repositories WHERE owner = $1 AND kind = 'repo' ORDER BY updated_ms DESC, name")
151
        .bind(owner)
152
        .fetch_all(pool)
153
        .await
154
}
155
156
// ------------------------------------------------------------------ drops
157
158
/// A drop's row (#279): a repository named `drops/ID`, its kind `drop`,
159
/// push protection on and Flow off. `visibility` is `public`, or
160
/// `private` for a secret drop.
161
pub async fn create_drop(
162
    pool: &Pool,
163
    owner: &str,
164
    id: &str,
165
    title: &str,
166
    description: &str,
167
    visibility: Visibility,
168
) -> sqlx::Result<RepoRow> {
169
    let now = now_ms();
170
    sqlx::query_as(
171
        "INSERT INTO repositories (owner, name, description, visibility, created_ms, updated_ms, kind, title, flow) \
172
         VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'off') RETURNING *",
173
    )
174
    .bind(owner)
175
    .bind(format!("{}/{id}", crate::repos::names::DROPS))
176
    .bind(description)
177
    .bind(visibility.as_str())
178
    .bind(now)
179
    .bind(now)
180
    .bind(DROP)
181
    .bind(title)
182
    .fetch_one(pool)
183
    .await
184
}
185
186
/// An owner's drops, newest activity first.
187
pub async fn drops_of(pool: &Pool, owner: &str) -> sqlx::Result<Vec<RepoRow>> {
188
    sqlx::query_as("SELECT * FROM repositories WHERE kind = 'drop' AND owner = $1 ORDER BY updated_ms DESC, id DESC")
189
        .bind(owner)
190
        .fetch_all(pool)
191
        .await
192
}
193
194
/// The drop with this id, whoever owns it: ids are random and made
195
/// unique across the hub.
196
pub async fn drop_by_id(pool: &Pool, id: &str) -> sqlx::Result<Option<RepoRow>> {
197
    sqlx::query_as("SELECT * FROM repositories WHERE kind = 'drop' AND name = $1")
198
        .bind(format!("{}/{id}", crate::repos::names::DROPS))
199
        .fetch_optional(pool)
200
        .await
201
}
202
203
/// A drop's title, description and visibility, and that it changed.
204
pub async fn set_drop(pool: &Pool, row_id: i64, title: &str, description: &str, visibility: Visibility) -> sqlx::Result<()> {
205
    sqlx::query("UPDATE repositories SET title = $1, description = $2, visibility = $3, updated_ms = $4 WHERE id = $5 AND kind = 'drop'")
206
        .bind(title)
207
        .bind(description)
208
        .bind(visibility.as_str())
209
        .bind(now_ms())
210
        .bind(row_id)
211
        .execute(pool)
212
        .await?;
213
    Ok(())
214
}
215
216
/// One repository, with its open issue and proposal counts for the tabs.
217
pub async fn get(pool: &Pool, owner: &str, name: &str) -> sqlx::Result<Option<RepoRow>> {
218
    sqlx::query_as(
219
        "SELECT r.*, \
220
           (SELECT COUNT(*) FROM issues i WHERE i.repo_id = r.id AND i.state = 'open') AS open_issues, \
221
           (SELECT COUNT(*) FROM proposals p WHERE p.repo_id = r.id AND p.state IN ('open', 'draft')) AS open_proposals \
222
         FROM repositories r WHERE r.owner = $1 AND r.name = $2",
223
    )
224
    .bind(owner)
225
    .bind(name)
226
    .fetch_optional(pool)
227
    .await
228
}
229
230
/// Create a repository row. One under an organisation of two or more
231
/// members requires one sealed approval to land a proposal (oxygenhub#64),
232
/// since someone other than the author can then give it; others require none.
233
pub async fn create(pool: &Pool, owner: &str, name: &str, description: &str, visibility: Visibility) -> sqlx::Result<RepoRow> {
234
    let now = now_ms();
235
    sqlx::query_as(
236
        "INSERT INTO repositories (owner, name, description, visibility, created_ms, updated_ms, required_approvals) \
237
         VALUES ($1, $2, $3, $4, $5, $6, (SELECT CASE WHEN COUNT(*) >= 2 THEN 1 ELSE 0 END FROM org_members m JOIN organisations o ON o.id = m.org_id WHERE o.name = $7)) RETURNING *",
238
    )
239
    .bind(owner)
240
    .bind(name)
241
    .bind(description)
242
    .bind(visibility.as_str())
243
    .bind(now)
244
    .bind(now)
245
    .bind(owner)
246
    .fetch_one(pool)
247
    .await
248
}
249
250
/// The repository an old name now points at, after a transfer.
251
pub async fn moved_to(pool: &Pool, owner: &str, name: &str) -> sqlx::Result<Option<RepoRow>> {
252
    let id: Option<i64> = sqlx::query_scalar("SELECT repo_id FROM repo_redirects WHERE old_owner = $1 AND old_name = $2")
253
        .bind(owner)
254
        .bind(name)
255
        .fetch_optional(pool)
256
        .await?;
257
    match id {
258
        Some(id) => get_by_id(pool, id).await,
259
        None => Ok(None),
260
    }
261
}
262
263
/// Move a repository to another owner, keeping its name: the row, and a
264
/// redirect from the old name. A redirect that pointed at the new name
265
/// goes, so names never loop.
266
pub async fn transfer(pool: &Pool, id: i64, old_owner: &str, name: &str, new_owner: &str) -> sqlx::Result<()> {
267
    sqlx::query("UPDATE repositories SET owner = $1, updated_ms = $2 WHERE id = $3")
268
        .bind(new_owner)
269
        .bind(now_ms())
270
        .bind(id)
271
        .execute(pool)
272
        .await?;
273
    sqlx::query("DELETE FROM repo_redirects WHERE old_owner = $1 AND old_name = $2").bind(new_owner).bind(name).execute(pool).await?;
274
    sqlx::query("INSERT INTO repo_redirects (old_owner, old_name, repo_id, moved_ms) VALUES ($1, $2, $3, $4) ON CONFLICT (old_owner, old_name) DO UPDATE SET repo_id = excluded.repo_id, moved_ms = excluded.moved_ms")
275
        .bind(old_owner)
276
        .bind(name)
277
        .bind(id)
278
        .bind(now_ms())
279
        .execute(pool)
280
        .await?;
281
    Ok(())
282
}
283
284
/// Archive (read-only) or bring back a repository.
285
pub async fn set_archived(pool: &Pool, id: i64, archived: bool, reason: &str) -> sqlx::Result<()> {
286
    sqlx::query("UPDATE repositories SET archived = $1, archived_reason = $2, updated_ms = $3 WHERE id = $4")
287
        .bind(archived)
288
        .bind(if archived { reason } else { "" })
289
        .bind(now_ms())
290
        .bind(id)
291
        .execute(pool)
292
        .await?;
293
    Ok(())
294
}
295
296
pub async fn delete(pool: &Pool, id: i64) -> sqlx::Result<()> {
297
    sqlx::query("DELETE FROM repositories WHERE id = $1").bind(id).execute(pool).await?;
298
    Ok(())
299
}
300
301
pub async fn set_description(pool: &Pool, id: i64, description: &str) -> sqlx::Result<()> {
302
    sqlx::query("UPDATE repositories SET description = $1 WHERE id = $2").bind(description).bind(id).execute(pool).await?;
303
    Ok(())
304
}
305
306
pub async fn set_default_bookmark(pool: &Pool, id: i64, bookmark: &str) -> sqlx::Result<()> {
307
    sqlx::query("UPDATE repositories SET default_bookmark = $1 WHERE id = $2").bind(bookmark).bind(id).execute(pool).await?;
308
    Ok(())
309
}
310
311
pub async fn set_visibility(pool: &Pool, id: i64, visibility: Visibility) -> sqlx::Result<()> {
312
    sqlx::query("UPDATE repositories SET visibility = $1 WHERE id = $2").bind(visibility.as_str()).bind(id).execute(pool).await?;
313
    Ok(())
314
}
315
316
/// Record a push's per-reference results and bump the repository's activity.
317
pub async fn record_push(pool: &Pool, id: i64, pusher: Option<(i64, &str)>, results: &[PushOutcome]) -> sqlx::Result<()> {
318
    record_push_via(pool, id, pusher, "", results).await
319
}
320
321
/// [`record_push`], naming the agent token the push came through (#217).
322
pub async fn record_push_via(pool: &Pool, id: i64, pusher: Option<(i64, &str)>, via: &str, results: &[PushOutcome]) -> sqlx::Result<()> {
323
    let now = now_ms();
324
    let mut tx = super::begin_write(pool).await?;
325
    for r in results {
326
        sqlx::query("INSERT INTO pushes (repo_id, at_ms, reference, ok, reason, old_state, new_state, heads, changes, account_id, pusher, via) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)")
327
            .bind(id)
328
            .bind(now)
329
            .bind(&r.reference)
330
            .bind(r.ok)
331
            .bind(&r.reason)
332
            .bind(&r.old_state)
333
            .bind(&r.new_state)
334
            .bind(r.heads)
335
            .bind(r.changes)
336
            .bind(pusher.map(|p| p.0).filter(|id| *id > 0))
337
            .bind(pusher.map(|p| p.1).unwrap_or(""))
338
            .bind(via)
339
            .execute(&mut *tx)
340
            .await?;
341
    }
342
    if results.iter().any(|r| r.ok) {
343
        sqlx::query("UPDATE repositories SET updated_ms = $1 WHERE id = $2").bind(now).bind(id).execute(&mut *tx).await?;
344
    }
345
    tx.commit().await
346
}
347
348
pub async fn pushes(pool: &Pool, id: i64, limit: i64) -> sqlx::Result<Vec<PushRow>> {
349
    sqlx::query_as("SELECT id, at_ms, reference, ok, reason, old_state, new_state, heads, changes, pusher FROM pushes WHERE repo_id = $1 ORDER BY at_ms DESC, id DESC LIMIT $2")
350
        .bind(id)
351
        .bind(limit)
352
        .fetch_all(pool)
353
        .await
354
}
355
356
/// Topics from user input: comma or space separated, lower-cased,
357
/// letters, digits and dashes, at most 35 characters each and 20 in all.
358
/// Returns the stored form or what is wrong.
359
pub fn parse_topics(input: &str) -> Result<String, String> {
360
    let mut out: Vec<String> = Vec::new();
361
    for t in input.split([',', ' ', '\n', '\t']).map(|t| t.trim().to_ascii_lowercase()).filter(|t| !t.is_empty()) {
362
        if t.len() > 35 || !t.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') || t.starts_with('-') {
363
            return Err(format!("\"{t}\" is not a topic: use letters, digits and dashes, up to 35 characters"));
364
        }
365
        if !out.contains(&t) {
366
            out.push(t);
367
        }
368
    }
369
    if out.len() > 20 {
370
        return Err("At most 20 topics.".into());
371
    }
372
    Ok(out.join(" "))
373
}
374
375
pub async fn set_topics(pool: &Pool, id: i64, topics: &str) -> sqlx::Result<()> {
376
    sqlx::query("UPDATE repositories SET topics = $1 WHERE id = $2").bind(topics).bind(id).execute(pool).await?;
377
    Ok(())
378
}
379
380
/// Star or unstar; returns whether it is now starred.
381
pub async fn toggle_star(pool: &Pool, account_id: i64, repo_id: i64) -> sqlx::Result<bool> {
382
    let gone = sqlx::query("DELETE FROM stars WHERE account_id = $1 AND repo_id = $2").bind(account_id).bind(repo_id).execute(pool).await?;
383
    if gone.rows_affected() > 0 {
384
        return Ok(false);
385
    }
386
    sqlx::query("INSERT INTO stars (account_id, repo_id, at_ms) VALUES ($1, $2, $3)")
387
        .bind(account_id)
388
        .bind(repo_id)
389
        .bind(now_ms())
390
        .execute(pool)
391
        .await?;
392
    Ok(true)
393
}
394
395
pub async fn starred(pool: &Pool, account_id: i64, repo_id: i64) -> sqlx::Result<bool> {
396
    let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM stars WHERE account_id = $1 AND repo_id = $2")
397
        .bind(account_id)
398
        .bind(repo_id)
399
        .fetch_one(pool)
400
        .await?;
401
    Ok(n > 0)
402
}
403
404
/// Stars and watchers of one repository.
405
pub async fn counts(pool: &Pool, repo_id: i64) -> sqlx::Result<(i64, i64)> {
406
    let stars: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM stars WHERE repo_id = $1").bind(repo_id).fetch_one(pool).await?;
407
    let watchers = super::notify::watchers(pool, repo_id).await?.len() as i64;
408
    Ok((stars, watchers))
409
}
410
411
/// Star counts of every starred repository.
412
pub async fn star_counts(pool: &Pool) -> sqlx::Result<std::collections::HashMap<i64, i64>> {
413
    let rows: Vec<(i64, i64)> = sqlx::query_as("SELECT repo_id, COUNT(*) FROM stars GROUP BY repo_id").fetch_all(pool).await?;
414
    Ok(rows.into_iter().collect())
415
}
416
417
/// Record that `id` is a fork of `from`.
418
pub async fn set_forked_from(pool: &Pool, id: i64, from: i64) -> sqlx::Result<()> {
419
    sqlx::query("UPDATE repositories SET forked_from = $1 WHERE id = $2").bind(from).bind(id).execute(pool).await?;
420
    Ok(())
421
}
422
423
/// The forks of `id`, newest activity first.
424
pub async fn forks_of(pool: &Pool, id: i64) -> sqlx::Result<Vec<RepoRow>> {
425
    sqlx::query_as("SELECT * FROM repositories WHERE forked_from = $1 AND kind = 'repo' ORDER BY updated_ms DESC")
426
        .bind(id)
427
        .fetch_all(pool)
428
        .await
429
}
430
431
/// `on`, `off`, or `` (follow hub.toml's `[flow] repos`).
432
pub async fn set_flow(pool: &Pool, id: i64, setting: &str) -> sqlx::Result<()> {
433
    sqlx::query("UPDATE repositories SET flow = $1 WHERE id = $2").bind(setting).bind(id).execute(pool).await?;
434
    Ok(())
435
}
436
437
pub async fn set_flow_priority(pool: &Pool, id: i64, priority: &str) -> sqlx::Result<()> {
438
    sqlx::query("UPDATE repositories SET flow_priority = $1 WHERE id = $2").bind(priority).bind(id).execute(pool).await?;
439
    Ok(())
440
}
441
442
/// Whether agents' tokens may decide the repository's approval gates (#258).
443
pub async fn set_agents_decide_gates(pool: &Pool, id: i64, on: bool) -> sqlx::Result<()> {
444
    sqlx::query("UPDATE repositories SET agents_decide_gates = $1 WHERE id = $2").bind(on).bind(id).execute(pool).await?;
445
    Ok(())
446
}
447
448
/// Whether a new run leaves the older runs it supersedes that are already
449
/// running to finish (#267), cancelling only the queued ones.
450
pub async fn set_flow_reuse(pool: &Pool, id: i64, on: bool) -> sqlx::Result<()> {
451
    sqlx::query("UPDATE repositories SET flow_reuse = $1 WHERE id = $2").bind(on).bind(id).execute(pool).await?;
452
    Ok(())
453
}
454
455
pub async fn set_flow_skip(pool: &Pool, id: i64, skip: &str) -> sqlx::Result<()> {
456
    sqlx::query("UPDATE repositories SET flow_skip = $1 WHERE id = $2").bind(skip).bind(id).execute(pool).await?;
457
    Ok(())
458
}
459
460
pub async fn set_flow_keep_running(pool: &Pool, id: i64, on: bool) -> sqlx::Result<()> {
461
    sqlx::query("UPDATE repositories SET flow_keep_running = $1 WHERE id = $2").bind(on).bind(id).execute(pool).await?;
462
    Ok(())
463
}
464
465
pub async fn set_backup(pool: &Pool, id: i64, setting: &str) -> sqlx::Result<()> {
466
    sqlx::query("UPDATE repositories SET backup = $1 WHERE id = $2").bind(setting).bind(id).execute(pool).await?;
467
    Ok(())
468
}
469
470
pub async fn set_diffusion_land(pool: &Pool, id: i64, setting: &str) -> sqlx::Result<()> {
471
    sqlx::query("UPDATE repositories SET diffusion_land = $1 WHERE id = $2").bind(setting).bind(id).execute(pool).await?;
472
    Ok(())
473
}
474
475
pub async fn set_diffusion(pool: &Pool, id: i64, setting: &str) -> sqlx::Result<()> {
476
    sqlx::query("UPDATE repositories SET diffusion = $1 WHERE id = $2").bind(setting).bind(id).execute(pool).await?;
477
    Ok(())
478
}
479
480
pub async fn set_git_mirror(pool: &Pool, id: i64, on: bool) -> sqlx::Result<()> {
481
    sqlx::query("UPDATE repositories SET git_mirror = $1 WHERE id = $2").bind(on).bind(id).execute(pool).await?;
482
    Ok(())
483
}
484
485
pub async fn set_git_mirror_state(pool: &Pool, id: i64, state: &str) -> sqlx::Result<()> {
486
    sqlx::query("UPDATE repositories SET git_mirror_state = $1 WHERE id = $2").bind(state).bind(id).execute(pool).await?;
487
    Ok(())
488
}
489
490
#[cfg(test)]
491
mod topic_tests {
492
    use super::parse_topics;
493
494
    #[test]
495
    fn topics_are_normalised_and_checked() {
496
        assert_eq!(parse_topics("Rust, vcs  retro-computing,rust").unwrap(), "rust vcs retro-computing");
497
        assert_eq!(parse_topics("").unwrap(), "");
498
        assert!(parse_topics("c++").is_err());
499
        assert!(parse_topics("-lead").is_err());
500
        assert!(parse_topics(&"x".repeat(36)).is_err());
501
        assert!(parse_topics(&(0..21).map(|i| format!("t{i}")).collect::<Vec<_>>().join(" ")).is_err());
502
    }
503
}