use derive_builder::Builder;
use crate::api::common::NameOrId;
use crate::api::endpoint_prelude::*;
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct CherryPick<'a> {
#[builder(setter(into))]
project: NameOrId<'a>,
#[builder(setter(into))]
sha: Cow<'a, str>,
#[builder(setter(into))]
branch: Cow<'a, str>,
#[builder(default)]
dry_run: Option<bool>,
#[builder(setter(into), default)]
message: Option<Cow<'a, str>>,
}
impl<'a> CherryPick<'a> {
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();
}
}