-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#312: Added ability to prefer git (ssh) protocol instead of https for…
… cloning repos (#724)
- Loading branch information
Showing
10 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
cli/src/main/java/com/devonfw/tools/ide/context/GitUrlSyntax.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.devonfw.tools.ide.context; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Enum representing the syntax of Git URLs, either SSH or HTTPS. Provides methods to format and convert Git URLs based on the syntax. | ||
*/ | ||
public enum GitUrlSyntax { | ||
|
||
/** | ||
* The DEFAULT Git URL syntax | ||
*/ | ||
DEFAULT(""), | ||
/** | ||
* The SSH Git URL syntax (e.g., [email protected]:user/repo.git). | ||
*/ | ||
SSH("git@"), | ||
|
||
/** | ||
* The HTTPS Git URL syntax (e.g., https://github.com/user/repo.git). | ||
*/ | ||
HTTPS("https://"); | ||
|
||
private final String prefix; | ||
|
||
private static final List<String> DOMAINS_WITH_NO_CONVERSION = Arrays.asList("github.com"); | ||
|
||
GitUrlSyntax(String prefix) { | ||
this.prefix = prefix; | ||
} | ||
|
||
/** | ||
* Formats the given Git URL according to the syntax represented by this enum constant. | ||
* <p> | ||
* Converts the URL between SSH and HTTPS formats. For example, an HTTPS URL can be converted to its corresponding SSH URL format, and vice versa. | ||
* </p> | ||
* | ||
* @param gitUrl the original {@link GitUrl} to be formatted. | ||
* @return the formatted {@link GitUrl} according to this syntax. | ||
* @throws IllegalArgumentException if the protocol is not supported. | ||
*/ | ||
public GitUrl format(GitUrl gitUrl) { | ||
if (this == DEFAULT) { | ||
return gitUrl; | ||
} | ||
String url = gitUrl.url(); | ||
|
||
// Prevent conversion for domains in the no-conversion list | ||
if (isDomainWithNoConversion(url.toLowerCase())) { | ||
return gitUrl; | ||
} | ||
|
||
switch (this) { | ||
case SSH -> { | ||
if (url.startsWith(HTTPS.prefix)) { | ||
int index = url.indexOf("/", HTTPS.prefix.length()); | ||
if (index > 0) { | ||
url = SSH.prefix + url.substring(HTTPS.prefix.length(), index) + ":" + url.substring(index + 1); | ||
} | ||
} | ||
} | ||
case HTTPS -> { | ||
if (url.startsWith(SSH.prefix)) { | ||
int index = url.indexOf(":"); | ||
if (index > 0) { | ||
url = HTTPS.prefix + url.substring(SSH.prefix.length(), index) + "/" + url.substring(index + 1); | ||
} | ||
} | ||
} | ||
default -> throw new IllegalArgumentException("Unsupported protocol: " + this); | ||
} | ||
|
||
return new GitUrl(url, gitUrl.branch()); | ||
} | ||
|
||
private boolean isDomainWithNoConversion(String url) { | ||
|
||
for (String domain : DOMAINS_WITH_NO_CONVERSION) { | ||
// Check if it's an HTTPS URL for the domain | ||
if (url.startsWith("https://" + domain + "/")) { | ||
return true; | ||
} | ||
|
||
// Check if it's an SSH URL for the domain | ||
if (url.startsWith("git@" + domain + ":")) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
cli/src/main/java/com/devonfw/tools/ide/variable/VariableDefinitionEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.devonfw.tools.ide.variable; | ||
|
||
import java.util.function.Function; | ||
|
||
import com.devonfw.tools.ide.context.IdeContext; | ||
|
||
/** | ||
* Implementation of {@link VariableDefinition} for a variable with the {@link #getValueType() value type} as an Enum. | ||
* | ||
* @param <E> the enum type. | ||
*/ | ||
public class VariableDefinitionEnum<E extends Enum<E>> extends AbstractVariableDefinition<E> { | ||
|
||
private final Class<E> enumType; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param name the {@link #getName() variable name}. | ||
* @param enumType the class of the enum. | ||
*/ | ||
public VariableDefinitionEnum(String name, Class<E> enumType) { | ||
super(name); | ||
this.enumType = enumType; | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param name the {@link #getName() variable name}. | ||
* @param legacyName the {@link #getLegacyName() legacy name}. | ||
* @param enumType the class of the enum. | ||
*/ | ||
public VariableDefinitionEnum(String name, String legacyName, Class<E> enumType) { | ||
super(name, legacyName); | ||
this.enumType = enumType; | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param name the {@link #getName() variable name}. | ||
* @param legacyName the {@link #getLegacyName() legacy name}. | ||
* @param enumType the class of the enum. | ||
* @param defaultValueFactory the factory {@link Function} for the {@link #getDefaultValue(IdeContext) default value}. | ||
*/ | ||
public VariableDefinitionEnum(String name, String legacyName, Class<E> enumType, Function<IdeContext, E> defaultValueFactory) { | ||
super(name, legacyName, defaultValueFactory); | ||
this.enumType = enumType; | ||
} | ||
|
||
@Override | ||
public Class<E> getValueType() { | ||
return enumType; | ||
} | ||
|
||
/** | ||
* Converts a string value to the corresponding enum constant. Converts the string to upper case before matching with the enum values. | ||
* | ||
* @param value the string representation of the enum. | ||
* @param context the context for the current operation. | ||
* @return the corresponding enum constant. | ||
* @throws IllegalArgumentException if the string doesn't match any enum constant. | ||
*/ | ||
@Override | ||
public E fromString(String value, IdeContext context) { | ||
return Enum.valueOf(enumType, value.toUpperCase(java.util.Locale.ROOT)); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
cli/src/test/java/com/devonfw/tools/ide/context/GitUrlSyntaxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.devonfw.tools.ide.context; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.devonfw.tools.ide.variable.IdeVariables; | ||
|
||
|
||
/** | ||
* Test class for verifying the behavior of Git URL conversions | ||
*/ | ||
public class GitUrlSyntaxTest extends AbstractIdeContextTest { | ||
|
||
/** | ||
* Tests the reading of property PREFERRED_GIT_PROTOCOL from ide.properties is done successfully | ||
*/ | ||
@Test | ||
public void testPreferredGitProtocolIsReadAsSsh() { | ||
// Read the PREFERRED_GIT_PROTOCOL from the config file | ||
IdeTestContext context = newContext("git"); | ||
GitUrlSyntax preferredProtocol = IdeVariables.PREFERRED_GIT_PROTOCOL.get(context); | ||
|
||
// Check if the configuration value is correctly set to SSH | ||
assertThat(GitUrlSyntax.SSH).isEqualTo(preferredProtocol); | ||
} | ||
|
||
/** | ||
* Tests the conversion of a Git URL from HTTPS to SSH protocol. | ||
* <p> | ||
* Given a Git URL in HTTPS format, this test ensures that it is correctly converted to the SSH format using the {@link GitUrl#convert(GitUrlSyntax)} method. | ||
*/ | ||
@Test | ||
public void testConvertGitUrlFromHttpsToSsh() { | ||
String url = "https://testgitdomain.com/devonfw/IDEasy.git"; | ||
GitUrl gitUrl = new GitUrl(url, null); | ||
|
||
// Use the convert method with GitUrlSyntax enum | ||
GitUrl convertedGitUrl = gitUrl.convert(GitUrlSyntax.SSH); | ||
|
||
String expectedSshUrl = "[email protected]:devonfw/IDEasy.git"; | ||
assertThat(convertedGitUrl.url()).isEqualTo(expectedSshUrl); | ||
} | ||
|
||
/** | ||
* Tests the conversion of a Git URL from SSH to HTTPS protocol. | ||
* <p> | ||
* Given a Git URL in SSH format, this test ensures that it is correctly converted to the HTTPS format using the {@link GitUrl#convert(GitUrlSyntax)} method. | ||
*/ | ||
@Test | ||
public void testConvertGitUrlFromSshToHttps() { | ||
String url = "[email protected]:devonfw/IDEasy.git"; | ||
GitUrl gitUrl = new GitUrl(url, null); | ||
|
||
// Use the convert method with GitUrlSyntax enum | ||
GitUrl convertedGitUrl = gitUrl.convert(GitUrlSyntax.HTTPS); | ||
|
||
String expectedHttpsUrl = "https://testgitdomain.com/devonfw/IDEasy.git"; | ||
assertThat(convertedGitUrl.url()).isEqualTo(expectedHttpsUrl); | ||
} | ||
|
||
/** | ||
* Tests that when a Git URL is in HTTPS format and points to the github.com domain, it remains in the original format and is not converted to SSH. | ||
* <p> | ||
* This test ensures that the Git URL for github.com stays in HTTPS format, even if SSH is specified as the preferred protocol. | ||
*/ | ||
@Test | ||
public void testConvertGitUrlGitHubDomain() { | ||
String url = "https://github.com/devonfw/IDEasy.git"; | ||
GitUrl gitUrl = new GitUrl(url, null); | ||
|
||
// Attempt to convert to SSH, but it should remain in HTTPS format for github.com | ||
GitUrl convertedGitUrl = gitUrl.convert(GitUrlSyntax.SSH); | ||
|
||
// The URL should remain unchanged in HTTPS format | ||
assertThat(convertedGitUrl.url()).isEqualTo(url); | ||
} | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
cli/src/test/resources/ide-projects/git/project/settings/ide.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PREFERRED_GIT_PROTOCOL=SSH |
Empty file.