Skip to content

Commit

Permalink
change back to semver4j
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Aug 7, 2024
1 parent 51cef2d commit 629568a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
5 changes: 5 additions & 0 deletions maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-verifier</artifactId>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openmrs.maven.plugins;

import com.github.zafarkhaja.semver.ParseException;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
Expand All @@ -26,8 +25,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -36,7 +33,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

/**
* Create docker configuration for distributions.
Expand Down
6 changes: 6 additions & 0 deletions sdk-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,12 @@ public DistroProperties resolveParentArtifact(Artifact parentArtifact, Server se
return resolveParentArtifact(parentArtifact, server.getServerDirectory(), distroProperties, appShellVersion);
}

public String findLatestMatchingVersion(String dependency) {
public String findLatestMatchingVersion(String dependency, String versionRange) {
if (dependency.startsWith("omod") || dependency.startsWith("owa") || dependency.startsWith("content.") || dependency.startsWith("war.")) {
return versionHelper.getLatestReleasedVersion(new Artifact(dependency, "latest"));
} else if (dependency.startsWith("spa.frontendModule")) {
packageJson.setName(dependency.substring("spa.frontendModules.".length()));
return npmVersionHelper.getLatestReleasedVersionFromNpmRegistry(packageJson);
return npmVersionHelper.getLatestReleasedVersionFromNpmRegistry(packageJson, versionRange);
}
throw new IllegalArgumentException("Unsupported dependency type: " + dependency);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,68 @@
package org.openmrs.maven.plugins.utility;

import org.json.JSONArray;
import org.openmrs.maven.plugins.model.PackageJson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NpmVersionHelper {

public String getLatestReleasedVersionFromNpmRegistry(PackageJson packageJson) {
private static final Logger log = LoggerFactory.getLogger(NpmVersionHelper.class);

/**
* Retrieves the resolved version of an NPM package based on the supplied semver range.
* <p>
* This method runs the `npm pack --dry-run --json <package>@<version>` command to
* get the exact version of the package that satisfies the specified semver range.
*
* @param packageJson The PackageJson object containing the name of the package.
* @param versionRange The semver range to resolve the version against.
* @return The resolved version of the package that satisfies the semver range.
* @throws RuntimeException if the command fails or the resolved version cannot be determined.
*/
public String getLatestReleasedVersionFromNpmRegistry(PackageJson packageJson, String versionRange) {
try {
String packageName = packageJson.getName();
if (packageName == null || packageName.isEmpty()) {
throw new IllegalArgumentException("Package name cannot be null or empty");
JSONArray jsonArray = getJsonArray(versionRange, packageName);
if (jsonArray.isEmpty()) {
throw new RuntimeException("No versions found for the specified range: " + versionRange);
}

URL url = new URL("https://registry.npmjs.org/" + packageName.replace("/", "%2F"));
JSONObject json = getJson(url);
return json.getJSONObject("dist-tags").getString("latest");

} catch (Exception e) {
throw new RuntimeException("Error retrieving latest version from NPM ", e);
JSONObject jsonObject = jsonArray.getJSONObject(0);
return jsonObject.getString("version");
} catch (IOException | InterruptedException e) {
log.error(e.getMessage());
throw new RuntimeException("Error retrieving resolved version from NPM", e);
}
}

private static JSONObject getJson(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
private static JSONArray getJsonArray(String versionRange, String packageName) throws IOException, InterruptedException {
if (packageName == null || packageName.isEmpty()) {
throw new IllegalArgumentException("Package name cannot be null or empty");
}

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
ProcessBuilder processBuilder = new ProcessBuilder()
.command("npm", "pack", "--dry-run", "--json", packageName + "@" + versionRange)
.redirectErrorStream(true)
.inheritIO();
Process process = processBuilder.start();

// Read the command output
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder outputBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
outputBuilder.append(line);
}

conn.disconnect();
return new JSONObject(sb.toString());
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("npm pack --dry-run --json command failed with exit code " + exitCode);
}
return new JSONArray(outputBuilder.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.zafarkhaja.semver.ParseException;
import com.vdurmont.semver4j.Semver;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -352,7 +352,7 @@ public static void parseContentProperties(File userDir, DistroProperties distroP
String distroVersion = distroProperties.get(dependency);

if (distroVersion == null) {
String latestVersion = findLatestMatchingVersion(dependency);
String latestVersion = findLatestMatchingVersion(dependency, versionRange);
if (latestVersion == null) {
throw new MojoExecutionException("No matching version found for dependency " + dependency);
}
Expand All @@ -364,8 +364,8 @@ public static void parseContentProperties(File userDir, DistroProperties distroP
}
}

private static String findLatestMatchingVersion(String dependency) {
return distroHelper.findLatestMatchingVersion(dependency);
private static String findLatestMatchingVersion(String dependency, String versionRange) {
return distroHelper.findLatestMatchingVersion(dependency, versionRange);
}

/**
Expand All @@ -379,7 +379,7 @@ private static String findLatestMatchingVersion(String dependency) {
* @throws MojoExecutionException If the version does not fall within the specified range or if the range format is invalid.
*/
private static void checkVersionInRange(String contentDependencyKey, String contentDependencyVersionRange, String distroPropertyVersion, String contentPackageName) throws MojoExecutionException {
com.github.zafarkhaja.semver.Version semverVersion = com.github.zafarkhaja.semver.Version.parse(distroPropertyVersion);
Semver semverVersion = new Semver(distroPropertyVersion, Semver.SemverType.NPM);

try {
boolean inRange = semverVersion.satisfies(contentDependencyVersionRange.trim());
Expand Down

0 comments on commit 629568a

Please sign in to comment.