Skip to content

Commit

Permalink
Strip userInfo instead of returning null
Browse files Browse the repository at this point in the history
  • Loading branch information
dotasek committed Jan 23, 2025
1 parent 191ce25 commit e5ec29c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.hl7.fhir.igtools.publisher;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -58,7 +56,7 @@ public static String getGitSource(File gitDir) {
try {
final String[] cmd = { "git", "remote", "get-url", "origin" };
final String url = execAndReturnString(cmd, new String[]{}, gitDir);
final String noUserInfoUrl = getURLIfNoUserInfo(url, "git remote origin url");
final String noUserInfoUrl = getURLWithNoUserInfo(url, "git remote origin url");
return noUserInfoUrl == null ? "" : noUserInfoUrl;
} catch (Exception e) {
System.out.println("Warning @ Unable to read the git source: " + e.getMessage().replace("fatal: ", "") );
Expand All @@ -75,13 +73,12 @@ public static String getGitSource(File gitDir) {
* @param urlSource A string representing the source of the URL to be output
* @return A valid URL that does not contain user information, or null.
*/
protected static String getURLIfNoUserInfo(final String url, final String urlSource) {
protected static String getURLWithNoUserInfo(final String url, final String urlSource) {
try {
URL newUrl = new URL(url);
boolean result = newUrl.getUserInfo() != null;
if (result) {
System.out.println("Warning @ Git URL contains user information. Source: " + urlSource);
return null;
if (newUrl.getUserInfo() != null) {
System.out.println("Info @ Removing user info from GIT URL. Source: " + urlSource);
return new URL(newUrl.getProtocol(), newUrl.getHost(), newUrl.getPort(), newUrl.getFile()).toString();
}

} catch (MalformedURLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -850,7 +849,7 @@ private void setRepoSource(final String repoSource) {
if (repoSource == null) {
return;
}
this.repoSource = GitUtilities.getURLIfNoUserInfo(repoSource, "-repo CLI parameter");
this.repoSource = GitUtilities.getURLWithNoUserInfo(repoSource, "-repo CLI parameter");
}

private String targetOutputNested;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void testGitStatusWhenNotGitDirectory() throws IOException {
@CsvSource(value= {
VALID_URL+","+VALID_URL,
"whwehwhasdlksdjsdf,''",
USERNAME_AND_TOKEN_URL+",''"})
USERNAME_AND_TOKEN_URL+","+ VALID_URL})
void testGetGitSource(String url, String expected) throws IOException, InterruptedException {
File gitRoot = initiateGitDirectory();
createOriginURL(gitRoot, url);
Expand All @@ -110,9 +110,9 @@ void testGetGitSource(String url, String expected) throws IOException, Interrupt
@CsvSource(value= {
VALID_URL+","+VALID_URL,
"whwehwhasdlksdjsdf,NULL",
USERNAME_AND_TOKEN_URL+",NULL"}, nullValues={"NULL"})
USERNAME_AND_TOKEN_URL+","+VALID_URL}, nullValues={"NULL"})
public void getURLIfNoUserInfo(String url, String expected) {
String result = GitUtilities.getURLIfNoUserInfo(url, "test");
String result = GitUtilities.getURLWithNoUserInfo(url, "test");
assertThat(result).isEqualTo(expected);
}
}

0 comments on commit e5ec29c

Please sign in to comment.