gitlab 0.1900.1

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 serde_json::json;

use crate::api::common::NameOrId;
use crate::api::endpoint_prelude::*;

/// Import a project from an exported archive at a remote URL.
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct RemoteImport<'a> {
    /// The path for the new project, including namespace (`namespace/project-name`).
    #[builder(setter(into))]
    path: Cow<'a, str>,

    /// URL for the remote archive to import from.
    #[builder(setter(into))]
    url: Cow<'a, str>,

    /// Name of the project to import.
    ///
    /// Defaults to the path of the project if not provided.
    #[builder(setter(into), default)]
    name: Option<Cow<'a, str>>,

    /// Namespace to import the project into.
    ///
    /// Defaults to the current user's namespace.
    #[builder(setter(into), default)]
    namespace: Option<NameOrId<'a>>,

    /// Whether to overwrite the project.
    #[builder(default)]
    overwrite: Option<bool>,
    // TODO: add `overwrite_params` support, a sub-object of per-entity overwrite flags. (#129)
}

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

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

    fn endpoint(&self) -> Cow<'static, str> {
        "projects/remote-import".into()
    }

    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
        let (namespace_id, namespace_path) = match &self.namespace {
            Some(NameOrId::Id(id)) => (Some(*id), None),
            Some(NameOrId::Name(path)) => (None, Some(path.as_ref())),
            None => (None, None),
        };
        JsonParams::into_body(&JsonParams::clean(json!({
            "name": self.name,
            "namespace_id": namespace_id,
            "namespace_path": namespace_path,
            "overwrite": self.overwrite,
            "path": self.path,
            "url": self.url,
        })))
    }
}

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

    use crate::api::projects::import::{RemoteImport, RemoteImportBuilderError};
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};

    #[test]
    fn path_is_necessary() {
        let err = RemoteImport::builder()
            .url("https://example.com/export.tar.gz")
            .build()
            .unwrap_err();
        crate::test::assert_missing_field!(err, RemoteImportBuilderError, "path");
    }

    #[test]
    fn url_is_necessary() {
        let err = RemoteImport::builder()
            .path("my-group/my-project")
            .build()
            .unwrap_err();
        crate::test::assert_missing_field!(err, RemoteImportBuilderError, "url");
    }

    #[test]
    fn path_and_url_are_sufficient() {
        RemoteImport::builder()
            .path("my-group/my-project")
            .url("https://example.com/export.tar.gz")
            .build()
            .unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .endpoint("projects/remote-import")
            .content_type("application/json")
            .body_str(concat!(
                "{",
                "\"path\":\"my-group/my-project\",",
                "\"url\":\"https://example.com/export.tar.gz\"",
                "}",
            ))
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = RemoteImport::builder()
            .path("my-group/my-project")
            .url("https://example.com/export.tar.gz")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_name() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .endpoint("projects/remote-import")
            .content_type("application/json")
            .body_str(concat!(
                "{",
                "\"name\":\"My Project\",",
                "\"path\":\"my-group/my-project\",",
                "\"url\":\"https://example.com/export.tar.gz\"",
                "}",
            ))
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = RemoteImport::builder()
            .path("my-group/my-project")
            .url("https://example.com/export.tar.gz")
            .name("My Project")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_namespace_id() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .endpoint("projects/remote-import")
            .content_type("application/json")
            .body_str(concat!(
                "{",
                "\"namespace_id\":5,",
                "\"path\":\"my-group/my-project\",",
                "\"url\":\"https://example.com/export.tar.gz\"",
                "}",
            ))
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = RemoteImport::builder()
            .path("my-group/my-project")
            .url("https://example.com/export.tar.gz")
            .namespace(5)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_namespace_path() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .endpoint("projects/remote-import")
            .content_type("application/json")
            .body_str(concat!(
                "{",
                "\"namespace_path\":\"my-group\",",
                "\"path\":\"my-group/my-project\",",
                "\"url\":\"https://example.com/export.tar.gz\"",
                "}",
            ))
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = RemoteImport::builder()
            .path("my-group/my-project")
            .url("https://example.com/export.tar.gz")
            .namespace("my-group")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_overwrite() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .endpoint("projects/remote-import")
            .content_type("application/json")
            .body_str(concat!(
                "{",
                "\"overwrite\":true,",
                "\"path\":\"my-group/my-project\",",
                "\"url\":\"https://example.com/export.tar.gz\"",
                "}",
            ))
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = RemoteImport::builder()
            .path("my-group/my-project")
            .url("https://example.com/export.tar.gz")
            .overwrite(true)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}