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
use impl_prelude::*;
use std::process::Command;
#[derive(Debug, Clone, Copy)]
pub enum ValidNameFullNamePolicy {
Required,
Preferred,
Optional,
}
impl Default for ValidNameFullNamePolicy {
fn default() -> Self {
ValidNameFullNamePolicy::Required
}
}
impl ValidNameFullNamePolicy {
fn apply<F>(&self, result: &mut CheckResult, msg: F)
where F: Fn(&str) -> String,
{
match *self {
ValidNameFullNamePolicy::Required => {
result.add_error(msg("required"));
},
ValidNameFullNamePolicy::Preferred => {
result.add_warning(msg("preferred"));
},
ValidNameFullNamePolicy::Optional => {},
}
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct ValidName {
full_name_policy: ValidNameFullNamePolicy,
}
fn check_name(name: &str) -> bool {
name.find(' ').is_some()
}
fn check_email(email: &str) -> bool {
let domain_part = email.splitn(2, '@')
.nth(1);
if let Some(domain) = domain_part {
let dig = Command::new("host")
.arg("-t").arg("MX")
.arg(format!("{}.", domain))
.output();
let dig_output = match dig {
Ok(dig_output) => dig_output,
Err(err) => {
error!(target: "git-checks/valid_name",
"failed to construct host command: {:?}",
err);
return false;
},
};
if dig_output.status.success() {
true
} else {
warn!(target: "git-checks/valid_name",
"failed to look up MX record for domain {}: {}",
domain,
String::from_utf8_lossy(&dig_output.stdout));
false
}
} else {
false
}
}
fn check_identity(what: &str, who: &str, identity: &Identity,
full_name_policy: ValidNameFullNamePolicy)
-> CheckResult {
let mut result = CheckResult::new();
if !check_name(&identity.name) {
full_name_policy.apply(&mut result, |policy| {
format!("The {} name (`{}`) for {} has no space in it. \
A full name is {} for contribution. Please set the \
`user.name` Git configuration value.",
who,
identity.name,
what,
policy)
});
}
if !check_email(&identity.email) {
result.add_error(format!("The {} email (`{}`) for {} has an unknown domain. Please set \
the `user.email` Git configuration value.",
who,
identity.email,
what));
}
result
}
impl ValidName {
pub fn new() -> Self {
Default::default()
}
pub fn set_full_name_policy(&mut self, policy: ValidNameFullNamePolicy) -> &mut Self {
self.full_name_policy = policy;
self
}
}
impl Check for ValidName {
fn name(&self) -> &str {
"valid-name"
}
fn check(&self, _: &CheckGitContext, commit: &Commit) -> Result<CheckResult> {
let what = format!("commit {}", commit.sha1);
Ok(if commit.author == commit.committer {
check_identity(&what, "given", &commit.author, self.full_name_policy)
} else {
let author_res = check_identity(&what, "author", &commit.author, self.full_name_policy);
let commiter_res =
check_identity(&what, "committer", &commit.committer, self.full_name_policy);
author_res.combine(commiter_res)
})
}
}
impl BranchCheck for ValidName {
fn name(&self) -> &str {
"valid-name"
}
fn check(&self, ctx: &CheckGitContext, _: &CommitId) -> Result<CheckResult> {
Ok(check_identity("the topic",
"owner",
ctx.topic_owner(),
self.full_name_policy))
}
}
#[cfg(test)]
mod tests {
use checks::ValidName;
use checks::ValidNameFullNamePolicy;
use checks::test::*;
static BAD_TOPIC: &'static str = "91d9fceb226bfc0faeb8a4e54b4f0b5a1ffd39e8";
static BAD_AUTHOR_NAME: &'static str = "edac4e5b3a00eac60280a78ee84b5ef8d4cce97a";
#[test]
fn test_valid_name_required() {
let check = ValidName::new();
let result = run_check("test_valid_name_required", BAD_TOPIC, check);
test_result_errors(result, &[
"The given name (`Mononym`) for commit 91d9fceb226bfc0faeb8a4e54b4f0b5a1ffd39e8 has \
no space in it. A full name is required for contribution. Please set the `user.name` \
Git configuration value.",
"The given email (`bademail`) for commit 91d9fceb226bfc0faeb8a4e54b4f0b5a1ffd39e8 has \
an unknown domain. Please set the `user.email` Git configuration value.",
"The committer email (`bademail@baddomain.invalid`) for commit \
dcd8895d299031d607481b4936478f8de4cc28ae has an unknown domain. Please set the \
`user.email` Git configuration value.",
"The author email (`bademail@baddomain.invalid`) for commit \
9002239437a06e81a58fed07150b215a917028d6 has an unknown domain. Please set the \
`user.email` Git configuration value.",
"The committer email (`bademail`) for commit da71ae048e5a387d6809558d59ad073d0e4fb089 \
has an unknown domain. Please set the `user.email` Git configuration value.",
"The committer name (`Mononym`) for commit 1debf1735a6e28880ef08f13baeea4b71a08a846 \
has no space in it. A full name is required for contribution. Please set the \
`user.name` Git configuration value.",
"The author email (`bademail`) for commit 9de4928f5ec425eef414ee7620d0692fda56ebb0 \
has an unknown domain. Please set the `user.email` Git configuration value.",
"The author name (`Mononym`) for commit edac4e5b3a00eac60280a78ee84b5ef8d4cce97a has \
no space in it. A full name is required for contribution. Please set the `user.name` \
Git configuration value.",
]);
}
#[test]
fn test_valid_name_preferred() {
let mut check = ValidName::new();
check.set_full_name_policy(ValidNameFullNamePolicy::Preferred);
let result = run_check("test_valid_name_preferred", BAD_AUTHOR_NAME, check);
test_result_warnings(result, &[
"The author name (`Mononym`) for commit edac4e5b3a00eac60280a78ee84b5ef8d4cce97a has \
no space in it. A full name is preferred for contribution. Please set the \
`user.name` Git configuration value.",
]);
}
#[test]
fn test_valid_name_optional() {
let mut check = ValidName::new();
check.set_full_name_policy(ValidNameFullNamePolicy::Optional);
run_check_ok("test_valid_name_optional", BAD_AUTHOR_NAME, check);
}
}