gitlab 0.1810.0

Gitlab API client.
Documentation
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use derive_builder::Builder;

use crate::api::common::{self, NameOrId};
use crate::api::endpoint_prelude::*;

/// Filter parameters.
#[derive(Debug, Clone, Builder)]
#[builder(setter(strip_option))]
pub struct GroupVariableFilter<'a> {
    /// Filter based on the environment scope.
    #[builder(setter(into), default)]
    environment_scope: Option<Cow<'a, str>>,
}

impl<'a> GroupVariableFilter<'a> {
    /// Create a builder for the endpoint.
    pub fn builder() -> GroupVariableFilterBuilder<'a> {
        GroupVariableFilterBuilder::default()
    }

    pub(crate) fn add_query<'b>(&'b self, params: &mut FormParams<'b>) {
        params.push_opt("filter[environment_scope]", self.environment_scope.as_ref());
    }
}

/// Get the variable from a group.
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct GroupVariable<'a> {
    /// The group to get the variable from.
    #[builder(setter(into))]
    group: NameOrId<'a>,
    /// The name of the variable.
    #[builder(setter(into))]
    key: Cow<'a, str>,
    /// Filter
    #[builder(default)]
    filter: Option<GroupVariableFilter<'a>>,
}

impl<'a> GroupVariable<'a> {
    /// Create a builder for the endpoint.
    pub fn builder() -> GroupVariableBuilder<'a> {
        GroupVariableBuilder::default()
    }
}

impl Endpoint for GroupVariable<'_> {
    fn method(&self) -> Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        format!(
            "groups/{}/variables/{}",
            self.group,
            common::path_escaped(self.key.as_ref()),
        )
        .into()
    }

    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
        let mut params = FormParams::default();

        if let Some(filter) = self.filter.as_ref() {
            filter.add_query(&mut params);
        }

        params.into_body()
    }
}

#[cfg(test)]
mod tests {
    use http::Method;

    use crate::api::groups::variables::variable::{
        GroupVariable, GroupVariableBuilderError, GroupVariableFilter,
    };
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};

    #[test]
    fn all_parameters_are_needed() {
        let err = GroupVariable::builder().build().unwrap_err();
        crate::test::assert_missing_field!(err, GroupVariableBuilderError, "group");
    }

    #[test]
    fn group_is_necessary() {
        let err = GroupVariable::builder().key("testkey").build().unwrap_err();
        crate::test::assert_missing_field!(err, GroupVariableBuilderError, "group");
    }

    #[test]
    fn key_is_necessary() {
        let err = GroupVariable::builder().group(1).build().unwrap_err();
        crate::test::assert_missing_field!(err, GroupVariableBuilderError, "key");
    }

    #[test]
    fn sufficient_parameters() {
        GroupVariable::builder()
            .group(1)
            .key("testkey")
            .build()
            .unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::GET)
            .endpoint("groups/simple%2Fgroup/variables/testkey%2F")
            .content_type("application/x-www-form-urlencoded")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = GroupVariable::builder()
            .group("simple/group")
            .key("testkey/")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_filter() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::GET)
            .endpoint("groups/simple%2Fgroup/variables/testkey%2F")
            .content_type("application/x-www-form-urlencoded")
            .body_str("filter%5Benvironment_scope%5D=production")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = GroupVariable::builder()
            .group("simple/group")
            .key("testkey/")
            .filter(
                GroupVariableFilter::builder()
                    .environment_scope("production")
                    .build()
                    .unwrap(),
            )
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}