1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::Client;
use serde_json;
use std::collections::HashMap;

#[derive(Clone, Debug, Deserialize)]
pub struct Job {
    pub id: String,
    pub name: String,
    pub group: Option<String>,
    pub project: String,
    pub href: String,
    pub permalink: String,
    pub description: String,
    pub schedule_enabled: Option<bool>,
    pub enabled: Option<bool>,
    pub scheduled: Option<bool>,
}
impl Job {
    pub fn name_with_group(&self) -> String {
        match self.group {
            Some(ref g) => format!("{}/{}", g, self.name),
            None => self.name.clone()
        }
    }
}

fn compile_filters(filters: Vec<&str>) -> Vec<String> {
    filters
        .iter()
        .map(|x|{
            let mut z = x.to_string();

            if z.starts_with("name") {
                z = format!("jobFilter={}", z.split("=").collect::<Vec<&str>>()[1]);
            } else if z.starts_with("group") {
                z = format!("groupPath={}", z.split("=").collect::<Vec<&str>>()[1]);
            }

            z
        })
        .collect::<Vec<String>>()
}

#[derive(Debug, Clone, Serialize)]
pub struct RunBody {
    pub filter: Option<String>,
    pub options: HashMap<String, String>,
    #[serde(rename="argString")]
    pub arg_string: Option<String>
}

#[derive(Clone)]
pub struct JobService<'a> {
    client: &'a Client<'a>
}

impl<'a> JobService<'a> {
    pub fn from_client(client: &'a Client) -> Result<Self, ()>
    {
        Ok(Self {
            client
        })
    }

    pub fn list(&self, project: &str, filters: Vec<&str>) -> Vec<Job> {
        let mut filters = compile_filters(filters);

        let ret = self.client.perform_get(&format!("project/{}/jobs",project), &mut filters);

        serde_json::from_str(&ret).unwrap()
    }

    pub fn run(&self, job: &str, body: &RunBody) {
        let mut body = body.clone();
        if self.client.api_version <= 18 {
            let mut arg_string: Vec<String> = Vec::new();
            for (name, value) in &body.options {
                arg_string.push(format!("-{} {}",name, value));
            }

            body.arg_string = Some(arg_string.join(" "));
        }
        let body = serde_json::to_string(&body).unwrap();
        self.client.perform_post(&format!("job/{}/run", job), &body);
    }
}