Skip to content

Disallow incorrectly formatted Git URLs #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class GitUtilities {

Expand Down Expand Up @@ -45,17 +47,45 @@ public static String getGitStatus(File gitDir) {
}
}



public static String getGitSource(File gitDir) {
if (!gitDir.exists()) {
return "";
}
try {
String[] cmd = { "git", "remote", "get-url", "origin" };
return execAndReturnString(cmd, new String[]{}, gitDir);
} catch (Exception e) {
final String[] cmd = { "git", "remote", "get-url", "origin" };
final String url = execAndReturnString(cmd, new String[]{}, gitDir);
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: ", "") );
return "";
}
}


/**
* Return the URL with no user information.
* <p/>
* This will also send log info to console that reports user information removal, as well as a malformed URL
* resulting in a null return.
*
* @param url The URL
* @param urlSource A string representing the source of the URL to be output (CLI param, git remote, etc.)
* @return A valid URL that does not contain user information or null if the url param was malformed.
*/
protected static String getURLWithNoUserInfo(final String url, final String urlSource) {
try {
URL newUrl = new URL(url);
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();
}
return url;
} catch (MalformedURLException e) {
System.out.println("Warning @ Git URL is not a valid URl. Source: " + urlSource);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,14 @@ public enum CacheOption {
private String packagesFolder;
private String targetOutput;
private String repoSource;

private void setRepoSource(final String repoSource) {
if (repoSource == null) {
return;
}
this.repoSource = GitUtilities.getURLWithNoUserInfo(repoSource, "-repo CLI parameter");
}

private String targetOutputNested;

private String folderToDelete;
Expand Down Expand Up @@ -14251,7 +14259,7 @@ public static void main(String[] args) throws Exception {
if (CliParams.hasNamedParam(args, "-auto-ig-build")) {
self.setMode(IGBuildMode.AUTOBUILD);
self.targetOutput = CliParams.getNamedParam(args, "-target");
self.repoSource = CliParams.getNamedParam(args, "-repo");
self.setRepoSource( CliParams.getNamedParam(args, "-repo"));
}

if (CliParams.hasNamedParam(args, "-no-narrative")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.hl7.fhir.igtools;

public class GitTests {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hl7.fhir.igtools.publisher;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.hl7.fhir.igtools.publisher.GitUtilities.execAndReturnString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -11,32 +12,36 @@
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeAll;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import javax.annotation.Nonnull;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class GitUtilitiesTests {

File normalBranchDirectory;
File worktreeBranchDirectory;
@BeforeAll
public void beforeAll() throws IOException, InterruptedException {
public static final String VALID_URL = "https://github.com/FHIR/fhir-core-examples.git";
public static final String USERNAME_AND_TOKEN_URL = "https://username:token@github.com/FHIR/fhir-core-examples.git";
public static final String NORMAL_BRANCH = "normal-branch";
public static final String WORKTREE_BRANCH = "branch-a";

public File initiateGitDirectory() throws IOException, InterruptedException {
File gitRoot = Files.createTempDirectory("testGitDirectory").toFile();

normalBranchDirectory = Path.of(gitRoot.getAbsolutePath(),"normal-branch").toFile();
File normalBranchDirectory = getGitBranchDirectory(gitRoot, NORMAL_BRANCH);
normalBranchDirectory.mkdir();

Path worktreeBranchPath = Path.of(gitRoot.getAbsolutePath(), "branch-a");

System.out.println(execAndReturnString(new String[]{"git", "init"}, null, normalBranchDirectory));

//git config --global user.email "you@example.com"
System.out.println(execAndReturnString(new String[]{"git", "config", "user.email", "\"dummy@dummy.org\""}, null, normalBranchDirectory));

System.out.println(execAndReturnString(new String[]{"git", "config", "user.name", "\"guyincognito\""}, null, normalBranchDirectory));

File dummyFile = Path.of(normalBranchDirectory.getAbsolutePath().toString(), "dummy.txt").toFile();
File dummyFile = getGitBranchDirectory(normalBranchDirectory, "dummy.txt");
dummyFile.createNewFile();

BufferedWriter writer = new BufferedWriter(new FileWriter(dummyFile));
Expand All @@ -46,28 +51,68 @@ public void beforeAll() throws IOException, InterruptedException {

System.out.println(execAndReturnString(new String[]{"git", "add", "--all"},null, normalBranchDirectory));
System.out.println(execAndReturnString(new String[]{"git", "commit", "-m", "test"},null, normalBranchDirectory));
System.out.println(execAndReturnString(new String[]{"git", "branch", "branch-a"}, null, normalBranchDirectory));
System.out.println(execAndReturnString(new String[]{"git", "branch", WORKTREE_BRANCH}, null, normalBranchDirectory));

System.out.println(execAndReturnString(new String[]{"git", "checkout", "-b", "branch-b"}, null, normalBranchDirectory));
return gitRoot;
}

private static @NotNull File getGitBranchDirectory(File gitRoot, String branchName) {
return Path.of(gitRoot.getAbsolutePath(), branchName).toFile();
}

private static @Nonnull File createWorktree(File gitRoot) throws IOException, InterruptedException {
Path worktreeBranchPath = Path.of(gitRoot.getAbsolutePath(), WORKTREE_BRANCH);
System.out.println(execAndReturnString(new String[]{"git", "worktree", "add", worktreeBranchPath.toString(), WORKTREE_BRANCH}, null, getGitBranchDirectory(gitRoot, NORMAL_BRANCH)));
return worktreeBranchPath.toFile();
}


System.out.println(execAndReturnString(new String[]{"git", "worktree", "add", worktreeBranchPath.toString(), "branch-a"}, null, normalBranchDirectory));

worktreeBranchDirectory = worktreeBranchPath.toFile();
private static void createOriginURL(File gitRoot, String urlString) throws IOException, InterruptedException {
System.out.println(execAndReturnString(new String[]{"git", "remote", "add", "origin", urlString }, null, getGitBranchDirectory(gitRoot, NORMAL_BRANCH)));
}

@Test
public void testGetGitStatus() {
public void testGetGitStatus() throws IOException, InterruptedException {
File gitRoot = initiateGitDirectory();
File normalBranchDirectory = getGitBranchDirectory(gitRoot, NORMAL_BRANCH);
String output = GitUtilities.getGitStatus(normalBranchDirectory);
assertEquals("branch-b", output.trim());
}

@Test
public void testGetGitWorktreeStatus() {
public void testGetGitWorktreeStatus() throws IOException, InterruptedException {
File gitRoot = initiateGitDirectory();
File worktreeBranchDirectory = createWorktree(gitRoot);
String output = GitUtilities.getGitStatus(worktreeBranchDirectory);
assertEquals("branch-a", output.trim());
assertEquals(WORKTREE_BRANCH, output.trim());
}
@Test
public void testGitStatusWhenNotGitDirectory() throws IOException {
String output = GitUtilities.getGitStatus(Files.createTempDirectory("emptyNonGitDirectory").toFile());
assertTrue(output.length() == 0);
}

@ParameterizedTest
@CsvSource(value= {
VALID_URL+","+VALID_URL,
"whwehwhasdlksdjsdf,''",
USERNAME_AND_TOKEN_URL+","+ VALID_URL})
void testGetGitSource(String url, String expected) throws IOException, InterruptedException {
File gitRoot = initiateGitDirectory();
createOriginURL(gitRoot, url);
String actual = GitUtilities.getGitSource(getGitBranchDirectory(gitRoot, NORMAL_BRANCH));
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@CsvSource(value= {
VALID_URL+","+VALID_URL,
"whwehwhasdlksdjsdf,NULL",
USERNAME_AND_TOKEN_URL+","+VALID_URL}, nullValues={"NULL"})
public void testGetURLWithNoUserInfo(String url, String expected) {
String result = GitUtilities.getURLWithNoUserInfo(url, "test");
assertThat(result).isEqualTo(expected);
}
}
Loading