OXYGENHUB

Coverage / crates/hub/src/db/issues.rs 91.1%

296 of 325 lines run, 68 of 152 functions · run #468 on main · run missed

1
//! Issues, labels, milestones and their events (hub spec §8).
2
3
use super::{now_ms, Pool};
4
use sqlx::FromRow;
5
6
#[derive(Clone, Debug, FromRow)]
7
pub struct IssueRow {
8
    pub id: i64,
9
    pub repo_id: i64,
10
    pub number: i64,
11
    pub title: String,
12
    pub body: String,
13
    pub author_id: Option<i64>,
14
    pub author: String,
15
    pub state: String,
16
    pub reason: String,
17
    pub locked: bool,
18
    pub pinned: bool,
19
    pub milestone_id: Option<i64>,
20
    pub created_ms: i64,
21
    pub updated_ms: i64,
22
    pub closed_ms: Option<i64>,
23
}
24
25
#[derive(Clone, Debug, FromRow)]
26
pub struct IssueComment {
27
    pub id: i64,
28
    pub issue_id: i64,
29
    pub author_id: Option<i64>,
30
    pub author: String,
31
    pub body: String,
32
    pub created_ms: i64,
33
    pub edited_ms: Option<i64>,
34
}
35
36
#[derive(Clone, Debug, FromRow)]
37
pub struct IssueEvent {
38
    pub id: i64,
39
    pub at_ms: i64,
40
    pub actor: String,
41
    pub event: String,
42
    pub detail: String,
43
}
44
45
#[derive(Clone, Debug, FromRow, PartialEq, Eq)]
46
pub struct Label {
47
    pub id: i64,
48
    pub repo_id: i64,
49
    pub name: String,
50
    pub colour: String,
51
    pub description: String,
52
}
53
54
#[derive(Clone, Debug, FromRow)]
55
pub struct Milestone {
56
    pub id: i64,
57
    pub repo_id: i64,
58
    pub title: String,
59
    pub description: String,
60
    pub due_ms: Option<i64>,
61
    pub state: String,
62
}
63
64
pub struct NewIssue<'a> {
65
    pub repo_id: i64,
66
    pub title: &'a str,
67
    pub body: &'a str,
68
    pub author_id: Option<i64>,
69
    pub author: &'a str,
