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
use impl_prelude::*;
#[derive(Debug)]
pub struct ReleaseBranch {
branch: String,
disallowed_commit: CommitId,
required: bool,
}
impl ReleaseBranch {
pub fn new<B, C>(branch: B, commit: C) -> Self
where B: ToString,
C: ToString,
{
ReleaseBranch {
branch: branch.to_string(),
disallowed_commit: CommitId::new(commit),
required: false,
}
}
pub fn set_required(&mut self, required: bool) -> &mut Self {
self.required = required;
self
}
}
impl BranchCheck for ReleaseBranch {
fn name(&self) -> &str {
"release-branch"
}
fn check(&self, ctx: &CheckGitContext, commit: &CommitId) -> Result<CheckResult> {
let merge_base = ctx.git()
.arg("merge-base")
.arg("--all")
.arg(commit.as_str())
.arg(self.disallowed_commit.as_str())
.output()
.chain_err(|| "failed to construct merge-base command")?;
if !merge_base.status.success() {
bail!(ErrorKind::Git(format!("failed to get the merge base for a release branch: {}",
String::from_utf8_lossy(&merge_base.stderr))));
}
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(test)]
mod tests {
use checks::ReleaseBranch;
use checks::test::*;
static RELEASE_BRANCH: &'static str = "3a22ca19fda09183da2faab60819ff6807568acd";
static POST_RELEASE_COMMIT: &'static str = "d02f015907371738253a22b9a7fec78607a969b2";
static POST_RELEASE_BRANCH: &'static str = "a61fd3759b61a4a1f740f3fe656bc42151cefbdd";
static POST_RELEASE_BRANCH_MERGE: &'static str = "c58dd19d9976722d82aa6bc6a52a2a01a52bd9e8";
fn make_release_branch_check() -> ReleaseBranch {
ReleaseBranch::new("release", POST_RELEASE_COMMIT)
}
#[test]
fn test_release_branch_ok() {
let check = make_release_branch_check();
let mut conf = GitCheckConfiguration::new();
conf.add_branch_check(&check);
let result = test_check("test_release_branch_ok", RELEASE_BRANCH, &conf);
assert_eq!(result.warnings().len(), 1);
assert_eq!(result.warnings()[0], "Eligible for the release branch.");
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 0);
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), true);
}
#[test]
fn test_release_branch_ok_required() {
let mut check = make_release_branch_check();
check.set_required(true);
let mut conf = GitCheckConfiguration::new();
conf.add_branch_check(&check);
let result = test_check("test_release_branch_ok_required", RELEASE_BRANCH, &conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 0);
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), true);
}
#[test]
fn test_post_release_branch() {
let check = make_release_branch_check();
let mut conf = GitCheckConfiguration::new();
conf.add_branch_check(&check);
let result = test_check("test_post_release_branch", POST_RELEASE_BRANCH, &conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 0);
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), true);
}
#[test]
fn test_post_release_branch_required() {
let mut check = make_release_branch_check();
check.set_required(true);
let mut conf = GitCheckConfiguration::new();
conf.add_branch_check(&check);
let result = test_check("test_post_release_branch_required",
POST_RELEASE_BRANCH,
&conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 1);
assert_eq!(result.errors()[0],
"This branch is ineligible for the release branch; it needs to be based on a \
commit before d02f015907371738253a22b9a7fec78607a969b2.");
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
#[test]
fn test_post_release_branch_merge() {
let check = make_release_branch_check();
let mut conf = GitCheckConfiguration::new();
conf.add_branch_check(&check);
let result = test_check("test_post_release_branch_merge",
POST_RELEASE_BRANCH_MERGE,
&conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 0);
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), true);
}
#[test]
fn test_post_release_branch_merge_required() {
let mut check = make_release_branch_check();
check.set_required(true);
let mut conf = GitCheckConfiguration::new();
conf.add_branch_check(&check);
let result = test_check("test_post_release_branch_merge_required",
POST_RELEASE_BRANCH_MERGE,
&conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 1);
assert_eq!(result.errors()[0],
"This branch is ineligible for the release branch; it needs to be based on a \
commit before d02f015907371738253a22b9a7fec78607a969b2.");
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
}