1
- use crate :: utils:: env:: { RoverEnv , RoverEnvKey } ;
2
- use crate :: Result ;
3
- use rover_client:: query:: { graph, subgraph} ;
1
+ use crate :: query:: { graph, subgraph} ;
4
2
5
3
use std:: env;
6
4
7
5
use git2:: { Reference , Repository } ;
8
6
use git_url_parse:: GitUrl ;
9
7
10
- #[ derive( Debug , PartialEq ) ]
8
+ #[ derive( Debug , Clone , PartialEq ) ]
11
9
pub struct GitContext {
12
10
pub branch : Option < String > ,
13
11
pub author : Option < String > ,
14
12
pub commit : Option < String > ,
15
- pub message : Option < String > ,
16
13
pub remote_url : Option < String > ,
17
14
}
18
15
19
16
impl GitContext {
20
- pub fn try_from_rover_env ( env : & RoverEnv ) -> Result < Self > {
21
- let repo = Repository :: discover ( env:: current_dir ( ) ?) ;
22
- match repo {
23
- Ok ( repo) => {
24
- let head = repo. head ( ) . ok ( ) ;
25
- Ok ( Self {
26
- branch : GitContext :: get_branch ( env, head. as_ref ( ) ) ?,
27
- commit : GitContext :: get_commit ( env, head. as_ref ( ) ) ?,
28
- author : GitContext :: get_author ( env, head. as_ref ( ) ) ?,
29
- remote_url : GitContext :: get_remote_url ( env, Some ( & repo) ) ?,
30
- message : None ,
31
- } )
17
+ pub fn new ( override_git_context : GitContext ) -> Self {
18
+ let repo = GitContext :: get_repo ( ) ;
19
+
20
+ let mut remote_url = override_git_context. remote_url ;
21
+
22
+ if let Some ( repo) = repo {
23
+ remote_url = remote_url. or_else ( || GitContext :: get_remote_url ( & repo) ) ;
24
+ if let Ok ( head) = repo. head ( ) {
25
+ let branch = override_git_context
26
+ . branch
27
+ . or_else ( || GitContext :: get_branch ( & head) ) ;
28
+
29
+ let author = override_git_context
30
+ . author
31
+ . or_else ( || GitContext :: get_author ( & head) ) ;
32
+
33
+ let commit = override_git_context
34
+ . commit
35
+ . or_else ( || GitContext :: get_commit ( & head) ) ;
36
+
37
+ return GitContext {
38
+ branch,
39
+ author,
40
+ commit,
41
+ remote_url,
42
+ } ;
32
43
}
33
- Err ( _ ) => Ok ( Self {
34
- branch : GitContext :: get_branch ( env , None ) ? ,
35
- commit : GitContext :: get_commit ( env , None ) ? ,
36
- author : GitContext :: get_author ( env , None ) ? ,
37
- remote_url : GitContext :: get_remote_url ( env , None ) ? ,
38
- message : None ,
39
- } ) ,
44
+ }
45
+
46
+ GitContext {
47
+ branch : override_git_context . branch ,
48
+ author : override_git_context . author ,
49
+ commit : override_git_context . commit ,
50
+ remote_url ,
40
51
}
41
52
}
42
53
43
- fn get_branch ( env : & RoverEnv , head : Option < & Reference > ) -> Result < Option < String > > {
44
- Ok ( env. get ( RoverEnvKey :: VcsBranch ) ?. or_else ( || {
45
- let mut branch = None ;
46
- if let Some ( head) = head {
47
- branch = head. shorthand ( ) . map ( |s| s. to_string ( ) )
48
- }
49
- branch
50
- } ) )
54
+ pub fn default ( ) -> Self {
55
+ GitContext :: new ( GitContext {
56
+ author : None ,
57
+ branch : None ,
58
+ commit : None ,
59
+ remote_url : None ,
60
+ } )
51
61
}
52
62
53
- fn get_commit ( env : & RoverEnv , head : Option < & Reference > ) -> Result < Option < String > > {
54
- Ok ( env. get ( RoverEnvKey :: VcsCommit ) ?. or_else ( || {
55
- let mut commit = None ;
56
- if let Some ( head) = head {
57
- if let Ok ( head_commit) = head. peel_to_commit ( ) {
58
- commit = Some ( head_commit. id ( ) . to_string ( ) )
59
- }
60
- }
61
- commit
62
- } ) )
63
+ fn get_repo ( ) -> Option < Repository > {
64
+ env:: current_dir ( )
65
+ . map ( |d| Repository :: discover ( d) . ok ( ) )
66
+ . ok ( )
67
+ . flatten ( )
63
68
}
64
69
65
- fn get_author ( env : & RoverEnv , head : Option < & Reference > ) -> Result < Option < String > > {
66
- Ok ( env. get ( RoverEnvKey :: VcsAuthor ) ?. or_else ( || {
67
- let mut author = None ;
68
- if let Some ( head) = head {
69
- if let Ok ( head_commit) = head. peel_to_commit ( ) {
70
- author = Some ( head_commit. author ( ) . to_string ( ) )
71
- }
72
- }
73
- author
74
- } ) )
70
+ fn get_branch ( head : & Reference ) -> Option < String > {
71
+ head. shorthand ( ) . map ( |s| s. to_string ( ) )
75
72
}
76
73
77
- fn get_remote_url ( env : & RoverEnv , repo : Option < & Repository > ) -> Result < Option < String > > {
78
- let remote_url = env. get ( RoverEnvKey :: VcsRemoteUrl ) ?. or_else ( || {
79
- let mut remote_url = None ;
80
- if let Some ( repo) = repo {
81
- if let Ok ( remote) = repo. find_remote ( "origin" ) {
82
- remote_url = remote. url ( ) . map ( |r| r. to_string ( ) )
83
- }
84
- }
85
- remote_url
86
- } ) ;
74
+ fn get_commit ( head : & Reference ) -> Option < String > {
75
+ if let Ok ( head_commit) = head. peel_to_commit ( ) {
76
+ Some ( head_commit. id ( ) . to_string ( ) )
77
+ } else {
78
+ None
79
+ }
80
+ }
87
81
88
- Ok ( if let Some ( remote_url) = remote_url {
89
- GitContext :: sanitize_remote_url ( & remote_url)
82
+ fn get_author ( head : & Reference ) -> Option < String > {
83
+ if let Ok ( head_commit) = head. peel_to_commit ( ) {
84
+ Some ( head_commit. author ( ) . to_string ( ) )
90
85
} else {
91
86
None
92
- } )
87
+ }
88
+ }
89
+
90
+ fn get_remote_url ( repo : & Repository ) -> Option < String > {
91
+ let remote_url = if let Ok ( remote) = repo. find_remote ( "origin" ) {
92
+ remote. url ( ) . map ( |r| r. to_string ( ) )
93
+ } else {
94
+ None
95
+ } ;
96
+ remote_url
97
+ . map ( |r| GitContext :: sanitize_remote_url ( & r) )
98
+ . flatten ( )
93
99
}
94
100
95
101
// Parses and sanitizes git remote urls according to the same rules as
@@ -139,7 +145,7 @@ impl From<GitContext> for GraphPublishContextInput {
139
145
commit : git_context. commit ,
140
146
committer : git_context. author ,
141
147
remote_url : git_context. remote_url ,
142
- message : git_context . message ,
148
+ message : None ,
143
149
}
144
150
}
145
151
}
@@ -152,7 +158,7 @@ impl From<GitContext> for GraphCheckContextInput {
152
158
commit : git_context. commit ,
153
159
committer : git_context. author ,
154
160
remote_url : git_context. remote_url ,
155
- message : git_context . message ,
161
+ message : None ,
156
162
}
157
163
}
158
164
}
@@ -166,7 +172,7 @@ impl From<GitContext> for SubgraphPublishContextInput {
166
172
commit : git_context. commit ,
167
173
committer : git_context. author ,
168
174
remote_url : git_context. remote_url ,
169
- message : git_context . message ,
175
+ message : None ,
170
176
}
171
177
}
172
178
}
@@ -180,15 +186,14 @@ impl From<GitContext> for SubgraphCheckContextInput {
180
186
commit : git_context. commit ,
181
187
committer : git_context. author ,
182
188
remote_url : git_context. remote_url ,
183
- message : git_context . message ,
189
+ message : None ,
184
190
}
185
191
}
186
192
}
187
193
188
194
#[ cfg( test) ]
189
195
mod tests {
190
196
use super :: * ;
191
- use crate :: PKG_NAME ;
192
197
193
198
#[ test]
194
199
fn removed_user_from_remote_with_only_user ( ) {
@@ -319,30 +324,21 @@ mod tests {
319
324
let commit = "f84b32caddddfdd9fa87d7ce2140d56eabe805ee" . to_string ( ) ;
320
325
let remote_url =
"[email protected] :roku/theworstremoteintheworld.git" . to_string ( ) ;
321
326
322
- let mut rover_env = RoverEnv :: new ( ) ;
323
- rover_env. insert ( RoverEnvKey :: VcsBranch , & branch) ;
324
- rover_env. insert ( RoverEnvKey :: VcsAuthor , & author) ;
325
- rover_env. insert ( RoverEnvKey :: VcsCommit , & commit) ;
326
- rover_env. insert ( RoverEnvKey :: VcsRemoteUrl , & remote_url) ;
327
-
328
- let expected_git_context = GitContext {
327
+ let override_git_context = GitContext {
329
328
branch : Some ( branch) ,
330
329
author : Some ( author) ,
331
330
commit : Some ( commit) ,
332
- message : None ,
333
331
remote_url : Some ( remote_url) ,
334
332
} ;
335
333
336
- let actual_git_context = GitContext :: try_from_rover_env ( & rover_env)
337
- . expect ( "Could not create GitContext from RoverEnv" ) ;
334
+ let actual_git_context = GitContext :: new ( override_git_context. clone ( ) ) ;
338
335
339
- assert_eq ! ( expected_git_context , actual_git_context) ;
336
+ assert_eq ! ( override_git_context , actual_git_context) ;
340
337
}
341
338
342
339
#[ test]
343
- fn it_can_create_git_context_committ_author_remote_url ( ) {
344
- let git_context =
345
- GitContext :: try_from_rover_env ( & RoverEnv :: new ( ) ) . expect ( "Could not create git context" ) ;
340
+ fn it_can_create_git_context_commit_author_remote_url ( ) {
341
+ let git_context = GitContext :: default ( ) ;
346
342
347
343
assert ! ( git_context. branch. is_some( ) ) ;
348
344
assert ! ( git_context. author. is_some( ) ) ;
@@ -353,10 +349,8 @@ mod tests {
353
349
panic ! ( "Could not find the commit hash" ) ;
354
350
}
355
351
356
- assert ! ( git_context. message. is_none( ) ) ;
357
-
358
352
if let Some ( remote_url) = git_context. remote_url {
359
- assert ! ( remote_url. contains( PKG_NAME ) ) ;
353
+ assert ! ( remote_url. contains( "apollographql" ) ) ;
360
354
} else {
361
355
panic ! ( "GitContext could not find the remote url" ) ;
362
356
}
0 commit comments