//! Issues, labels, milestones and their events (hub spec ยง8). use super::{now_ms, Pool}; use sqlx::FromRow; #[derive(Clone, Debug, FromRow)] pub struct IssueRow { pub id: i64, pub repo_id: i64, pub number: i64, pub title: String, pub body: String, pub author_id: Option, pub author: String, pub state: String, pub reason: String, pub locked: bool, pub pinned: bool, pub milestone_id: Option, pub created_ms: i64, pub updated_ms: i64, pub closed_ms: Option, } #[derive(Clone, Debug, FromRow)] pub struct IssueComment { pub id: i64, pub issue_id: i64, pub author_id: Option, pub author: String, pub body: String, pub created_ms: i64, pub edited_ms: Option, } #[derive(Clone, Debug, FromRow)] pub struct IssueEvent { pub id: i64, pub at_ms: i64, pub actor: String, pub event: String, pub detail: String, } #[derive(Clone, Debug, FromRow, PartialEq, Eq)] pub struct Label { pub id: i64, pub repo_id: i64, pub name: String, pub colour: String, pub description: String, } #[derive(Clone, Debug, FromRow)] pub struct Milestone { pub id: i64, pub repo_id: i64, pub title: String, pub description: String, pub due_ms: Option, pub state: String, } pub struct NewIssue<'a> { pub repo_id: i64, pub title: &'a str, pub body: &'a str, pub author_id: Option, pub author: &'a str, } pub async fn create(pool: &Pool, i: NewIssue<'_>) -> sqlx::Result { let number = super::proposals::next_number(pool, i.repo_id).await?; let now = now_ms(); 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 *") .bind(i.repo_id) .bind(number) .bind(i.title) .bind(i.body) .bind(i.author_id) .bind(i.author) .bind(now) .bind(now) .fetch_one(pool) .await } pub async fn get(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM issues WHERE repo_id = $1 AND number = $2").bind(repo_id).bind(number).fetch_optional(pool).await } pub async fn get_by_id(pool: &Pool, id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM issues WHERE id = $1").bind(id).fetch_optional(pool).await } pub async fn list(pool: &Pool, repo_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM issues WHERE repo_id = $1 ORDER BY pinned DESC, number DESC").bind(repo_id).fetch_all(pool).await } /// Numbers in use by issues and proposals, for cross references. pub async fn numbers(pool: &Pool, repo_id: i64) -> sqlx::Result<(Vec, Vec)> { let issues: Vec = sqlx::query_scalar("SELECT number FROM issues WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await?; let proposals: Vec = sqlx::query_scalar("SELECT number FROM proposals WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await?; Ok((issues, proposals)) } pub async fn assigned_to(pool: &Pool, account_id: i64) -> sqlx::Result> { 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") .bind(account_id) .fetch_all(pool) .await } pub async fn edit(pool: &Pool, id: i64, title: &str, body: &str) -> sqlx::Result<()> { sqlx::query("UPDATE issues SET title = $1, body = $2, updated_ms = $3 WHERE id = $4") .bind(title) .bind(body) .bind(now_ms()) .bind(id) .execute(pool) .await?; Ok(()) } pub async fn set_state(pool: &Pool, id: i64, state: &str, reason: &str) -> sqlx::Result<()> { let now = now_ms(); let closed: Option = if state == "closed" { Some(now) } else { None }; sqlx::query("UPDATE issues SET state = $1, reason = $2, closed_ms = $3, updated_ms = $4 WHERE id = $5") .bind(state) .bind(reason) .bind(closed) .bind(now) .bind(id) .execute(pool) .await?; Ok(()) } pub async fn set_flags(pool: &Pool, id: i64, locked: Option, pinned: Option) -> sqlx::Result<()> { if let Some(l) = locked { sqlx::query("UPDATE issues SET locked = $1 WHERE id = $2").bind(l).bind(id).execute(pool).await?; } if let Some(p) = pinned { sqlx::query("UPDATE issues SET pinned = $1 WHERE id = $2").bind(p).bind(id).execute(pool).await?; } Ok(()) } pub async fn set_milestone(pool: &Pool, id: i64, milestone_id: Option) -> sqlx::Result<()> { sqlx::query("UPDATE issues SET milestone_id = $1, updated_ms = $2 WHERE id = $3") .bind(milestone_id) .bind(now_ms()) .bind(id) .execute(pool) .await?; Ok(()) } pub async fn touch(pool: &Pool, id: i64) -> sqlx::Result<()> { sqlx::query("UPDATE issues SET updated_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?; Ok(()) } pub async fn comments(pool: &Pool, issue_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM issue_comments WHERE issue_id = $1 ORDER BY created_ms, id").bind(issue_id).fetch_all(pool).await } pub async fn add_comment(pool: &Pool, issue_id: i64, author_id: Option, author: &str, body: &str) -> sqlx::Result { let row = sqlx::query_as( "INSERT INTO issue_comments (issue_id, author_id, author, body, created_ms) VALUES ($1, $2, $3, $4, $5) RETURNING *", ) .bind(issue_id) .bind(author_id) .bind(author) .bind(body) .bind(now_ms()) .fetch_one(pool) .await?; touch(pool, issue_id).await?; Ok(row) } pub async fn edit_comment(pool: &Pool, id: i64, author_id: i64, body: &str) -> sqlx::Result { let n = sqlx::query("UPDATE issue_comments SET body = $1, edited_ms = $2 WHERE id = $3 AND author_id = $4") .bind(body) .bind(now_ms()) .bind(id) .bind(author_id) .execute(pool) .await?; Ok(n.rows_affected() > 0) } pub async fn delete_comment(pool: &Pool, id: i64) -> sqlx::Result<()> { sqlx::query("DELETE FROM issue_comments WHERE id = $1").bind(id).execute(pool).await?; Ok(()) } pub async fn events(pool: &Pool, issue_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT id, at_ms, actor, event, detail FROM issue_events WHERE issue_id = $1 ORDER BY at_ms, id") .bind(issue_id) .fetch_all(pool) .await } pub async fn add_event(pool: &Pool, issue_id: i64, actor: &str, event: &str, detail: &str) -> sqlx::Result<()> { sqlx::query("INSERT INTO issue_events (issue_id, at_ms, actor, event, detail) VALUES ($1, $2, $3, $4, $5)") .bind(issue_id) .bind(now_ms()) .bind(actor) .bind(event) .bind(detail) .execute(pool) .await?; Ok(()) } // ------------------------------------------------------------------ labels pub async fn labels(pool: &Pool, repo_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM labels WHERE repo_id = $1 ORDER BY name").bind(repo_id).fetch_all(pool).await } pub async fn label_by_name(pool: &Pool, repo_id: i64, name: &str) -> sqlx::Result> { sqlx::query_as("SELECT * FROM labels WHERE repo_id = $1 AND name = $2").bind(repo_id).bind(name).fetch_optional(pool).await } pub async fn upsert_label(pool: &Pool, repo_id: i64, name: &str, colour: &str, description: &str) -> sqlx::Result