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