|
| 1 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 | +// option. This file may not be copied, modified, or distributed |
| 5 | +// except according to those terms. |
| 6 | + |
| 7 | +use derive_builder::Builder; |
| 8 | + |
| 9 | +use crate::api::common::NameOrId; |
| 10 | +use crate::api::endpoint_prelude::*; |
| 11 | + |
| 12 | +/// Get the diffs between two commits. |
| 13 | +#[derive(Debug, Builder, Clone)] |
| 14 | +#[builder(setter(strip_option))] |
| 15 | +pub struct CompareCommits<'a> { |
| 16 | + /// The project to get a commit from. |
| 17 | + #[builder(setter(into))] |
| 18 | + project: NameOrId<'a>, |
| 19 | + /// The from commit sha or branch name. |
| 20 | + #[builder(setter(into))] |
| 21 | + from: Cow<'a, str>, |
| 22 | + /// The to commit sha or branch name. |
| 23 | + #[builder(setter(into))] |
| 24 | + to: Cow<'a, str>, |
| 25 | + /// The project ID to compare from. |
| 26 | + #[builder(default)] |
| 27 | + from_project_id: Option<u64>, |
| 28 | + /// Comparison method. |
| 29 | + /// |
| 30 | + /// When `true`, the commits are compared directly. When `false` (the default), commits are |
| 31 | + /// compared taking their merge base into account. |
| 32 | + #[builder(default)] |
| 33 | + straight: Option<bool>, |
| 34 | + /// Present diffs in the unified diff format. |
| 35 | + /// |
| 36 | + /// Default is false. |
| 37 | + #[builder(default)] |
| 38 | + unidiff: Option<bool>, |
| 39 | +} |
| 40 | + |
| 41 | +impl<'a> CompareCommits<'a> { |
| 42 | + /// Create a builder for the endpoint. |
| 43 | + pub fn builder() -> CompareCommitsBuilder<'a> { |
| 44 | + CompareCommitsBuilder::default() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<'a> Endpoint for CompareCommits<'a> { |
| 49 | + fn method(&self) -> Method { |
| 50 | + Method::GET |
| 51 | + } |
| 52 | + |
| 53 | + fn endpoint(&self) -> Cow<'static, str> { |
| 54 | + format!("projects/{}/repository/compare", self.project).into() |
| 55 | + } |
| 56 | + |
| 57 | + fn parameters(&self) -> QueryParams { |
| 58 | + let mut params = QueryParams::default(); |
| 59 | + params |
| 60 | + .push("from", self.from.as_ref()) |
| 61 | + .push("to", self.to.as_ref()) |
| 62 | + .push_opt("from_project_id", self.from_project_id) |
| 63 | + .push_opt("straight", self.straight) |
| 64 | + .push_opt("unidiff", self.unidiff); |
| 65 | + |
| 66 | + params |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + |
| 73 | + use crate::api::projects::repository::commits::{CompareCommits, CompareCommitsBuilderError}; |
| 74 | + use crate::api::{self, Query}; |
| 75 | + use crate::test::client::{ExpectedUrl, SingleTestClient}; |
| 76 | + use http::Method; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn project_is_necessary() { |
| 80 | + let err = CompareCommits::builder() |
| 81 | + .from("0000000000000000000000000000000000000000") |
| 82 | + .to("0000000000000000000000000000000000000000") |
| 83 | + .build() |
| 84 | + .unwrap_err(); |
| 85 | + crate::test::assert_missing_field!(err, CompareCommitsBuilderError, "project"); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn to_is_necessary() { |
| 90 | + let err = CompareCommits::builder() |
| 91 | + .project(1) |
| 92 | + .from("0000000000000000000000000000000000000000") |
| 93 | + .build() |
| 94 | + .unwrap_err(); |
| 95 | + crate::test::assert_missing_field!(err, CompareCommitsBuilderError, "to"); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn from_is_necessary() { |
| 100 | + let err = CompareCommits::builder() |
| 101 | + .project(1) |
| 102 | + .to("0000000000000000000000000000000000000000") |
| 103 | + .build() |
| 104 | + .unwrap_err(); |
| 105 | + crate::test::assert_missing_field!(err, CompareCommitsBuilderError, "from"); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn project_from_and_to_are_sufficient() { |
| 110 | + CompareCommits::builder() |
| 111 | + .project(1) |
| 112 | + .to("0000000000000000000000000000000000000000") |
| 113 | + .from("0000000000000000000000000000000000000000") |
| 114 | + .build() |
| 115 | + .unwrap(); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn endpoint() { |
| 120 | + let endpoint = ExpectedUrl::builder() |
| 121 | + .method(Method::GET) |
| 122 | + .endpoint("projects/simple%2Fproject/repository/compare") |
| 123 | + .add_query_params(&[ |
| 124 | + ("from", "0000000000000000000000000000000000000000"), |
| 125 | + ("to", "0000000000000000000000000000000000000000"), |
| 126 | + ]) |
| 127 | + .build() |
| 128 | + .unwrap(); |
| 129 | + let client = SingleTestClient::new_raw(endpoint, ""); |
| 130 | + let endpoint = CompareCommits::builder() |
| 131 | + .project("simple/project") |
| 132 | + .from("0000000000000000000000000000000000000000") |
| 133 | + .to("0000000000000000000000000000000000000000") |
| 134 | + .build() |
| 135 | + .unwrap(); |
| 136 | + api::ignore(endpoint).query(&client).unwrap(); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn endpoint_from_project_id() { |
| 141 | + let endpoint = ExpectedUrl::builder() |
| 142 | + .method(Method::GET) |
| 143 | + .endpoint("projects/simple%2Fproject/repository/compare") |
| 144 | + .add_query_params(&[ |
| 145 | + ("from", "0000000000000000000000000000000000000000"), |
| 146 | + ("to", "0000000000000000000000000000000000000000"), |
| 147 | + ("from_project_id", "234876"), |
| 148 | + ]) |
| 149 | + .build() |
| 150 | + .unwrap(); |
| 151 | + let client = SingleTestClient::new_raw(endpoint, ""); |
| 152 | + let endpoint = CompareCommits::builder() |
| 153 | + .project("simple/project") |
| 154 | + .from("0000000000000000000000000000000000000000") |
| 155 | + .to("0000000000000000000000000000000000000000") |
| 156 | + .from_project_id(234876) |
| 157 | + .build() |
| 158 | + .unwrap(); |
| 159 | + api::ignore(endpoint).query(&client).unwrap(); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn endpoint_straight() { |
| 164 | + let endpoint = ExpectedUrl::builder() |
| 165 | + .method(Method::GET) |
| 166 | + .endpoint("projects/simple%2Fproject/repository/compare") |
| 167 | + .add_query_params(&[ |
| 168 | + ("from", "0000000000000000000000000000000000000000"), |
| 169 | + ("to", "0000000000000000000000000000000000000000"), |
| 170 | + ("straight", "true"), |
| 171 | + ]) |
| 172 | + .build() |
| 173 | + .unwrap(); |
| 174 | + let client = SingleTestClient::new_raw(endpoint, ""); |
| 175 | + let endpoint = CompareCommits::builder() |
| 176 | + .project("simple/project") |
| 177 | + .from("0000000000000000000000000000000000000000") |
| 178 | + .to("0000000000000000000000000000000000000000") |
| 179 | + .straight(true) |
| 180 | + .build() |
| 181 | + .unwrap(); |
| 182 | + api::ignore(endpoint).query(&client).unwrap(); |
| 183 | + } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn endpoint_unidiff() { |
| 187 | + let endpoint = ExpectedUrl::builder() |
| 188 | + .method(Method::GET) |
| 189 | + .endpoint("projects/simple%2Fproject/repository/compare") |
| 190 | + .add_query_params(&[ |
| 191 | + ("from", "0000000000000000000000000000000000000000"), |
| 192 | + ("to", "0000000000000000000000000000000000000000"), |
| 193 | + ("unidiff", "true"), |
| 194 | + ]) |
| 195 | + .build() |
| 196 | + .unwrap(); |
| 197 | + let client = SingleTestClient::new_raw(endpoint, ""); |
| 198 | + let endpoint = CompareCommits::builder() |
| 199 | + .project("simple/project") |
| 200 | + .from("0000000000000000000000000000000000000000") |
| 201 | + .to("0000000000000000000000000000000000000000") |
| 202 | + .unidiff(true) |
| 203 | + .build() |
| 204 | + .unwrap(); |
| 205 | + api::ignore(endpoint).query(&client).unwrap(); |
| 206 | + } |
| 207 | +} |
0 commit comments