1717
1818package pl .project13 .maven .git ;
1919
20+ import org .apache .http .client .utils .URIBuilder ;
2021import org .jetbrains .annotations .NotNull ;
2122import pl .project13 .maven .git .log .LoggerBridge ;
2223import pl .project13 .maven .git .util .PropertyManager ;
2324
24- import java .io .IOException ;
25+ import java .net .URI ;
26+ import java .net .URISyntaxException ;
2527import java .util .Map ;
2628import java .util .Properties ;
2729import java .util .TimeZone ;
2830import java .text .SimpleDateFormat ;
31+ import java .util .regex .Pattern ;
2932
3033import static com .google .common .base .Strings .isNullOrEmpty ;
3134
@@ -228,4 +231,48 @@ protected void put(@NotNull Properties properties, String key, String value) {
228231 log .info ("{} {}" , keyWithPrefix , value );
229232 PropertyManager .putWithoutPrefix (properties , keyWithPrefix , value );
230233 }
234+
235+ /**
236+ * Regex to check for SCP-style SSH+GIT connection strings such as 'git@github.com'
237+ */
238+ static final Pattern GIT_SCP_FORMAT = Pattern .compile ("^([a-zA-Z0-9_.+-])+@(.*)" );
239+ /**
240+ * If the git remote value is a URI and contains a user info component, strip the password from it if it exists.
241+ *
242+ * @param gitRemoteString The value of the git remote
243+ * @return
244+ * @throws GitCommitIdExecutionException
245+ */
246+ protected static String stripCredentialsFromOriginUrl (String gitRemoteString ) throws GitCommitIdExecutionException {
247+
248+ // The URL might be null if the repo hasn't set a remote
249+ if (gitRemoteString == null ) {
250+ return gitRemoteString ;
251+ }
252+
253+ // Remotes using ssh connection strings in the 'git@github' format aren't
254+ // proper URIs and won't parse . Plus since you should be using SSH keys,
255+ // credentials like are not in the URL.
256+ if (GIT_SCP_FORMAT .matcher (gitRemoteString ).matches ()) {
257+ return gitRemoteString ;
258+ }
259+ // At this point, we should have a properly formatted URL
260+ try {
261+ URI original = new URI (gitRemoteString );
262+ String userInfoString = original .getUserInfo ();
263+ if (null == userInfoString ) {
264+ return gitRemoteString ;
265+ }
266+ URIBuilder b = new URIBuilder (gitRemoteString );
267+ String [] userInfo = userInfoString .split (":" );
268+ // Build a new URL from the original URL, but nulling out the password
269+ // component of the userinfo. We keep the username so that ssh uris such
270+ // ssh://git@github.com will retain 'git@'.
271+ b .setUserInfo (userInfo [0 ]);
272+ return b .build ().toString ();
273+
274+ } catch (URISyntaxException e ) {
275+ throw new GitCommitIdExecutionException (e );
276+ }
277+ }
231278}
0 commit comments