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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! interfaces for interacting with travis envs

use futures::{Future as StdFuture, IntoFuture};
use futures::future;

use super::{Client, Error, Future};
use hyper::client::Connect;
use std::borrow::Cow;

#[derive(Debug, Deserialize)]
struct EnvVarsWrapper {
    env_vars: Vec<EnvVar>,
}

#[derive(Debug, Serialize)]
pub struct EnvVarCreate {
    #[serde(rename = "env_var.name")]
    pub name: String,
    #[serde(rename = "env_var.value")]
    pub value: String,
    #[serde(rename = "env_var.public")]
    pub public: bool,
}

#[derive(Debug, Serialize)]
pub struct EnvVarPatch {
    #[serde(rename = "env_var.name", skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(rename = "env_var.value", skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    #[serde(rename = "env_var.public", skip_serializing_if = "Option::is_none")]
    pub public: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct EnvVar {
    pub id: String,
    pub name: Option<String>,
    pub public: Option<bool>,
    pub value: Option<String>,
    #[serde(rename = "@permissions")]
    pub permissions: EnvVarPermissions,
}

#[derive(Debug, Deserialize)]
pub struct EnvVarPermissions {
    pub read: bool,
    pub write: bool,
}

/// Interface for travis repositorty env vars
///
/// This is typicall accessed through the travis client
/// via `travis.env("owner/repo")`
pub struct Env<'a, C>
where
    C: Clone + Connect,
{
    pub(crate) travis: &'a Client<C>,
    pub(crate) slug: String,
}

impl<'a, C> Env<'a, C>
where
    C: Clone + Connect,
{
    /// Return a vector of EnvVars
    pub fn vars(&self) -> Future<Vec<EnvVar>> {
        Box::new(
            self.travis
                .get(
                    format!(
                        "{host}/repo/{slug}/env_vars",
                        host = self.travis.host,
                        slug = self.slug
                    ).parse()
                        .map_err(Error::from)
                        .into_future(),
                )
                .and_then(
                    |wrapper: EnvVarsWrapper| future::ok(wrapper.env_vars),
                ),
        )
    }

    /// gets an env var by id
    pub fn get<'v, V>(&self, var_id: V) -> Future<EnvVar>
    where
        V: Into<Cow<'v, str>>,
    {
        self.travis.get(
            format!(
                "{host}/repo/{slug}/env_var/{var_id}",
                host = self.travis.host,
                slug = self.slug,
                var_id = var_id.into()
            ).parse()
                .map_err(Error::from)
                .into_future(),
        )
    }

    /// updates the contents of an env var
    pub fn update<'v, V>(
        &self,
        var_id: V,
        options: EnvVarPatch,
    ) -> Future<EnvVar>
    where
        V: Into<Cow<'v, str>>,
    {
        self.travis.patch(
            format!(
                "{host}/repo/{slug}/env_var/{var_id}",
                host = self.travis.host,
                slug = self.slug,
                var_id = var_id.into()
            ).parse()
                .map_err(Error::from)
                .into_future(),
            options,
        )
    }

    /// sets a new env var for this repo
    pub fn set(&self, options: EnvVarCreate) -> Future<EnvVar> {
        self.travis.post(
            format!(
                "{host}/repo/{slug}/env_vars",
                host = self.travis.host,
                slug = self.slug
            ).parse()
                .map_err(Error::from)
                .into_future(),
            options,
        )
    }

    /// deletes env var
    pub fn delete<'v, V>(&self, var_id: V) -> Future<()>
    where
        V: Into<Cow<'v, str>>,
    {
        self.travis.delete(
            format!(
                "{host}/repo/{slug}/env_var/{var_id}",
                host = self.travis.host,
                slug = self.slug,
                var_id = var_id.into()
            ).parse()
                .map_err(Error::from)
                .into_future(),
        )
    }
}