gitlab 0.1903.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::NameOrId;
use crate::api::endpoint_prelude::*;

/// Delete a group.
///
/// Schedules the group for deletion. The group is removed at the end of the
/// instance's retention period. Immediate removal via `permanently_remove`
/// only applies to a subgroup that has already been marked for deletion.
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option), build_fn(validate = "Self::validate"))]
pub struct DeleteGroup<'a> {
    /// The group to delete.
    #[builder(setter(into))]
    group: NameOrId<'a>,

    /// The full path of the group to delete.
    #[builder(setter(into), default)]
    full_path: Option<Cow<'a, str>>,
    /// Whether to permanently remove the group.
    ///
    /// Requires setting `full_path`.
    #[builder(default)]
    permanently_remove: Option<bool>,
}

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

static PERMANENT_REMOVAL_REQUIRES_FULL_PATH: &str = "`permanently_remove` requires `full_path`";

#[non_exhaustive]
enum DeleteGroupValidationError {
    PermanentRemovalRequiresFullPath,
}

impl From<DeleteGroupValidationError> for DeleteGroupBuilderError {
    fn from(validation_error: DeleteGroupValidationError) -> Self {
        match validation_error {
            DeleteGroupValidationError::PermanentRemovalRequiresFullPath => {
                DeleteGroupBuilderError::ValidationError(
                    PERMANENT_REMOVAL_REQUIRES_FULL_PATH.into(),
                )
            },
        }
    }
}

impl DeleteGroupBuilder<'_> {
    fn validate(&self) -> Result<(), DeleteGroupValidationError> {
        if self.permanently_remove.and_then(|p| p).unwrap_or(false) && self.full_path.is_none() {
            return Err(DeleteGroupValidationError::PermanentRemovalRequiresFullPath);
        }

        Ok(())
    }
}

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

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

    fn parameters(&self) -> QueryParams<'_> {
        let mut params = QueryParams::default();

        params
            .push_opt("full_path", self.full_path.as_ref())
            .push_opt("permanently_remove", self.permanently_remove);

        params
    }
}

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

    use crate::api::groups::{DeleteGroup, DeleteGroupBuilderError};
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};

    use super::PERMANENT_REMOVAL_REQUIRES_FULL_PATH;

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

    #[test]
    fn group_is_sufficient() {
        DeleteGroup::builder().group(1).build().unwrap();
    }

    #[test]
    fn permanently_remove_true_requires_full_path() {
        let err = DeleteGroup::builder()
            .group("group/subgroup")
            .permanently_remove(true)
            .build()
            .unwrap_err();
        assert_eq!(err.to_string(), PERMANENT_REMOVAL_REQUIRES_FULL_PATH);
    }

    #[test]
    fn permanently_remove_true_with_full_path() {
        DeleteGroup::builder()
            .group("group/subgroup")
            .permanently_remove(true)
            .full_path("group/subgroup-deleted-42")
            .build()
            .unwrap();
    }

    #[test]
    fn endpoint_permanently_remove_false() {
        DeleteGroup::builder()
            .group("group/subgroup")
            .permanently_remove(false)
            .build()
            .unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::DELETE)
            .endpoint("groups/group%2Fsubgroup")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = DeleteGroup::builder()
            .group("group/subgroup")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_full_path() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::DELETE)
            .endpoint("groups/group%2Fsubgroup")
            .add_query_params(&[("full_path", "group/subgroup-deleted-42")])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = DeleteGroup::builder()
            .group("group/subgroup")
            .full_path("group/subgroup-deleted-42")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_permanently_remove() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::DELETE)
            .endpoint("groups/group%2Fsubgroup")
            .add_query_params(&[
                ("full_path", "group/subgroup-deleted-42"),
                ("permanently_remove", "true"),
            ])
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = DeleteGroup::builder()
            .group("group/subgroup")
            .full_path("group/subgroup-deleted-42")
            .permanently_remove(true)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}