Skip to content

Commit

Permalink
Review the code to support the git protocol. quarkiverse#68
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard authored and iocanel committed Jan 23, 2025
1 parent 7a22db3 commit 819f4a4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Map;
import java.util.Optional;

import io.dekorate.utils.Git;
import io.quarkiverse.argocd.deployment.utils.Git;
import io.quarkus.builder.item.SimpleBuildItem;

public final class ScmInfoBuildItem extends SimpleBuildItem {
Expand Down Expand Up @@ -55,7 +55,7 @@ public static Optional<ScmInfoBuildItem> fromPath(Path path) {
return scmInfo;
}
try {
Map<String, String> remotes = Git.getRemotes(path);
Map<String, String> remotes = io.dekorate.utils.Git.getRemotes(path);
if (remotes.isEmpty()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,40 @@ public static Optional<String> getRemoteUrl(Path path, String remote, boolean ht
}
}

/**
* Generate an HTTPS url of an SCM according to: https://git-scm.com/docs/git-clone#_git_urls
* <p>
* The method handles different protocols of an SCM repository:
* HTTP : http[s]://<GIT_SERVER>[:<PORT>]/path/project[.git]
* HTTP with Basic Auth : http[s]://<username>:<password>@<GIT_SERVER>[:<PORT>]/path/project[.git]
* Git : git@<GIT_SERVER>:<REPOSITORY_ORG>/path/project[.git]
*
* @param remoteUrl the String of the url of the scm.
* @return The String of the url converted.
*/
public static String sanitizeRemoteUrl(String remoteUrl) {
final int atSign = remoteUrl.indexOf('@');
if (atSign > 0) {
remoteUrl = remoteUrl.substring(atSign + 1);
remoteUrl = "https://" + remoteUrl;
if (remoteUrl == null || remoteUrl.isEmpty()) {
throw new IllegalArgumentException("The remote URL cannot be null or empty.");
}

String normalizedUrl = null;

if (remoteUrl.startsWith("http://") || remoteUrl.startsWith("https://")) {
// Handle HTTP(S) and HTTP(S) with Basic Auth
normalizedUrl = remoteUrl.replaceAll("https?://[\\w%:]*@", "https://");
} else if (remoteUrl.startsWith("git@")) {
// Handle Git URL (SSH)
normalizedUrl = remoteUrl.replaceFirst("git@([\\w.-]+):", "https://$1/");
} else {
throw new IllegalArgumentException("Unsupported URL format: " + remoteUrl);
}
if (!remoteUrl.endsWith(".git")) {
remoteUrl += ".git";

// Ensure the URL ends with .git
if (!normalizedUrl.endsWith(".git")) {
normalizedUrl += ".git";
}
return remoteUrl;

return normalizedUrl;
}

public static Optional<String> getBranch(Path path) {
Expand Down Expand Up @@ -74,7 +98,7 @@ public static Optional<String> getCommitSHA(Path path) {
*
* @param remote The target remote.
* @param state An atomic boolean which holds the predicate state.
* @reuturn The predicate.
* @return The predicate.
*/
public static Predicate<String> inRemote(String remote, AtomicBoolean state) {
return l -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,40 @@

import io.quarkiverse.argocd.deployment.utils.Git;

public class RepoUrlTest {
public class ScmTest {

@Test
public void httpsRepository() {
public void HttpProtocolScm() {
String url = "https://github.com/quarkiverse/quarkus-argocd";
String sanitizedRemoteUrl = Git.sanitizeRemoteUrl(url);
Assertions.assertEquals(url + ".git", sanitizedRemoteUrl);
}

@Test
public void httpsRepositoryWithSuffixGit() {
public void HttpProtocolScmWithSuffixGit() {
String url = "https://github.com/quarkiverse/quarkus-argocd.git";
String sanitizedRemoteUrl = Git.sanitizeRemoteUrl(url);
Assertions.assertEquals(url, sanitizedRemoteUrl);
}

@Test
public void httpsTokenRepository() {
public void HttpProtocolBasicAuthScm() {
String url = "https://[email protected]/quarkiverse/quarkus-argocd.git";
String sanitizedRemoteUrl = Git.sanitizeRemoteUrl(url);
Assertions.assertEquals("https://github.com/quarkiverse/quarkus-argocd.git", sanitizedRemoteUrl);
}

@Test
public void httpsTokenRepositoryAndPort() {
public void HttpProtocolBasicAuthScmAndPort() {
String url = "https://[email protected]:8443/quarkus/my-quarkus-hello.git";
String sanitizedRemoteUrl = Git.sanitizeRemoteUrl(url);
Assertions.assertEquals("https://gitea.cnoe.localtest.me:8443/quarkus/my-quarkus-hello.git", sanitizedRemoteUrl);
}

@Test
public void GitProtocolRepository() {
String url = "[email protected]:quarkus/my-quarkus-hello.git";
String sanitizedRemoteUrl = Git.sanitizeRemoteUrl(url);
Assertions.assertEquals("https://gitea.cnoe.localtest.me/quarkus/my-quarkus-hello.git", sanitizedRemoteUrl);
}
}

0 comments on commit 819f4a4

Please sign in to comment.