-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathgit_context.rs
316 lines (270 loc) · 9.91 KB
/
git_context.rs
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
use std::{env, panic};
use git2::{Reference, Repository};
use git_url_parse::GitUrl;
#[derive(Debug, Clone, PartialEq)]
pub struct GitContext {
pub branch: Option<String>,
pub author: Option<String>,
pub commit: Option<String>,
pub remote_url: Option<String>,
}
impl GitContext {
pub fn new_with_override(override_git_context: GitContext) -> Self {
let repo = GitContext::get_repo();
let mut remote_url = override_git_context.remote_url;
if let Some(repo) = repo {
remote_url = remote_url.or_else(|| GitContext::get_remote_url(&repo));
if let Ok(head) = repo.head() {
let branch = override_git_context
.branch
.or_else(|| GitContext::get_branch(&head));
let author = override_git_context
.author
.or_else(|| GitContext::get_author(&head));
let commit = override_git_context
.commit
.or_else(|| GitContext::get_commit(&head));
return GitContext {
branch,
author,
commit,
remote_url,
};
}
}
GitContext {
branch: override_git_context.branch,
author: override_git_context.author,
commit: override_git_context.commit,
remote_url,
}
}
pub fn default() -> Self {
GitContext::new_with_override(GitContext {
author: None,
branch: None,
commit: None,
remote_url: None,
})
}
fn get_repo() -> Option<Repository> {
env::current_dir()
.map(|d| Repository::discover(d).ok())
.ok()
.flatten()
}
fn get_branch(head: &Reference) -> Option<String> {
head.shorthand().map(|s| s.to_string())
}
fn get_commit(head: &Reference) -> Option<String> {
if let Ok(head_commit) = head.peel_to_commit() {
Some(head_commit.id().to_string())
} else {
None
}
}
fn get_author(head: &Reference) -> Option<String> {
if let Ok(head_commit) = head.peel_to_commit() {
Some(head_commit.author().to_string())
} else {
None
}
}
fn get_remote_url(repo: &Repository) -> Option<String> {
let remote_url = if let Ok(remote) = repo.find_remote("origin") {
remote.url().map(|r| r.to_string())
} else {
None
};
remote_url
.map(|r| GitContext::sanitize_remote_url(&r))
.flatten()
}
// Parses and sanitizes git remote urls according to the same rules as
// defined in apollo-tooling https://github.com/apollographql/apollo-tooling/blob/fd642ab59620cd836651dcab4c3ecbcbcca3f780/packages/apollo/src/git.ts#L36
//
// If parsing fails, or if the url doesn't match a valid host, this fn
// will return None
fn sanitize_remote_url(remote_url: &str) -> Option<String> {
// try to parse url into git info
// GitUrl::parse can panic, so we attempt to catch it and
// just return None if the parsing fails.
let parsed_remote_url = panic::catch_unwind(|| GitUrl::parse(remote_url).ok())
.ok()
.flatten();
if let Some(mut parsed_remote_url) = parsed_remote_url {
// return None for any remote that is not a supported host
if let Some(host) = &parsed_remote_url.host {
match host.as_str() {
"github.com" | "gitlab.com" | "bitbucket.org" => {}
_ => return None,
}
} else {
return None;
};
let optional_user = parsed_remote_url.user.clone();
parsed_remote_url = parsed_remote_url.trim_auth();
// if the user is "git" we can add back in the user. Otherwise, return
// the clean remote url
// this is done previously here:
// https://github.com/apollographql/apollo-tooling/blob/fd642ab59620cd836651dcab4c3ecbcbcca3f780/packages/apollo/src/git.ts#L49
if let Some(user) = &optional_user {
if user == "git" {
parsed_remote_url.user = optional_user;
}
};
Some(parsed_remote_url.to_string())
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn removed_user_from_remote_with_only_user() {
let clean = GitContext::sanitize_remote_url("https://[email protected]/apollographql/test");
assert_eq!(
clean.unwrap(),
"https://bitbucket.org/apollographql/test".to_string()
);
}
#[test]
fn does_not_mind_case() {
let clean = GitContext::sanitize_remote_url("https://[email protected]/apollographql/test");
assert_eq!(
clean.unwrap(),
"https://github.com/apollographql/test".to_string()
);
}
#[test]
fn strips_usernames_from_ssh_urls() {
let clean = GitContext::sanitize_remote_url("ssh://un%[email protected]/apollographql/test");
assert_eq!(
clean.unwrap(),
"ssh://github.com:apollographql/test".to_string()
);
}
#[test]
fn works_with_special_chars() {
let clean = GitContext::sanitize_remote_url(
"https://un:p%40ssw%[email protected]/apollographql/test",
);
assert_eq!(
clean.unwrap(),
"https://github.com/apollographql/test".to_string()
);
let clean = GitContext::sanitize_remote_url(
"https://un:p%40ssw%[email protected]/apollographql/test",
);
assert_eq!(
clean.unwrap(),
"https://bitbucket.org/apollographql/test".to_string()
);
let clean = GitContext::sanitize_remote_url(
"https://un:p%40ssw%[email protected]/apollographql/test",
);
assert_eq!(
clean.unwrap(),
"https://gitlab.com/apollographql/test".to_string()
);
}
#[test]
/// preserves `git` user for github
fn works_with_non_url_github_remotes() {
let clean =
GitContext::sanitize_remote_url("[email protected]:apollographql/apollo-tooling.git");
assert_eq!(
clean.unwrap(),
"[email protected]:apollographql/apollo-tooling.git".to_string()
);
let clean =
GitContext::sanitize_remote_url("[email protected]:apollographql/apollo-tooling.git");
assert_eq!(
clean.unwrap(),
"github.com:apollographql/apollo-tooling.git".to_string()
);
}
#[test]
/// preserves `git` user for bitbucket
fn works_with_not_url_bitbucket_remotes() {
let clean =
GitContext::sanitize_remote_url("[email protected]:apollographql/apollo-tooling.git");
assert_eq!(
clean.unwrap(),
"[email protected]:apollographql/apollo-tooling.git".to_string()
);
let clean =
GitContext::sanitize_remote_url("[email protected]:apollographql/apollo-tooling.git");
assert_eq!(
clean.unwrap(),
"bitbucket.org:apollographql/apollo-tooling.git".to_string()
);
}
#[test]
/// preserves `git` user for gitlab
fn works_with_non_url_gitlab_remotes() {
let clean =
GitContext::sanitize_remote_url("[email protected]:apollographql/apollo-tooling.git");
assert_eq!(
clean.unwrap(),
"[email protected]:apollographql/apollo-tooling.git".to_string()
);
let clean =
GitContext::sanitize_remote_url("[email protected]:apollographql/apollo-tooling.git");
assert_eq!(
clean.unwrap(),
"gitlab.com:apollographql/apollo-tooling.git".to_string()
);
}
#[test]
fn does_not_allow_remotes_from_unrecognized_providers() {
let clean = GitContext::sanitize_remote_url("[email protected]:apollographql/apollo-tooling.git");
assert_eq!(clean, None);
}
#[test]
fn returns_none_unrecognized_protocol() {
let clean = GitContext::sanitize_remote_url(
"git+http://un:p%[email protected]/apollographql/test",
);
assert_eq!(clean, None);
}
#[test]
fn it_can_create_git_context_from_env() {
let branch = "mybranch".to_string();
let author = "test subject number one".to_string();
let commit = "f84b32caddddfdd9fa87d7ce2140d56eabe805ee".to_string();
let remote_url = "[email protected]:roku/theworstremoteintheworld.git".to_string();
let override_git_context = GitContext {
branch: Some(branch),
author: Some(author),
commit: Some(commit),
remote_url: Some(remote_url),
};
let actual_git_context = GitContext::new_with_override(override_git_context.clone());
assert_eq!(override_git_context, actual_git_context);
}
#[test]
fn it_can_create_git_context_commit_author_remote_url() {
let git_context = GitContext::default();
assert!(git_context.branch.is_some());
assert!(git_context.author.is_some());
if let Some(commit) = git_context.commit {
assert_eq!(commit.len(), 40);
} else {
panic!("Could not find the commit hash");
}
if let Some(remote_url) = git_context.remote_url {
assert!(remote_url.contains("apollographql"));
} else {
panic!("GitContext could not find the remote url");
}
}
#[test]
// regression test for https://github.com/apollographql/rover/issues/670
fn it_does_not_panic_on_remote_urls_with_no_apparent_owner() {
let clean = GitContext::sanitize_remote_url("ssh://[email protected]:4000/repo-name");
assert_eq!(clean, None);
}
}