1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crates::git_checks_core::impl_prelude::*;
use crates::thiserror::Error;
#[derive(Debug, Error)]
enum ReleaseBranchError {
#[error(
"failed to get the merge-base for {} against a release branch {}: {}",
commit,
base,
output
)]
MergeBase {
commit: CommitId,
base: CommitId,
output: String,
},
}
impl ReleaseBranchError {
fn merge_base(commit: CommitId, base: CommitId, output: &[u8]) -> Self {
ReleaseBranchError::MergeBase {
commit,
base,
output: String::from_utf8_lossy(output).into(),
}
}
}
#[derive(Builder, Debug, Clone)]
#[builder(field(private))]
pub struct ReleaseBranch {
#[builder(setter(into))]
branch: String,
#[builder(setter(into))]
disallowed_commit: CommitId,
#[builder(default = "false")]
required: bool,
}
impl ReleaseBranch {
pub fn builder() -> ReleaseBranchBuilder {
ReleaseBranchBuilder::default()
}
}
impl BranchCheck for ReleaseBranch {
fn name(&self) -> &str {
"release-branch"
}
fn check(
&self,
ctx: &CheckGitContext,
commit: &CommitId,
) -> Result<CheckResult, Box<dyn Error>> {
let merge_base = ctx
.git()
.arg("merge-base")
.arg("--all")
.arg(commit.as_str())
.arg(self.disallowed_commit.as_str())
.output()
.map_err(|err| GitError::subcommand("merge-base", err))?;
if !merge_base.status.success() {
return Err(ReleaseBranchError::merge_base(
commit.clone(),
self.disallowed_commit.clone(),
&merge_base.stderr,
)
.into());
}
let merge_bases = String::from_utf8_lossy(&merge_base.stdout);
let is_eligible = merge_bases
.lines()
.all(|merge_base| merge_base != self.disallowed_commit.as_str());
let mut result = CheckResult::new();
if is_eligible && !self.required {
result.add_warning(format!("Eligible for the {} branch.", self.branch));
} else if !is_eligible && self.required {
result.add_error(format!(
"This branch is ineligible for the {} branch; it needs to \
be based on a commit before {}.",
self.branch, self.disallowed_commit,
));
}
Ok(result)
}
}
#[cfg(feature = "config")]
pub(crate) mod config {
use crates::git_checks_config::{BranchCheckConfig, IntoCheck};
use crates::git_workarea::CommitId;
use crates::inventory;
#[cfg(test)]
use crates::serde_json;
#[cfg(test)]
use test;
use ReleaseBranch;
#[derive(Deserialize, Debug)]
pub struct ReleaseBranchConfig {
branch: String,
disallowed_commit: String,
#[serde(default)]
required: Option<bool>,
}
impl IntoCheck for ReleaseBranchConfig {
type Check = ReleaseBranch;
fn into_check(self) -> Self::Check {
let mut builder = ReleaseBranch::builder();
builder
.branch(self.branch)
.disallowed_commit(CommitId::new(self.disallowed_commit));
if let Some(required) = self.required {
builder.required(required);
}
builder
.build()
.expect("configuration mismatch for `ReleaseBranch`")
}
}
register_checks! {
ReleaseBranchConfig {
"release_branch" => BranchCheckConfig,
},
}
#[test]
fn test_release_branch_config_empty() {
let json = json!({});
let err = serde_json::from_value::<ReleaseBranchConfig>(json).unwrap_err();
test::check_missing_json_field(err, "branch");
}
#[test]
fn test_release_branch_config_branch_is_required() {
let exp_disallowed_commit = "post-branch commit hash";
let json = json!({
"disallowed_commit": exp_disallowed_commit,
});
let err = serde_json::from_value::<ReleaseBranchConfig>(json).unwrap_err();
test::check_missing_json_field(err, "branch");
}
#[test]
fn test_release_branch_config_disallowed_commit_is_required() {
let exp_branch = "v1.x";
let json = json!({
"branch": exp_branch,
});
let err = serde_json::from_value::<ReleaseBranchConfig>(json).unwrap_err();
test::check_missing_json_field(err, "disallowed_commit");
}
#[test]
fn test_release_branch_config_minimum_fields() {
let exp_branch = "v1.x";
let exp_disallowed_commit = "post-branch commit hash";
let json = json!({
"branch": exp_branch,
"disallowed_commit": exp_disallowed_commit,
});
let check: ReleaseBranchConfig = serde_json::from_value(json).unwrap();
assert_eq!(check.branch, exp_branch);
assert_eq!(check.disallowed_commit, exp_disallowed_commit);
assert_eq!(check.required, None);
}
#[test]
fn test_release_branch_config_all_fields() {
let exp_branch = "v1.x";
let exp_disallowed_commit = "post-branch commit hash";
let json = json!({
"branch": exp_branch,
"disallowed_commit": exp_disallowed_commit,
"required": true,
});
let check: ReleaseBranchConfig = serde_json::from_value(json).unwrap();
assert_eq!(check.branch, exp_branch);
assert_eq!(check.disallowed_commit, exp_disallowed_commit);
assert_eq!(check.required, Some(true));
}
}
#[cfg(test)]
mod tests {
use crates::git_workarea::CommitId;
use builders::ReleaseBranchBuilder;
use test::*;
use ReleaseBranch;
const RELEASE_BRANCH: &str = "3a22ca19fda09183da2faab60819ff6807568acd";
const POST_RELEASE_COMMIT: &str = "d02f015907371738253a22b9a7fec78607a969b2";
const POST_RELEASE_BRANCH: &str = "a61fd3759b61a4a1f740f3fe656bc42151cefbdd";
const POST_RELEASE_BRANCH_MERGE: &str = "c58dd19d9976722d82aa6bc6a52a2a01a52bd9e8";
#[test]
fn test_release_branch_builder_default() {
assert!(ReleaseBranch::builder().build().is_err());
}
#[test]
fn test_release_branch_builder_branch_is_required() {
assert!(ReleaseBranch::builder()
.disallowed_commit(CommitId::new(POST_RELEASE_COMMIT))
.build()
.is_err());
}
#[test]
fn test_release_branch_builder_commit_is_required() {
assert!(ReleaseBranch::builder().branch("release").build().is_err());
}
#[test]
fn test_release_branch_builder_minimum_fields() {
assert!(ReleaseBranch::builder()
.branch("release")
.disallowed_commit(CommitId::new(POST_RELEASE_COMMIT))
.build()
.is_ok());
}
fn make_release_branch_check() -> ReleaseBranchBuilder {
let mut builder = ReleaseBranch::builder();
builder
.branch("release")
.disallowed_commit(CommitId::new(POST_RELEASE_COMMIT));
builder
}
#[test]
fn test_release_branch_ok() {
let check = make_release_branch_check().build().unwrap();
let result = run_branch_check("test_release_branch_ok", RELEASE_BRANCH, check);
test_result_warnings(result, &["Eligible for the release branch."]);
}
#[test]
fn test_release_branch_ok_required() {
let check = make_release_branch_check().required(true).build().unwrap();
run_branch_check_ok("test_release_branch_ok_required", RELEASE_BRANCH, check);
}
#[test]
fn test_post_release_branch() {
let check = make_release_branch_check().build().unwrap();
run_branch_check_ok("test_post_release_branch", POST_RELEASE_BRANCH, check);
}
#[test]
fn test_post_release_branch_required() {
let check = make_release_branch_check().required(true).build().unwrap();
let result = run_branch_check(
"test_post_release_branch_required",
POST_RELEASE_BRANCH,
check,
);
test_result_errors(result, &[
"This branch is ineligible for the release branch; it needs to be based on a commit \
before d02f015907371738253a22b9a7fec78607a969b2.",
]);
}
#[test]
fn test_post_release_branch_merge() {
let check = make_release_branch_check().build().unwrap();
run_branch_check_ok(
"test_post_release_branch_merge",
POST_RELEASE_BRANCH_MERGE,
check,
);
}
#[test]
fn test_post_release_branch_merge_required() {
let check = make_release_branch_check().required(true).build().unwrap();
let result = run_branch_check(
"test_post_release_branch_merge_required",
POST_RELEASE_BRANCH_MERGE,
check,
);
test_result_errors(result, &[
"This branch is ineligible for the release branch; it needs to be based on a commit \
before d02f015907371738253a22b9a7fec78607a969b2.",
]);
}
}