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(
26            reqwest::header::AUTHORIZATION,
27            format!("Bearer {}", token_str),
28        )
29        .json(&json!({
30            "workflow_name": params.workflow_name,
31            "repo_url": params.repo,
32            "workflow_path": params.path,
33            "git_ref": params.git_ref,
34            "inputs": inputs,
35        }))
36        .send()
37        .await?;
38
39    handle_run_response(http_client, url, token_str, res, params.tail, params.watch).await
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use reqwest_middleware::ClientBuilder;
46    use stormchaser_model::RunStatus;
47    use wiremock::matchers::{header, method, path};
48    use wiremock::{Mock, MockServer, ResponseTemplate};
49
50    #[tokio::test]
51    async fn test_runs_enqueue() {
52        let server = MockServer::start().await;
53        Mock::given(method("POST"))
54            .and(path("/api/v1/runs"))
55            .and(header(reqwest::header::AUTHORIZATION, "Bearer test-token"))
56            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
57                "id": "12345678-1234-1234-1234-123456789012",
58                "status": RunStatus::Queued
59            })))
60            .mount(&server)
61            .await;
62
63        let client = ClientBuilder::new(reqwest::Client::new()).build();
64        let params = EnqueueRunParams {
65            workflow_name: "test".to_string(),
66            repo: "http://git".to_string(),
67            path: "workflow.yaml".to_string(),
68            git_ref: "main".to_string(),
69            input: vec![],
70            tail: false,
71            watch: false,
72        };
73
74        let result = enqueue_run(&server.uri(), Some("test-token"), &client, params).await;
75        assert!(result.is_ok());
76    }
77}