70
}
71
7262×
pub async fn create(pool: &Pool, i: NewIssue<'_>) -> sqlx::Result<IssueRow> {
7362×
    let number = super::proposals::next_number(pool, i.repo_id).await?;
7462×
    let now = now_ms();
7562×
    sqlx::query_as("INSERT INTO issues (repo_id, number, title, body, author_id, author, created_ms, updated_ms) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *")
7662×
        .bind(i.repo_id)
7762×
        .bind(number)
7862×
        .bind(i.title)
7962×
        .bind(i.body)
8062×
        .bind(i.author_id)
8162×
        .bind(i.author)
8262×
        .bind(now)
8362×
        .bind(now)
8462×
        .fetch_one(pool)
8562×
        .await
8662×
}
87
88121×
pub async fn get(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<IssueRow>> {
89121×
    sqlx::query_as("SELECT * FROM issues WHERE repo_id = $1 AND number = $2").bind(repo_id).bind(number).fetch_optional(pool).await
90121×
}
91
920×
pub async fn get_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<IssueRow>> {
930×
    sqlx::query_as("SELECT * FROM issues WHERE id = $1").bind(id).fetch_optional(pool).await
940×
}
95
9657×
pub async fn list(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<IssueRow>> {
9757×
    sqlx::query_as("SELECT * FROM issues WHERE repo_id = $1 ORDER BY pinned DESC, number DESC").bind(repo_id).fetch_all(pool).await
9857×
}
99
100
/// Numbers in use by issues and proposals, for cross references.
101385×
pub async fn numbers(pool: &Pool, repo_id: i64) -> sqlx::Result<(Vec<i64>, Vec<i64>)> {
102385×
    let issues: Vec<i64> = sqlx::query_scalar("SELECT number FROM issues WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await?;
103385×
    let proposals: Vec<i64> = sqlx::query_scalar("SELECT number FROM proposals WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await?;
104385×
    Ok((issues, proposals))
105385×
}
106
1070×
pub async fn assigned_to(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<IssueRow>> {
1080×
    sqlx::query_as("SELECT i.* FROM issues i JOIN issue_assignees a ON a.issue_id = i.id WHERE a.account_id = $1 AND i.state = 'open' ORDER BY i.updated_ms DESC")
1090×
        .bind(account_id)
1100×
        .fetch_all(pool)
1110×
        .await
1120×
}
113
1142×
pub async fn edit(pool: &Pool, id: i64, title: &str, body: &str) -> sqlx::Result<()> {
1152×
    sqlx::query("UPDATE issues SET title = $1, body = $2, updated_ms = $3 WHERE id = $4")
1162×
        .bind(title)
1172×
        .bind(body)
1182×
        .bind(now_ms())
1192×
        .bind(id)
1202×
        .execute(pool)
1212×
        .await?;
1222×
    Ok(())
1232×
}
124
1258×
pub async fn set_state(pool: &Pool, id: i64, state: &str, reason: &str) -> sqlx::Result<()> {
1268×
    let now = now_ms();
1278×
    let closed: Option<i64> = if state == "closed" { Some(now) } else { None };
1288×
    sqlx::query("UPDATE issues SET state = $1, reason = $2, closed_ms = $3, updated_ms = $4 WHERE id = $5")
1298×
        .bind(state)
1308×
        .bind(reason)
1318×
        .bind(closed)
1328×
        .bind(now)
1338×
        .bind(id)
1348×
        .execute(pool)
1358×
        .await?;
1368×
    Ok(())
1378×
}
138
1391×
pub async fn set_flags(pool: &Pool, id: i64, locked: Option<bool>, pinned: Option<bool>) -> sqlx::Result<()> {
1401×
    if let Some(l) = locked {
1410×
        sqlx::query("UPDATE issues SET locked = $1 WHERE id = $2").bind(l).bind(id).execute(pool).await?;
1421×
    }
1431×
    if let Some(p) = pinned {
1441×
        sqlx::query("UPDATE issues SET pinned = $1 WHERE id = $2").bind(p).bind(id).execute(pool).await?;
1450×
    }
1461×
    Ok(())
1471×
}
148
1492×
pub async fn set_milestone(pool: &Pool, id: i64, milestone_id: Option<i64>) -> sqlx::Result<()> {
1502×
    sqlx::query("UPDATE issues SET milestone_id = $1, updated_ms = $2 WHERE id = $3")
1512×
        .bind(milestone_id)
1522×
        .bind(now_ms())
1532×
        .bind(id)
1542×
        .execute(pool)
1552×
        .await?;
1562×
    Ok(())
1572×
}
158
15913×
pub async fn touch(pool: &Pool, id: i64) -> sqlx::Result<()> {
16013×
    sqlx::query("UPDATE issues SET updated_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?;
16113×
    Ok(())
16213×
}
163
16463×
pub async fn comments(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<IssueComment>> {
16563×
    sqlx::query_as("SELECT * FROM issue_comments WHERE issue_id = $1 ORDER BY created_ms, id").bind(issue_id).fetch_all(pool).await
16663×
}
167
1689×
pub async fn add_comment(pool: &Pool, issue_id: i64, author_id: Option<i64>, author: &str, body: &str) -> sqlx::Result<IssueComment> {
1699×
    let row = sqlx::query_as(
1709×
        "INSERT INTO issue_comments (issue_id, author_id, author, body, created_ms) VALUES ($1, $2, $3, $4, $5) RETURNING *",
1719×
    )
1729×
    .bind(issue_id)
1739×
    .bind(author_id)
1749×
    .bind(author)
1759×
    .bind(body)
1769×
    .bind(now_ms())
1779×
    .fetch_one(pool)
1789×
    .await?;
1799×
    touch(pool, issue_id).await?;
1809×
    Ok(row)
1819×
}
182
1830×
pub async fn edit_comment(pool: &Pool, id: i64, author_id: i64, body: &str) -> sqlx::Result<bool> {
1840×
    let n = sqlx::query("UPDATE issue_comments SET body = $1, edited_ms = $2 WHERE id = $3 AND author_id = $4")
1850×
        .bind(body)
1860×
        .bind(now_ms())
1870×
        .bind(id)
1880×
        .bind(author_id)
1890×
        .execute(pool)
1900×
        .await?;
1910×
    Ok(n.rows_affected() > 0)
1920×
}
193
1940×
pub async fn delete_comment(pool: &Pool, id: i64) -> sqlx::Result<()> {
1950×
    sqlx::query("DELETE FROM issue_comments WHERE id = $1").bind(id).execute(pool).await?;
1960×
    Ok(())
1970×
}
198
19927×
pub async fn events(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<IssueEvent>> {
20027×
    sqlx::query_as("SELECT id, at_ms, actor, event, detail FROM issue_events WHERE issue_id = $1 ORDER BY at_ms, id")
20127×
        .bind(issue_id)
20227×
        .fetch_all(pool)
20327×
        .await
20427×
}
205
206
/// Whether change `hash` (its short form) already references it (#318):
207
/// a change is recorded once, whichever bookmarks it is pushed on.
2082×
pub async fn referenced_by(pool: &Pool, issue_id: i64, hash: &str) -> sqlx::Result<bool> {
2092×
    let n: i64 = sqlx::query_scalar(
2102×
        "SELECT COUNT(*) FROM issue_events WHERE issue_id = $1 AND event = 'referenced by change' AND (detail = $2 OR detail LIKE $3)",
2112×
    )
2122×
    .bind(issue_id)
2132×
    .bind(hash)
2142×
    .bind(format!("{hash} %"))
2152×
    .fetch_one(pool)
2162×
    .await?;
2172×
    Ok(n > 0)
2182×
}
219
22046×
pub async fn add_event(pool: &Pool, issue_id: i64, actor: &str, event: &str, detail: &str) -> sqlx::Result<()> {
22146×
    sqlx::query("INSERT INTO issue_events (issue_id, at_ms, actor, event, detail) VALUES ($1, $2, $3, $4, $5)")
22246×
        .bind(issue_id)
22346×
        .bind(now_ms())
22446×
        .bind(actor)
22546×
        .bind(event)
22646×
        .bind(detail)
22746×
        .execute(pool)
22846×
        .await?;
22946×
    Ok(())
23046×
}
231
232
// ------------------------------------------------------------------ labels
233
234106×
pub async fn labels(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<Label>> {
235106×
    sqlx::query_as("SELECT * FROM labels WHERE repo_id = $1 ORDER BY name").bind(repo_id).fetch_all(pool).await
236106×
}
237
23824×
pub async fn label_by_name(pool: &Pool, repo_id: i64, name: &str) -> sqlx::Result<Option<Label>> {
23924×
    sqlx::query_as("SELECT * FROM labels WHERE repo_id = $1 AND name = $2").bind(repo_id).bind(name).fetch_optional(pool).await
24024×
}
241
24223×
pub async fn upsert_label(pool: &Pool, repo_id: i64, name: &str, colour: &str, description: &str) -> sqlx::Result<Label> {
24323×
    sqlx::query_as("INSERT INTO labels (repo_id, name, colour, description) VALUES ($1, $2, $3, $4) ON CONFLICT (repo_id, name) DO UPDATE SET colour = excluded.colour, description = excluded.description RETURNING *")
24423×
        .bind(repo_id)
24523×
        .bind(name)
24623×
        .bind(colour)
24723×
        .bind(description)
24823×
        .fetch_one(pool)
24923×
        .await
25023×
}
251
2521×
pub async fn delete_label(pool: &Pool, id: i64) -> sqlx::Result<()> {
2531×
    sqlx::query("DELETE FROM labels WHERE id = $1").bind(id).execute(pool).await?;
2541×
    Ok(())
2551×
}
256
25761×
pub async fn issue_labels(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<Label>> {
25861×
    sqlx::query_as("SELECT l.* FROM labels l JOIN issue_labels il ON il.label_id = l.id WHERE il.issue_id = $1 ORDER BY l.name")
25961×
        .bind(issue_id)
26061×
        .fetch_all(pool)
26161×
        .await
26261×
}
263
26410×
pub async fn set_issue_label(pool: &Pool, issue_id: i64, label_id: i64, on: bool) -> sqlx::Result<()> {
26510×
    if on {
2668×
        sqlx::query("INSERT INTO issue_labels (issue_id, label_id) VALUES ($1, $2) ON CONFLICT DO NOTHING")
2678×
            .bind(issue_id)
2688×
            .bind(label_id)
2698×
            .execute(pool)
2708×
            .await?;
271
    } else {
2722×
        sqlx::query("DELETE FROM issue_labels WHERE issue_id = $1 AND label_id = $2").bind(issue_id).bind(label_id).execute(pool).await?;
273
    }
27410×
    Ok(())
27510×
}
276
277
/// Labels of every issue of a repository, for the list page.
278
/// Every proposal's labels in a repository, for filtering the list.
27916×
pub async fn labels_by_proposal(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<(i64, Label)>> {
28016×
    let rows: Vec<(i64, i64, i64, String, String, String)> = sqlx::query_as(
28116×
        "SELECT pl.proposal_id, l.id, l.repo_id, l.name, l.colour, l.description FROM proposal_labels pl JOIN labels l ON l.id = pl.label_id JOIN proposals p ON p.id = pl.proposal_id WHERE p.repo_id = $1 ORDER BY l.name",
28216×
    )
28316×
    .bind(repo_id)
28416×
    .fetch_all(pool)
28516×
    .await?;
28616×
    Ok(rows
28716×
        .into_iter()
28816×
        .map(|(proposal_id, id, repo_id, name, colour, description)| (proposal_id, Label { id, repo_id, name, colour, description }))
28916×
        .collect())
29016×
}
291
29252×
pub async fn labels_by_issue(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<(i64, Label)>> {
29352×
    let rows: Vec<(i64, i64, i64, String, String, String)> = sqlx::query_as(
29452×
        "SELECT il.issue_id, l.id, l.repo_id, l.name, l.colour, l.description FROM issue_labels il JOIN labels l ON l.id = il.label_id JOIN issues i ON i.id = il.issue_id WHERE i.repo_id = $1 ORDER BY l.name",
29552×
    )
29652×
    .bind(repo_id)
29752×
    .fetch_all(pool)
29852×
    .await?;
29952×
    Ok(rows
30052×
        .into_iter()
30152×
        .map(|(issue_id, id, repo_id, name, colour, description)| (issue_id, Label { id, repo_id, name, colour, description }))
30252×
        .collect())
30352×
}
304
305115×
pub async fn proposal_labels(pool: &Pool, proposal_id: i64) -> sqlx::Result<Vec<Label>> {
306115×
    sqlx::query_as("SELECT l.* FROM labels l JOIN proposal_labels pl ON pl.label_id = l.id WHERE pl.proposal_id = $1 ORDER BY l.name")
307115×
        .bind(proposal_id)
308115×
        .fetch_all(pool)
309115×
        .await
310115×
}
311
31214×
pub async fn set_proposal_label(pool: &Pool, proposal_id: i64, label_id: i64, on: bool) -> sqlx::Result<()> {
31314×
    if on {
31413×
        sqlx::query("INSERT INTO proposal_labels (proposal_id, label_id) VALUES ($1, $2) ON CONFLICT DO NOTHING")
31513×
            .bind(proposal_id)
31613×
            .bind(label_id)
31713×
            .execute(pool)
31813×
            .await?;
319
    } else {
3201×
        sqlx::query("DELETE FROM proposal_labels WHERE proposal_id = $1 AND label_id = $2")
3211×
            .bind(proposal_id)
3221×
            .bind(label_id)
3231×
            .execute(pool)
3241×
            .await?;
325
    }
32614×
    Ok(())
32714×
}
328
329
// --------------------------------------------------------------- assignees
330
33167×
pub async fn assignees(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<String>> {
33267×
    sqlx::query_scalar(
33367×
        "SELECT a.username FROM accounts a JOIN issue_assignees ia ON ia.account_id = a.id WHERE ia.issue_id = $1 ORDER BY a.username",
33467×
    )
33567×
    .bind(issue_id)
33667×
    .fetch_all(pool)
33767×
    .await
33867×
}
339
34010×
pub async fn set_assignee(pool: &Pool, issue_id: i64, account_id: i64, on: bool) -> sqlx::Result<()> {
34110×
    if on {
3427×
        sqlx::query("INSERT INTO issue_assignees (issue_id, account_id) VALUES ($1, $2) ON CONFLICT DO NOTHING")
3437×
            .bind(issue_id)
3447×
            .bind(account_id)
3457×
            .execute(pool)
3467×
            .await?;
347
    } else {
3483×
        sqlx::query("DELETE FROM issue_assignees WHERE issue_id = $1 AND account_id = $2")
3493×
            .bind(issue_id)
3503×
            .bind(account_id)
3513×
            .execute(pool)
3523×
            .await?;
353
    }
35410×
    Ok(())
35510×
}
356
357
/// Assignees of every issue of a repository, for the list page.
35852×
pub async fn assignees_by_issue(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<(i64, String)>> {
35952×
    sqlx::query_as("SELECT ia.issue_id, a.username FROM issue_assignees ia JOIN accounts a ON a.id = ia.account_id JOIN issues i ON i.id = ia.issue_id WHERE i.repo_id = $1")
36052×
        .bind(repo_id)
36152×
        .fetch_all(pool)
36252×
        .await
36352×
}
364
365
// -------------------------------------------------------------- milestones
366
36741×
pub async fn milestones(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<Milestone>> {
36841×
    sqlx::query_as("SELECT * FROM milestones WHERE repo_id = $1 ORDER BY state, due_ms, title").bind(repo_id).fetch_all(pool).await
36941×
}
370
37111×
pub async fn milestone(pool: &Pool, id: i64) -> sqlx::Result<Option<Milestone>> {
37211×
    sqlx::query_as("SELECT * FROM milestones WHERE id = $1").bind(id).fetch_optional(pool).await
37311×
}
374
3753×
pub async fn upsert_milestone(pool: &Pool, repo_id: i64, title: &str, description: &str, due_ms: Option<i64>) -> sqlx::Result<Milestone> {
3763×
    sqlx::query_as("INSERT INTO milestones (repo_id, title, description, due_ms) VALUES ($1, $2, $3, $4) ON CONFLICT (repo_id, title) DO UPDATE SET description = excluded.description, due_ms = excluded.due_ms RETURNING *")
3773×
        .bind(repo_id)
3783×
        .bind(title)
3793×
        .bind(description)
3803×
        .bind(due_ms)
3813×
        .fetch_one(pool)
3823×
        .await
3833×
}
384
385
/// Change a milestone's title, description and due date.
3861×
pub async fn update_milestone(pool: &Pool, id: i64, title: &str, description: &str, due_ms: Option<i64>) -> sqlx::Result<()> {
3871×
    sqlx::query("UPDATE milestones SET title = $1, description = $2, due_ms = $3 WHERE id = $4")
3881×
        .bind(title)
3891×
        .bind(description)
3901×
        .bind(due_ms)
3911×
        .bind(id)
3921×
        .execute(pool)
3931×
        .await?;
3941×
    Ok(())
3951×
}
396
3971×
pub async fn set_milestone_state(pool: &Pool, id: i64, state: &str) -> sqlx::Result<()> {
3981×
    sqlx::query("UPDATE milestones SET state = $1 WHERE id = $2").bind(state).bind(id).execute(pool).await?;
3991×
    Ok(())
4001×
}
401
402
/// `(open, closed)` issue counts per milestone.
4035×
pub async fn milestone_counts(pool: &Pool, milestone_id: i64) -> sqlx::Result<(i64, i64)> {
4045×
    let open: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM issues WHERE milestone_id = $1 AND state = 'open'")
4055×
        .bind(milestone_id)
4065×
        .fetch_one(pool)
4075×
        .await?;
4085×
    let closed: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM issues WHERE milestone_id = $1 AND state = 'closed'")
4095×
        .bind(milestone_id)
4105×
        .fetch_one(pool)
4115×
        .await?;
4125×
    Ok((open, closed))
4135×
}