forked from guacsec/trustify-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefix.rs
More file actions
206 lines (189 loc) · 7.58 KB
/
prefix.rs
File metadata and controls
206 lines (189 loc) · 7.58 KB
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
// Motivated by, and largely copied from,
// https://github.com/kubernetes-sigs/kubebuilder-release-tools
use core::fmt;
use regex::Regex;
use crate::error::Result;
//these prefixes in the PR title prevent validation failure
const PREFIX_FEATURE: (&str, &str) = (":sparkles:", "✨");
const PREFIX_BUG_FIX: (&str, &str) = (":bug:", "🐛");
const PREFIX_DOCS: (&str, &str) = (":book:", "📖");
const PREFIX_INFRA: (&str, &str) = (":seedling:", "🌱");
const PREFIX_BREAKING: (&str, &str) = (":warning:", "⚠");
const PREFIX_NO_NOTE: (&str, &str) = (":ghost:", "👻");
#[derive(Debug, PartialEq)]
pub enum PRType {
Feature(String),
BugFix(String),
Docs(String),
Infra(String),
Breaking(String),
NoNote(String),
}
impl fmt::Display for PRType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Feature(title) => write!(f, "PR type 'Feature'\n PR title '{}'", title),
Self::BugFix(title) => write!(f, "PR type 'Bug'\n PR title '{}'", title),
Self::Docs(title) => write!(f, "PR type 'Docs'\n PR title '{}'", title),
Self::Infra(title) => write!(f, "PR type 'Infra'\n PR title '{}'", title),
Self::Breaking(title) => write!(f, "PR type 'Breaking'\n PR title '{}'", title),
Self::NoNote(title) => write!(f, "PR type 'NoNote'\n PR title '{}'", title),
}
}
}
impl PRType {
pub fn from_title(value: &str) -> Result<Self> {
let wip_regex = Regex::new(r"(?i)^\W?WIP\W").unwrap();
let tag_regex = Regex::new(r"^\[[\w.-]*]").unwrap();
// Remove the WIP prefix if found.
let value = wip_regex.replace_all(value, "");
// Trim to remove spaces after WIP.
let value = value.trim();
// Remove a tag prefix if found.
let value = tag_regex.replace_all(value, "");
let value = value.trim();
if value.is_empty() {
return Err(crate::error::Error::InvalidTitle {
title: value.to_string(),
emoji: None,
});
}
// Trusting those that came before...
// https://github.com/kubernetes-sigs/kubebuilder-release-tools/blob/4f3d1085b4458a49ed86918b4b55505716715b77/notes/common/prefix.go#L123-L125
// strip the variation selector from the title, if present
// (some systems sneak it in -- my guess is OSX)
fn trust(title: &str) -> String {
let result = if title.starts_with('\u{FE0F}') {
let result: String = title.chars().skip(1).collect();
result
} else {
title.to_string()
};
result.trim().to_string()
}
if let Some(title) = value.strip_prefix(PREFIX_FEATURE.0) {
Ok(PRType::Feature(trust(title)))
} else if let Some(title) = value.strip_prefix(PREFIX_BUG_FIX.0) {
Ok(PRType::BugFix(trust(title)))
} else if let Some(title) = value.strip_prefix(PREFIX_DOCS.0) {
Ok(PRType::Docs(trust(title)))
} else if let Some(title) = value.strip_prefix(PREFIX_INFRA.0) {
Ok(PRType::Infra(trust(title)))
} else if let Some(title) = value.strip_prefix(PREFIX_BREAKING.0) {
Ok(PRType::Breaking(trust(title)))
} else if let Some(title) = value.strip_prefix(PREFIX_NO_NOTE.0) {
Ok(PRType::NoNote(trust(title)))
} else if value.strip_prefix(PREFIX_FEATURE.1).is_some()
|| value.strip_prefix(PREFIX_BUG_FIX.1).is_some()
|| value.strip_prefix(PREFIX_DOCS.1).is_some()
|| value.strip_prefix(PREFIX_INFRA.1).is_some()
|| value.strip_prefix(PREFIX_BREAKING.1).is_some()
|| value.strip_prefix(PREFIX_NO_NOTE.1).is_some()
{
let emoji = value.chars().next().map(|c| c.to_string());
return Err(crate::error::Error::InvalidTitle {
title: trust(value),
emoji,
});
} else {
return Err(crate::error::Error::InvalidTitle {
title: trust(value),
emoji: None,
});
}
}
pub fn title(&self) -> String {
match self {
PRType::Feature(title) => title.to_string(),
PRType::BugFix(title) => title.to_string(),
PRType::Docs(title) => title.to_string(),
PRType::Infra(title) => title.to_string(),
PRType::Breaking(title) => title.to_string(),
PRType::NoNote(title) => title.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use crate::{error::Result, prefix::PRType};
struct TestCase {
pub title: &'static str,
pub expected_result: Result<PRType>,
}
#[test]
//test title validation
fn title_cases() {
let test_cases = vec![
TestCase {
title: "WIP: [docs] Update documentation",
expected_result: Err(crate::error::Error::InvalidTitle {
title: "Update documentation".to_string(),
emoji: None,
}),
},
TestCase {
title: "WIP: :sparkles: Add new feature",
expected_result: Ok(PRType::Feature("Add new feature".to_string())),
},
TestCase {
title: "WIP: :warning: Breaking change",
expected_result: Ok(PRType::Breaking("Breaking change".to_string())),
},
TestCase {
title: "WIP: :bug: Fix bug",
expected_result: Ok(PRType::BugFix("Fix bug".to_string())),
},
TestCase {
title: ":ghost: Don't put me in release notes",
expected_result: Ok(PRType::NoNote("Don't put me in release notes".to_string())),
},
TestCase {
title: "WIP: :seedling: Infrastructure change",
expected_result: Ok(PRType::Infra("Infrastructure change".to_string())),
},
TestCase {
title: "WIP: No prefix in title",
expected_result: Err(crate::error::Error::InvalidTitle {
title: "No prefix in title".to_string(),
emoji: None,
}),
},
TestCase {
title: "No prefix in title",
expected_result: Err(crate::error::Error::InvalidTitle {
title: "No prefix in title".to_string(),
emoji: None,
}),
},
TestCase {
title: "WIP:",
expected_result: Err(crate::error::Error::InvalidTitle {
title: "".to_string(),
emoji: None,
}),
},
TestCase {
title: "",
expected_result: Err(crate::error::Error::InvalidTitle {
title: "".to_string(),
emoji: None,
}),
},
TestCase {
title: "WIP: [tag] :sparkles: Add new feature",
expected_result: Ok(PRType::Feature("Add new feature".to_string())),
},
TestCase {
title: "👻 I should have used the alias",
expected_result: Err(crate::error::Error::InvalidTitle {
title: "👻 I should have used the alias".to_string(),
emoji: Some("👻".to_string()),
}),
},
];
for tc in test_cases {
let pr = PRType::from_title(tc.title);
assert_eq!(tc.expected_result, pr);
}
}
}