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

/// Cherry-picks a commit to a given branch.
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct CherryPick<'a> {
    /// The ID or URL-encoded path of the project owned by the authenticated user.
    #[builder(setter(into))]
    project: NameOrId<'a>,
    /// The commit hash.
    #[builder(setter(into))]
    sha: Cow<'a, str>,
    /// The name of the branch
    #[builder(setter(into))]
    branch: Cow<'a, str>,
    /// Does not commit any changes. Default is false.
    #[builder(default)]
    dry_run: Option<bool>,
    /// A custom commit message to use for the new commit.
    #[builder(setter(into), default)]
    message: Option<Cow<'a, str>>,
}

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

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

    fn endpoint(&self) -> Cow<'static, str> {
        format!(
            "projects/{}/repository/commits/{}/cherry_pick",
            self.project, self.sha,
        )
        .into()
    }

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

        params
            .push("branch", self.branch.as_ref())
            .push_opt("message", self.message.as_ref())
            .push_opt("dry_run", self.dry_run);

        params.into_body()
    }
}

#[cfg(test)]
mod tests {
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};

    use super::*;

    #[test]
    fn project_sha_and_branch_are_necessary() {
        let err = CherryPick::builder().build().unwrap_err();
        crate::test::assert_missing_field!(err, CherryPickBuilderError, "project");
    }

    #[test]
    fn project_is_necessary() {
        let err = CherryPick::builder()
            .sha("123")
            .branch("main")
            .build()
            .unwrap_err();
        crate::test::assert_missing_field!(err, CherryPickBuilderError, "project");
    }

    #[test]
    fn sha_is_necessary() {
        let err = CherryPick::builder()
            .project(1)
            .branch("main")
            .build()
            .unwrap_err();
        crate::test::assert_missing_field!(err, CherryPickBuilderError, "sha");
    }

    #[test]
    fn branch_is_necessary() {
        let err = CherryPick::builder()
            .project(1)
            .sha("123")
            .build()
            .unwrap_err();
        crate::test::assert_missing_field!(err, CherryPickBuilderError, "branch");
    }

    #[test]
    fn project_sha_and_branch_are_sufficient() {
        CherryPick::builder()
            .project(1)
            .sha("123")
            .branch("main")
            .build()
            .unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .content_type("application/x-www-form-urlencoded")
            .endpoint("projects/simple%2Fproject/repository/commits/123/cherry_pick")
            .body_str("branch=main")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = CherryPick::builder()
            .project("simple/project")
            .sha("123")
            .branch("main")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_dry_run() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .content_type("application/x-www-form-urlencoded")
            .endpoint("projects/simple%2Fproject/repository/commits/123/cherry_pick")
            .body_str(concat!("branch=main", "&dry_run=true"))
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = CherryPick::builder()
            .project("simple/project")
            .sha("123")
            .branch("main")
            .dry_run(true)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_message() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::POST)
            .content_type("application/x-www-form-urlencoded")
            .endpoint("projects/simple%2Fproject/repository/commits/123/cherry_pick")
            .body_str(concat!(
                "branch=main",
                "&message=This+is+an+example+message.",
            ))
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = CherryPick::builder()
            .project("simple/project")
            .sha("123")
            .branch("main")
            .message("This is an example message.")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}