| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use std::time::Duration; |
| 14 |
|
| 15 |
use ops_status::{Action, Method}; |
| 16 |
use tokio::sync::mpsc; |
| 17 |
|
| 18 |
use crate::config::Source; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#[derive(Debug, Clone)] |
| 23 |
pub(crate) struct Outcome { |
| 24 |
pub key: String, |
| 25 |
|
| 26 |
pub result: Result<u16, String>, |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
const ACTION_TIMEOUT: Duration = Duration::from_mins(2); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
const BODY_KEEP: usize = 120; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
pub(crate) fn fire(source: &Source, action: Action, key: String, tx: mpsc::Sender<Outcome>) { |
| 49 |
let url = source.action_url(&action.url); |
| 50 |
let token = source.token(); |
| 51 |
tokio::spawn(async move { |
| 52 |
let result = issue(url, action, token).await; |
| 53 |
let _ = tx.send(Outcome { key, result }).await; |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
async fn issue(url: String, action: Action, token: Option<String>) -> Result<u16, String> { |
| 58 |
let client = reqwest::Client::builder() |
| 59 |
.timeout(ACTION_TIMEOUT) |
| 60 |
.build() |
| 61 |
.map_err(|e| format!("client: {e}"))?; |
| 62 |
|
| 63 |
let mut request = client.request(method(action.method), &url); |
| 64 |
if let Some(token) = token { |
| 65 |
request = request.bearer_auth(token); |
| 66 |
} |
| 67 |
if let Some(body) = &action.body { |
| 68 |
request = request.json(body); |
| 69 |
} |
| 70 |
|
| 71 |
let response = request.send().await.map_err(short_error)?; |
| 72 |
let status = response.status(); |
| 73 |
if status.is_success() { |
| 74 |
return Ok(status.as_u16()); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
let code = status.as_u16(); |
| 80 |
if code == 401 || code == 403 { |
| 81 |
return Err("unauthorized (check the source's token_env)".into()); |
| 82 |
} |
| 83 |
let body = response.text().await.unwrap_or_default(); |
| 84 |
let trimmed = body.trim(); |
| 85 |
if trimmed.is_empty() { |
| 86 |
Err(format!("HTTP {code}")) |
| 87 |
} else { |
| 88 |
Err(format!("HTTP {code}: {}", clip(trimmed))) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
fn method(method: Method) -> reqwest::Method { |
| 93 |
match method { |
| 94 |
Method::Get => reqwest::Method::GET, |
| 95 |
Method::Post => reqwest::Method::POST, |
| 96 |
Method::Put => reqwest::Method::PUT, |
| 97 |
Method::Delete => reqwest::Method::DELETE, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
fn clip(body: &str) -> String { |
| 103 |
let flat = body.replace('\n', " "); |
| 104 |
if flat.chars().count() <= BODY_KEEP { |
| 105 |
return flat; |
| 106 |
} |
| 107 |
let kept: String = flat.chars().take(BODY_KEEP.saturating_sub(1)).collect(); |
| 108 |
format!("{kept}…") |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
#[allow(clippy::needless_pass_by_value)] |
| 115 |
fn short_error(e: reqwest::Error) -> String { |
| 116 |
if e.is_timeout() { |
| 117 |
return "timed out".into(); |
| 118 |
} |
| 119 |
if e.is_connect() { |
| 120 |
return "connection refused".into(); |
| 121 |
} |
| 122 |
let text = e.to_string(); |
| 123 |
text.split(':') |
| 124 |
.next_back() |
| 125 |
.unwrap_or(&text) |
| 126 |
.trim() |
| 127 |
.to_string() |
| 128 |
} |
| 129 |
|
| 130 |
#[cfg(test)] |
| 131 |
mod tests { |
| 132 |
use super::*; |
| 133 |
|
| 134 |
fn source(url: &str) -> Source { |
| 135 |
toml::from_str(&format!("name = \"sando\"\nurl = \"{url}\"\n")).unwrap() |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn methods_map_across_to_reqwest() { |
| 140 |
assert_eq!(method(Method::Get), reqwest::Method::GET); |
| 141 |
assert_eq!(method(Method::Post), reqwest::Method::POST); |
| 142 |
assert_eq!(method(Method::Put), reqwest::Method::PUT); |
| 143 |
assert_eq!(method(Method::Delete), reqwest::Method::DELETE); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn a_failure_body_is_clipped_to_one_line() { |
| 148 |
assert_eq!(clip("gate\nnot\nsatisfied"), "gate not satisfied"); |
| 149 |
let long = clip(&"x".repeat(400)); |
| 150 |
assert!(long.ends_with('…')); |
| 151 |
assert_eq!(long.chars().count(), BODY_KEEP); |
| 152 |
} |
| 153 |
|
| 154 |
#[tokio::test] |
| 155 |
async fn an_unreachable_daemon_reports_a_short_reason_not_a_url_chain() { |
| 156 |
|
| 157 |
let err = issue( |
| 158 |
"http://127.0.0.1:1/rollback/b".into(), |
| 159 |
Action { |
| 160 |
label: "Roll back".into(), |
| 161 |
method: Method::Post, |
| 162 |
url: "/rollback/b".into(), |
| 163 |
confirm: true, |
| 164 |
danger: true, |
| 165 |
body: None, |
| 166 |
}, |
| 167 |
None, |
| 168 |
) |
| 169 |
.await |
| 170 |
.unwrap_err(); |
| 171 |
assert!(err.len() < 60, "{err:?}"); |
| 172 |
assert!(!err.contains("http://"), "{err:?}"); |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn a_body_carrying_action_serializes_it() { |
| 177 |
|
| 178 |
|
| 179 |
let action = Action { |
| 180 |
label: "Publish".into(), |
| 181 |
method: Method::Post, |
| 182 |
url: "/publish".into(), |
| 183 |
confirm: true, |
| 184 |
danger: false, |
| 185 |
body: Some(serde_json::json!({"app": "goingson", "target": "macos"})), |
| 186 |
}; |
| 187 |
assert_eq!( |
| 188 |
source("http://x").action_url(&action.url), |
| 189 |
"http://x/publish" |
| 190 |
); |
| 191 |
assert!(action.body.is_some()); |
| 192 |
} |
| 193 |
} |
| 194 |
|