Skip to main content

stormchaser_cli/commands/runs/
enqueue.rs

1use crate::utils::{handle_run_response, parse_key_val_list, require_token};
2use anyhow::Result;
3use serde_json::json;
4
5pub struct EnqueueRunParams {
6    pub workflow_name: String,
7    pub repo: String,
8    pub path: String,
9    pub git_ref: String,
10    pub input: Vec<String>,
11    pub tail: bool,
12    pub watch: bool,
13}
14
15pub async fn enqueue_run(
16    url: &str,
17    token: Option<&str>,
18    http_client: &reqwest_middleware::ClientWithMiddleware,
19    params: EnqueueRunParams,
20) -> Result<()> {
21    let inputs = parse_key_val_list(params.input);
22    let token_str = require_token(token)?;
23    let res = http_client
24        .post(format!("{}/api/v1/runs", url))
25        .header("Authorization", format!("Bearer {}", token_str))
26        .json(&json!({
27            "workflow_name": params.workflow_name,
28            "repo_url": params.repo,
29            "workflow_path": params.path,
30            "git_ref": params.git_ref,
31            "inputs": inputs,
32        }))
33        .send()
34        .await?;
35
36    handle_run_response(http_client, url, token_str, res, params.tail, params.watch).await
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use reqwest_middleware::ClientBuilder;
43    use wiremock::matchers::{header, method, path};
44    use wiremock::{Mock, MockServer, ResponseTemplate};
45
46    #[tokio::test]
47    async fn test_runs_enqueue() {
48        let server = MockServer::start().await;
49        Mock::given(method("POST"))
50            .and(path("/api/v1/runs"))
51            .and(header("Authorization", "Bearer test-token"))
52            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
53                "id": "12345678-1234-1234-1234-123456789012",
54                "status": "queued"
55            })))
56            .mount(&server)
57            .await;
58
59        let client = ClientBuilder::new(reqwest::Client::new()).build();
60        let params = EnqueueRunParams {
61            workflow_name: "test".to_string(),
62            repo: "http://git".to_string(),
63            path: "workflow.yaml".to_string(),
64            git_ref: "main".to_string(),
65            input: vec![],
66            tail: false,
67            watch: false,
68        };
69
70        let result = enqueue_run(&server.uri(), Some("test-token"), &client, params).await;
71        assert!(result.is_ok());
72    }
73}