Skip to content

Commit

Permalink
Add UpdateMavenWrapper recipe including checksum verification (open…
Browse files Browse the repository at this point in the history
…rewrite#3392)

* Add `UpdateMavenWrapper` recipe including checksum verification

Fixes openrewritegh-1565 and openrewritegh-2996

* Add missing license headers

* Ensure cmd files are using crlf line endings
  • Loading branch information
shanman190 authored Jul 3, 2023
1 parent ef24f5a commit bdc9dc4
Show file tree
Hide file tree
Showing 11 changed files with 1,619 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# These are explicitly windows files and should use crlf
*.bat text eol=crlf
*.cmd text eol=crlf

# These files are text and should be normalized (Convert crlf => lf)
*.bash text eol=lf
Expand Down
15 changes: 11 additions & 4 deletions rewrite-core/src/main/java/org/openrewrite/Checksum.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.Value;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.ipc.http.HttpSender;
import org.openrewrite.remote.Remote;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -99,14 +100,20 @@ public static SourceFile checksum(SourceFile sourceFile, @Nullable String algori

try {
MessageDigest md = MessageDigest.getInstance(algorithm);
try (InputStream is = Files.newInputStream(sourceFile.getSourcePath());
DigestInputStream dis = new DigestInputStream(is, md)) {
InputStream is;
if (sourceFile instanceof Remote) {
is = ((Remote) sourceFile).getInputStream(new InMemoryExecutionContext());
} else {
is = Files.newInputStream(sourceFile.getSourcePath());
}

try (DigestInputStream dis = new DigestInputStream(is, md)) {
//noinspection StatementWithEmptyBody
while (dis.read() != -1) {
// read decorated stream to EOF
// read stream to EOF
}
return sourceFile.withChecksum(new Checksum(algorithm, md.digest()));
}
return sourceFile.withChecksum(new Checksum(algorithm, md.digest()));
} catch (NoSuchAlgorithmException | IOException e) {
throw new IllegalArgumentException(e);
}
Expand Down
11 changes: 8 additions & 3 deletions rewrite-core/src/main/java/org/openrewrite/remote/Remote.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,24 @@ public Builder fileAttributes(FileAttributes fileAttributes) {
return this;
}

public Builder checksum(Checksum checksum) {
this.checksum = checksum;
return this;
}

public RemoteFile build() {
return new RemoteFile(id, sourcePath, markers, uri, charset, charsetBomMarked, fileAttributes, description);
return new RemoteFile(id, sourcePath, markers, uri, charset, charsetBomMarked, fileAttributes, description, checksum);
}

public RemoteArchive build(Path path) {
return new RemoteArchive(id, sourcePath, markers, uri, charset, charsetBomMarked, fileAttributes, description,
Arrays.asList(path.toString().replace("/", "\\/").replace(".", "\\.")
.split("!")));
.split("!")), checksum);
}

public RemoteArchive build(String... paths) {
return new RemoteArchive(id, sourcePath, markers, uri, charset, charsetBomMarked, fileAttributes, description,
Arrays.asList(paths));
Arrays.asList(paths), checksum);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.Value;
import lombok.With;
import org.intellij.lang.annotations.Language;
import org.openrewrite.Checksum;
import org.openrewrite.ExecutionContext;
import org.openrewrite.FileAttributes;
import org.openrewrite.HttpSenderExecutionContextView;
Expand Down Expand Up @@ -81,6 +82,9 @@ public class RemoteArchive implements Remote {
*/
List<String> paths;

@Nullable
Checksum checksum;

@Override
public InputStream getInputStream(ExecutionContext ctx) {
HttpSender httpSender = HttpSenderExecutionContextView.view(ctx).getHttpSender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.Value;
import lombok.With;
import org.intellij.lang.annotations.Language;
import org.openrewrite.Checksum;
import org.openrewrite.ExecutionContext;
import org.openrewrite.FileAttributes;
import org.openrewrite.HttpSenderExecutionContextView;
Expand Down Expand Up @@ -57,6 +58,9 @@ public class RemoteFile implements Remote {
@Language("markdown")
String description;

@Nullable
Checksum checksum;

@Override
public InputStream getInputStream(ExecutionContext ctx) {
HttpSender httpSender = HttpSenderExecutionContextView.view(ctx).getHttpSender();
Expand Down
2 changes: 1 addition & 1 deletion rewrite-maven/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

compileOnly("org.rocksdb:rocksdbjni:latest.release")
compileOnly(project(":rewrite-yaml"))
compileOnly(project(":rewrite-properties"))
implementation(project(":rewrite-properties"))

implementation("io.micrometer:micrometer-core:1.9.+")

Expand Down
Loading

0 comments on commit bdc9dc4

Please sign in to comment.