Skip to content
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

IPROTO-383 Allow proto-schema-compatibility-maven-plugin to compare a… #323

Merged
merged 2 commits into from
Oct 1, 2024
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
5 changes: 5 additions & 0 deletions maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -34,14 +36,20 @@
requiresDependencyResolution = ResolutionScope.COMPILE,
threadSafe = true)
public class ProtoCompatibilityMojo extends AbstractMojo {
@Parameter(defaultValue = "false")
private boolean commitProtoLock;

@Parameter(defaultValue = "${basedir}")
private String protoLockRoot;

@Parameter(defaultValue = "${project.build.directory}/classes/proto", readonly = true)
private String protoSourceRoot;

@Parameter(defaultValue = "false")
private boolean commitProtoLock;
@Parameter
private String remoteLockFiles;

@Parameter(defaultValue = "${session}")
private MavenSession session;

@Parameter(defaultValue = "false")
private boolean skip;
Expand All @@ -60,23 +68,13 @@ public void execute() throws MojoExecutionException {

Path lockFile = Paths.get(protoLockRoot, "proto.lock");
boolean lockFileExists = Files.exists(lockFile);
if (!commitProtoLock && !lockFileExists) {
getLog().info("Ignoring protolock check as there isn't an existing proto.lock file and commitProtoLock=false.");
if (!commitProtoLock && !lockFileExists && !remoteCheck()) {
getLog().info("Ignoring protolock check as there isn't an existing proto.lock file, commitProtoLock=false and no remoteLockFiles are specified.");
return;
}
Path protoRoot = Paths.get(protoSourceRoot);
List<Path> protoFiles;
try (Stream<Path> stream = Files.walk(protoRoot)) {
protoFiles = stream.filter(p -> p.getFileName().toString().endsWith(".proto")).toList();
}
FileDescriptorSource fds = new FileDescriptorSource();
for (Path p : protoFiles) {
fds.addProtoFile(protoRoot.relativize(p).toString(), p.toFile());
}
ProtostreamProtoParser parser = new ProtostreamProtoParser(Configuration.builder().build());
Map<String, FileDescriptor> descriptors = parser.parse(fds);
ProtoLock protoNew = new ProtoLock(descriptors.values());
ProtoLock protoNew = protoLockFromDir(Paths.get(protoSourceRoot));
if (!lockFileExists) {
checkRemoteCompatibility(protoNew);
try (OutputStream os = Files.newOutputStream(lockFile)) {
protoNew.writeLockFile(os);
}
Expand All @@ -87,7 +85,8 @@ public void execute() throws MojoExecutionException {
protoOld = ProtoLock.readLockFile(is);
}
protoOld.checkCompatibility(protoNew, true);
getLog().info("Backwards compatibility check passed.");
getLog().info(String.format("Backwards compatibility check against local file '%s' passed.", lockFile));
checkRemoteCompatibility(protoNew);

if (commitProtoLock) {
try (OutputStream os = Files.newOutputStream(lockFile)) {
Expand All @@ -100,4 +99,41 @@ public void execute() throws MojoExecutionException {
throw new MojoExecutionException("An error occurred while running protolock", e);
}
}

private ProtoLock protoLockFromDir(Path protoRoot) throws IOException {
List<Path> protoFiles;
try (Stream<Path> stream = Files.walk(protoRoot)) {
protoFiles = stream.filter(p -> p.getFileName().toString().endsWith(".proto")).toList();
}
FileDescriptorSource fds = new FileDescriptorSource();
for (Path p : protoFiles) {
fds.addProtoFile(protoRoot.relativize(p).toString(), p.toFile());
}
ProtostreamProtoParser parser = new ProtostreamProtoParser(Configuration.builder().build());
Map<String, FileDescriptor> descriptors = parser.parse(fds);
return new ProtoLock(descriptors.values());
}

private void checkRemoteCompatibility(ProtoLock currentState) throws IOException {
if (!remoteCheck())
return;

if (session.isOffline()) {
getLog().info("Skipping backwards compatibility check against remote files as maven is in Offline mode");
return;
}

for (String file : remoteLockFiles.split(",")) {
getLog().info(String.format("Checking backwards compatibility check against remote file '%s'", file));
try (InputStream is = new URL(file).openStream()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In offline mode we may want to allow URLs with the file protocol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have protoSourceRoot and protoLockRoot for defining checks against local files.

ProtoLock remoteLockFile = ProtoLock.readLockFile(is);
currentState.checkCompatibility(remoteLockFile, true);
}
getLog().info(String.format("Backwards compatibility check against remote file '%s' passed", file));
}
}

private boolean remoteCheck() {
return remoteLockFiles != null && !remoteLockFiles.isEmpty();
}
}
6 changes: 6 additions & 0 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${version.plugin.api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down