| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::{open, ui}; |
| 6 | | use anyhow::{anyhow, Context, Result}; |
| 7 | | use oxy_core::hash::ChangeHash; |
| 8 | | use oxy_core::repo::Repo; |
| 9 | | use serde_json::Value; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | 0× | pub fn api_url(url: &str) -> Result<(String, String)> { |
| 14 | 0× | let url = url.trim_end_matches('/'); |
| 15 | 0× | if url.contains("/api/v1/repos/") { |
| 16 | 0× | let (base, rest) = url.split_once("/api/v1/repos/").unwrap(); |
| 17 | 0× | let mut parts = rest.split('/'); |
| 18 | 0× | let (owner, name) = (parts.next().unwrap_or(""), parts.next().unwrap_or("")); |
| 19 | 0× | return Ok((url.to_string(), format!("{base}/{owner}/{name}"))); |
| 20 | 0× | } |
| 21 | 0× | let (repo_url, number) = url |
| 22 | 0× | .rsplit_once("/proposal/") |
| 23 | 0× | .ok_or_else(|| anyhow!("{url} is not a proposal URL (…/OWNER/NAME/proposal/N)"))?; |
| 24 | 0× | let (base, slug) = repo_url |
| 25 | 0× | .rsplit_once('/') |
| 26 | 0× | .and_then(|(b, n)| b.rsplit_once('/').map(|(bb, o)| (bb, format!("{o}/{n}")))) |
| 27 | 0× | .ok_or_else(|| anyhow!("{url} is not a proposal URL"))?; |
| 28 | 0× | Ok(( |
| 29 | 0× | format!("{base}/api/v1/repos/{slug}/proposals/{number}"), |
| 30 | 0× | repo_url.to_string(), |
| 31 | 0× | )) |
| 32 | 0× | } |
| 33 | |
|
| 34 | 0× | pub fn fetch(repo: &Repo, url: &str) -> Result<(Value, String)> { |
| 35 | 0× | let (api, repo_url) = api_url(url)?; |
| 36 | 0× | let options = repo.options_for_url(&repo_url); |
| 37 | 0× | let body = oxy_core::protocol::http_get(&api, &options).map_err(|e| anyhow!("{e}"))?; |
| 38 | 0× | let v: Value = serde_json::from_slice(&body).context("the hub did not answer with JSON")?; |
| 39 | 0× | Ok((v, repo_url)) |
| 40 | 0× | } |
| 41 | |
|
| 42 | 0× | fn hashes(v: &Value, key: &str) -> Vec<ChangeHash> { |
| 43 | 0× | v.get(key) |
| 44 | 0× | .and_then(Value::as_array) |
| 45 | 0× | .map(|a| { |
| 46 | 0× | a.iter() |
| 47 | 0× | .filter_map(|x| x.as_str().and_then(ChangeHash::from_hex)) |
| 48 | 0× | .collect() |
| 49 | 0× | }) |
| 50 | 0× | .unwrap_or_default() |
| 51 | 0× | } |
| 52 | |
|
| 53 | 0× | fn change_hashes(v: &Value) -> Vec<ChangeHash> { |
| 54 | 0× | v.get("changes") |
| 55 | 0× | .and_then(Value::as_array) |
| 56 | 0× | .map(|a| { |
| 57 | 0× | a.iter() |
| 58 | 0× | .filter_map(|c| { |
| 59 | 0× | c.get("hash") |
| 60 | 0× | .and_then(Value::as_str) |
| 61 | 0× | .and_then(ChangeHash::from_hex) |
| 62 | 0× | }) |
| 63 | 0× | .collect() |
| 64 | 0× | }) |
| 65 | 0× | .unwrap_or_default() |
| 66 | 0× | } |
| 67 | |
|
| 68 | 0× | fn text<'a>(v: &'a Value, key: &str) -> &'a str { |
| 69 | 0× | v.get(key).and_then(Value::as_str).unwrap_or("") |
| 70 | 0× | } |
| 71 | |
|
| 72 | 0× | pub fn show(url: &str, as_json: bool) -> Result<i32> { |
| 73 | 0× | let repo = open()?; |
| 74 | 0× | let (v, _) = fetch(&repo, url)?; |
| 75 | 0× | if as_json { |
| 76 | 0× | println!("{v}"); |
| 77 | 0× | return Ok(0); |
| 78 | 0× | } |
| 79 | 0× | println!( |
| 80 | | "{} #{} {}", |
| 81 | 0× | ui::done("Proposal"), |
| 82 | 0× | v.get("number").and_then(Value::as_i64).unwrap_or(0), |
| 83 | 0× | ui::bookmark(text(&v, "title")) |
| 84 | | ); |
| 85 | 0× | println!("{}", ui::kv("state", text(&v, "state"))); |
| 86 | 0× | println!("{}", ui::kv("target", ui::bookmark(text(&v, "target")))); |
| 87 | 0× | println!("{}", ui::kv("author", ui::author(text(&v, "author")))); |
| 88 | 0× | let changes = v |
| 89 | 0× | .get("changes") |
| 90 | 0× | .and_then(Value::as_array) |
| 91 | 0× | .cloned() |
| 92 | 0× | .unwrap_or_default(); |
| 93 | 0× | println!("{}", ui::kv("changes", changes.len().to_string())); |
| 94 | 0× | for c in &changes { |
| 95 | 0× | println!( |
| 96 | 0× | " {} {}", |
| 97 | 0× | ui::hash(&text(c, "hash")[..12.min(text(c, "hash").len())]), |
| 98 | 0× | text(c, "message").lines().next().unwrap_or("") |
| 99 | 0× | ); |
| 100 | 0× | } |
| 101 | 0× | if v.get("conflicted") |
| 102 | 0× | .and_then(Value::as_bool) |
| 103 | 0× | .unwrap_or(false) |
| 104 | 0× | { |
| 105 | 0× | println!("{}", ui::warn("landing would carry conflicts")); |
| 106 | 0× | } |
| 107 | 0× | if let Some(blockers) = v.get("blockers").and_then(Value::as_array) { |
| 108 | 0× | for b in blockers { |
| 109 | 0× | println!(" {} {}", ui::warn("blocked:"), b.as_str().unwrap_or("")); |
| 110 | 0× | } |
| 111 | 0× | } |
| 112 | 0× | let approvals = v |
| 113 | 0× | .get("approvals") |
| 114 | 0× | .and_then(Value::as_array) |
| 115 | 0× | .cloned() |
| 116 | 0× | .unwrap_or_default(); |
| 117 | 0× | for a in &approvals { |
| 118 | 0× | let mark = if a.get("sealed").and_then(Value::as_bool).unwrap_or(false) { |
| 119 | 0× | ui::done("approved") |
| 120 | | } else { |
| 121 | 0× | ui::warn("pending seal") |
| 122 | | }; |
| 123 | 0× | println!(" {mark} {}", text(a, "account")); |
| 124 | | } |
| 125 | 0× | Ok(0) |
| 126 | 0× | } |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | 0× | pub fn seal(url: &str, as_json: bool) -> Result<i32> { |
| 131 | 0× | let repo = open()?; |
| 132 | 0× | let (v, repo_url) = fetch(&repo, url)?; |
| 133 | 0× | let hashes = change_hashes(&v); |
| 134 | 0× | if hashes.is_empty() { |
| 135 | 0× | anyhow::bail!("the proposal has no changes to seal"); |
| 136 | 0× | } |
| 137 | 0× | ensure_changes(&repo, &repo_url, &hashes, &hashes)?; |
| 138 | 0× | let mut refs = Vec::new(); |
| 139 | 0× | for chunk in hashes.chunks(oxy_core::attest::MAX_SEAL_HASHES) { |
| 140 | 0× | refs.push(repo.seal(chunk)?); |
| 141 | | } |
| 142 | 0× | let remote = repo.remote_for_url(&repo_url).ok_or_else(|| { |
| 143 | 0× | anyhow!("no remote is configured for {repo_url}; add one with `oxy remote add`") |
| 144 | 0× | })?; |
| 145 | 0× | repo.push_seals(&remote, &refs)?; |
| 146 | 0× | if as_json { |
| 147 | 0× | let list: Vec<String> = hashes |
| 148 | 0× | .iter() |
| 149 | 0× | .map(|h| format!("\"{}\"", h.to_hex())) |
| 150 | 0× | .collect(); |
| 151 | 0× | println!("{{\"sealed\":[{}],\"pushed\":true}}", list.join(",")); |
| 152 | | } else { |
| 153 | 0× | println!( |
| 154 | | "{} {} change{} of the proposal and pushed the seal to {remote}", |
| 155 | 0× | ui::done("Sealed"), |
| 156 | 0× | hashes.len(), |
| 157 | 0× | if hashes.len() == 1 { "" } else { "s" } |
| 158 | | ); |
| 159 | | } |
| 160 | 0× | Ok(0) |
| 161 | 0× | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | 0× | fn ensure_changes( |
| 166 | 0× | repo: &Repo, |
| 167 | 0× | repo_url: &str, |
| 168 | 0× | want: &[ChangeHash], |
| 169 | 0× | _changes: &[ChangeHash], |
| 170 | 0× | ) -> Result<()> { |
| 171 | 0× | let missing: Vec<ChangeHash> = want |
| 172 | 0× | .iter() |
| 173 | 0× | .copied() |
| 174 | 0× | .filter(|h| !repo.store().has(h)) |
| 175 | 0× | .collect(); |
| 176 | 0× | if missing.is_empty() { |
| 177 | 0× | return Ok(()); |
| 178 | 0× | } |
| 179 | 0× | let (_, remote) = repo.open_remote_at(repo_url)?; |
| 180 | 0× | let view = repo.view()?; |
| 181 | 0× | let have = repo.known_heads(&view)?; |
| 182 | 0× | let needed = remote.negotiate(&have, want)?; |
| 183 | 0× | let bundle = remote.fetch(&needed, repo.config.max_blob)?; |
| 184 | 0× | repo.import_bundle(&bundle, false)?; |
| 185 | 0× | Ok(()) |
| 186 | 0× | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | 0× | pub fn land(url: &str, force: bool) -> Result<i32> { |
| 192 | 0× | let repo = open()?; |
| 193 | 0× | let (v, repo_url) = fetch(&repo, url)?; |
| 194 | 0× | let state = text(&v, "state"); |
| 195 | 0× | if state != "open" && !force { |
| 196 | 0× | anyhow::bail!("the proposal is {state}, not open (use --force to land anyway)"); |
| 197 | 0× | } |
| 198 | 0× | if !v.get("landable").and_then(Value::as_bool).unwrap_or(false) && !force { |
| 199 | 0× | let blockers: Vec<String> = v |
| 200 | 0× | .get("blockers") |
| 201 | 0× | .and_then(Value::as_array) |
| 202 | 0× | .map(|a| { |
| 203 | 0× | a.iter() |
| 204 | 0× | .filter_map(|b| b.as_str().map(String::from)) |
| 205 | 0× | .collect() |
| 206 | 0× | }) |
| 207 | 0× | .unwrap_or_default(); |
| 208 | 0× | anyhow::bail!( |
| 209 | | "the hub says the proposal cannot land yet: {} (use --force to land anyway)", |
| 210 | 0× | blockers.join("; ") |
| 211 | | ); |
| 212 | 0× | } |
| 213 | 0× | let target = text(&v, "target").to_string(); |
| 214 | 0× | let heads = hashes(&v, "projected_heads"); |
| 215 | 0× | if heads.is_empty() { |
| 216 | 0× | anyhow::bail!("the hub has not computed the projected state yet; try again shortly"); |
| 217 | 0× | } |
| 218 | 0× | ensure_changes(&repo, &repo_url, &heads, &change_hashes(&v))?; |
| 219 | 0× | let remote = repo.remote_for_url(&repo_url).ok_or_else(|| { |
| 220 | 0× | anyhow!("no remote is configured for {repo_url}; add one with `oxy remote add`") |
| 221 | 0× | })?; |
| 222 | | |
| 223 | 0× | repo.fetch(&remote)?; |
| 224 | 0× | repo.bookmark_set(&target, &heads)?; |
| 225 | 0× | let results = repo.push_bookmark(&remote, &target, false)?; |
| 226 | 0× | for r in &results { |
| 227 | 0× | if r.ok { |
| 228 | 0× | println!( |
| 229 | 0× | "{} {}/{} - landed proposal", |
| 230 | 0× | ui::done("Pushed"), |
| 231 | 0× | remote, |
| 232 | 0× | r.name |
| 233 | 0× | ); |
| 234 | 0× | } else { |
| 235 | 0× | println!( |
| 236 | 0× | "{} {}/{} - {}", |
| 237 | 0× | ui::warn("Rejected"), |
| 238 | 0× | remote, |
| 239 | 0× | r.name, |
| 240 | 0× | r.reason |
| 241 | 0× | ); |
| 242 | 0× | } |
| 243 | | } |
| 244 | 0× | Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 }) |
| 245 | 0× | } |