Commit 567f5bd7 authored by Bradley Wood's avatar Bradley Wood
Browse files

add MergeMergeRequest

parent 0fbc3a64
......@@ -11,6 +11,8 @@
labels from a project.
* Added the `api::projects::labels::PromoteLabel` endpoint to promote a project
label to a group label.
* Added the `api::projects:merge_requests::MergeMergeRequest` endpoint to
merge open merge requests
# v0.1301.1
......
......@@ -58,6 +58,7 @@ These API endpoints have been implemented.
* `POST /projects/:project/merge_requests` `projects/merge_requests/create.rs`
* `GET /projects/:project/merge_requests/:merge_request` `projects/merge_requests/merge_request.rs`
* `PUT /projects/:project/merge_requests/:merge_request` `projects/merge_requests/edit.rs`
* `PUT /projects/:project/merge_requests/:merge_request/merge` `projects/merge_requests/merge.rs`
* `GET /projects/:project/merge_requests/:merge_request/award_emoji` `projects/merge_requests/awards/awards.rs`
* `GET /projects/:project/merge_requests/:merge_request/closes_issues` `projects/merge_requests/issues_closed_by.rs`
* `GET /projects/:project/merge_requests/:merge_request/discussions` `projects/merge_requests/discussions/discussions.rs`
......@@ -223,7 +224,6 @@ instead of having to search the page for missing endpoints.
* `POST /projects/:project/merge_requests/:merge_request/cancel_merge_when_pipeline_succeeds` https://gitlab.kitware.com/help/api/merge_requests.md#cancel-merge-when-pipeline-succeeds
* `GET /projects/:project/merge_requests/:merge_request/changes` https://gitlab.kitware.com/help/api/merge_requests.md#get-single-mr-changes
* `GET /projects/:project/merge_requests/:merge_request/commits` https://gitlab.kitware.com/help/api/merge_requests.md#get-single-mr-commits
* `POST /projects/:project/merge_requests/:merge_request/merge` https://gitlab.kitware.com/help/api/merge_requests.md#accept-mr
* `GET /projects/:project/merge_requests/:merge_request/merge_ref` https://gitlab.kitware.com/help/api/merge_requests.md#merge-to-default-merge-ref-path
* `GET /projects/:project/merge_requests/:merge_request/participants` https://gitlab.kitware.com/help/api/merge_requests.md#get-single-mr-participants
* `GET /projects/:project/merge_requests/:merge_request/pipelines` https://gitlab.kitware.com/help/api/merge_requests.md#list-mr-pipelines
......
......@@ -13,6 +13,7 @@ mod create;
pub mod discussions;
mod edit;
mod issues_closed_by;
mod merge;
mod merge_request;
mod merge_requests;
pub mod notes;
......@@ -31,6 +32,9 @@ pub use self::issues_closed_by::IssuesClosedByBuilder;
pub use self::merge_request::MergeRequest;
pub use self::merge_request::MergeRequestBuilder;
pub use self::merge::MergeMergeRequest;
pub use self::merge::MergeMergeRequestBuilder;
pub use self::merge_requests::MergeRequestOrderBy;
pub use self::merge_requests::MergeRequestScope;
pub use self::merge_requests::MergeRequestState;
......
// 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::*;
/// Merge a merge request.
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct MergeMergeRequest<'a> {
/// The project with the merge request.
#[builder(setter(into))]
project: NameOrId<'a>,
/// The ID of the merge request.
merge_request: u64,
/// Commit message to use on the merge commit.
#[builder(setter(into), default)]
merge_commit_message: Option<Cow<'a, str>>,
/// Commit message to use on the squash commit.
#[builder(setter(into), default)]
squash_commit_message: Option<Cow<'a, str>>,
/// Squash source branch commits into a single merge commit?
#[builder(default)]
squash: Option<bool>,
/// Remove source branch on successful merge?
#[builder(default)]
should_remove_source_branch: Option<bool>,
/// Merge when pipeline succeeds?
#[builder(default)]
merge_when_pipeline_succeeds: Option<bool>,
/// Git commit SHA to merge. If set, this must match the head of the branch being merged or the
/// merge will fail.
#[builder(setter(into), default)]
sha: Option<Cow<'a, str>>,
}
impl<'a> MergeMergeRequest<'a> {
/// Create a builder for the endpoint.
pub fn builder() -> MergeMergeRequestBuilder<'a> {
MergeMergeRequestBuilder::default()
}
}
impl<'a> Endpoint for MergeMergeRequest<'a> {
fn method(&self) -> Method {
Method::PUT
}
fn endpoint(&self) -> Cow<'static, str> {
format!(
"projects/{}/merge_requests/{}/merge",
self.project, self.merge_request,
)
.into()
}
fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
let mut params = FormParams::default();
params
.push_opt("merge_commit_message", self.merge_commit_message.as_ref())
.push_opt("squash_commit_message", self.squash_commit_message.as_ref())
.push_opt("squash", self.squash)
.push_opt(
"should_remove_source_branch",
self.should_remove_source_branch,
)
.push_opt(
"merge_when_pipeline_succeeds",
self.merge_when_pipeline_succeeds,
)
.push_opt("sha", self.sha.as_ref());
params.into_body()
}
}
#[cfg(test)]
mod tests {
use http::Method;
use crate::api::projects::merge_requests::MergeMergeRequest;
use crate::api::{self, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
#[test]
fn project_and_merge_request_are_needed() {
let err = MergeMergeRequest::builder().build().unwrap_err();
assert_eq!(err, "`project` must be initialized");
}
#[test]
fn project_is_needed() {
let err = MergeMergeRequest::builder()
.merge_request(1)
.build()
.unwrap_err();
assert_eq!(err, "`project` must be initialized");
}
#[test]
fn merge_request_is_needed() {
let err = MergeMergeRequest::builder().project(1).build().unwrap_err();
assert_eq!(err, "`merge_request` must be initialized");
}
#[test]
fn project_and_merge_request_are_sufficient() {
MergeMergeRequest::builder()
.project(1)
.merge_request(1)
.build()
.unwrap();
}
#[test]
fn endpoint() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("projects/simple%2Fproject/merge_requests/1/merge")
.content_type("application/x-www-form-urlencoded")
.body_str("")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = MergeMergeRequest::builder()
.project("simple/project")
.merge_request(1)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_merge_commit_message() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("projects/simple%2Fproject/merge_requests/1/merge")
.content_type("application/x-www-form-urlencoded")
.body_str("merge_commit_message=blahblahblah")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = MergeMergeRequest::builder()
.project("simple/project")
.merge_request(1)
.merge_commit_message("blahblahblah")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_squash_commit_message() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("projects/simple%2Fproject/merge_requests/1/merge")
.content_type("application/x-www-form-urlencoded")
.body_str("squash_commit_message=blahblahblah")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = MergeMergeRequest::builder()
.project("simple/project")
.merge_request(1)
.squash_commit_message("blahblahblah")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_merge_when_pipeline_succeeds() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("projects/simple%2Fproject/merge_requests/1/merge")
.content_type("application/x-www-form-urlencoded")
.body_str("merge_when_pipeline_succeeds=true")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = MergeMergeRequest::builder()
.project("simple/project")
.merge_request(1)
.merge_when_pipeline_succeeds(true)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_should_remove_source_branch() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("projects/simple%2Fproject/merge_requests/1/merge")
.content_type("application/x-www-form-urlencoded")
.body_str("should_remove_source_branch=true")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = MergeMergeRequest::builder()
.project("simple/project")
.merge_request(1)
.should_remove_source_branch(true)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_squash() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("projects/simple%2Fproject/merge_requests/1/merge")
.content_type("application/x-www-form-urlencoded")
.body_str("squash=true")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = MergeMergeRequest::builder()
.project("simple/project")
.merge_request(1)
.squash(true)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_sha() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("projects/simple%2Fproject/merge_requests/1/merge")
.content_type("application/x-www-form-urlencoded")
.body_str("sha=blahblahblah")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = MergeMergeRequest::builder()
.project("simple/project")
.merge_request(1)
.sha("blahblahblah")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment