-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#455 validate version on provider and module upload
- Loading branch information
Showing
8 changed files
with
93 additions
and
0 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
14 changes: 14 additions & 0 deletions
14
src/main/java/core/exceptions/InvalidVersionException.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,14 @@ | ||
package core.exceptions; | ||
|
||
public class InvalidVersionException extends TapirException implements RegistryComplianceException { | ||
|
||
private static final String errorMessage = "Version %s is invalid and does not comply with the Terraform registry versioning specification"; | ||
|
||
public InvalidVersionException(String version) { | ||
super(String.format(errorMessage, version)); | ||
} | ||
|
||
public InvalidVersionException(String version, Throwable cause) { | ||
super(String.format(errorMessage, version), cause); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/core/exceptions/RegistryComplianceException.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,4 @@ | ||
package core.exceptions; | ||
|
||
public interface RegistryComplianceException { | ||
} |
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,20 @@ | ||
package core.service; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class VersionService { | ||
|
||
// see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | ||
private static final String SEMVER_PATTERN_TEMPLATE = "^%s(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"; | ||
|
||
private static final Pattern moduleSemVerPattern = Pattern.compile(String.format(SEMVER_PATTERN_TEMPLATE, "(v?)")); | ||
private static final Pattern providerSemVerPattern = Pattern.compile(String.format(SEMVER_PATTERN_TEMPLATE, "v")); | ||
|
||
public static boolean isValidModuleVersion(String version) { | ||
return moduleSemVerPattern.matcher(version).matches(); | ||
} | ||
|
||
public static boolean isValidProviderVersion(String version) { | ||
return providerSemVerPattern.matcher(version).matches(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class VersionServiceTest { | ||
|
||
@Test | ||
void isValidModuleVersion() { | ||
assertTrue(VersionService.isValidModuleVersion("1.0.0")); | ||
assertTrue(VersionService.isValidModuleVersion("v1.0.0")); | ||
assertTrue(VersionService.isValidModuleVersion("2.1.3-alpha")); | ||
assertTrue(VersionService.isValidModuleVersion("0.0.1-beta+exp.sha.5114f85")); | ||
assertTrue(VersionService.isValidModuleVersion("1.0.0+20130313144700")); | ||
assertFalse(VersionService.isValidModuleVersion("1.0")); | ||
assertFalse(VersionService.isValidModuleVersion("1.Foo.0")); | ||
} | ||
|
||
@Test | ||
void isValidProviderVersion() { | ||
assertTrue(VersionService.isValidProviderVersion("v1.0.0")); | ||
assertTrue(VersionService.isValidProviderVersion("v2.1.3-alpha")); | ||
assertTrue(VersionService.isValidProviderVersion("v0.0.1-beta+exp.sha.5114f85")); | ||
assertFalse(VersionService.isValidProviderVersion("1.0.0")); | ||
assertFalse(VersionService.isValidProviderVersion("1.0.0+20130313144700")); | ||
assertFalse(VersionService.isValidProviderVersion("v1.0")); | ||
assertFalse(VersionService.isValidProviderVersion("v1.Foo.0")); | ||
} | ||
} |