From 58ab201ade92f89ee200f85d9b39c6c397e50a99 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fabian=20Kr=C3=BCger?=
<56278322+fabapp2@users.noreply.github.com>
Date: Sat, 20 Apr 2024 00:49:04 +0200
Subject: [PATCH] Extract test helper module
---
pom.xml | 7 +++
.../pom.xml | 51 ++++++++++++++++
.../BootUpgradeWithRewritePluginExample.java | 60 +++++++++++++++++++
.../src/main/resources/application.properties | 1 +
spring-rewrite-commons-examples/pom.xml | 1 +
spring-rewrite-commons-test/pom.xml | 50 ++++++++++++++++
spring-rewrite-commons-utils/pom.xml | 29 +++++++++
.../springframework/rewrite/utils/GitHub.java | 53 ++++++++++++++++
8 files changed, 252 insertions(+)
create mode 100644 spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/pom.xml
create mode 100644 spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/java/org/springframework/rewrite/BootUpgradeWithRewritePluginExample.java
create mode 100644 spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/resources/application.properties
create mode 100644 spring-rewrite-commons-test/pom.xml
create mode 100644 spring-rewrite-commons-utils/pom.xml
create mode 100644 spring-rewrite-commons-utils/src/main/java/org/springframework/rewrite/utils/GitHub.java
diff --git a/pom.xml b/pom.xml
index 7a00c97..6633939 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,6 +18,8 @@
spring-rewrite-commons-starters
spring-rewrite-commons-gradle
spring-rewrite-commons-plugin-invoker
+ spring-rewrite-commons-utils
+ spring-rewrite-commons-test
@@ -129,6 +131,11 @@
rewrite-core
${rewrite.version}
+
+ org.springframework.rewrite
+ spring-rewrite-commons-test
+ ${project.version}
+
diff --git a/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/pom.xml b/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/pom.xml
new file mode 100644
index 0000000..e2744a9
--- /dev/null
+++ b/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/pom.xml
@@ -0,0 +1,51 @@
+
+
+ 4.0.0
+
+ org.springframework.rewrite
+ spring-rewrite-commons-examples
+ 0.1.0-SNAPSHOT
+ ../pom.xml
+
+ org.springframework.rewrite.example
+ plugin-invoker-boot-upgrade-examples
+ plugin-invoker-boot-upgrade-examples
+ plugin-invoker-boot-upgrade-examples
+
+ 17
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.rewrite
+ spring-rewrite-commons-plugin-invoker-polyglot
+ 0.1.0-SNAPSHOT
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.springframework.rewrite
+ spring-rewrite-commons-utils
+ 0.1.0-SNAPSHOT
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/java/org/springframework/rewrite/BootUpgradeWithRewritePluginExample.java b/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/java/org/springframework/rewrite/BootUpgradeWithRewritePluginExample.java
new file mode 100644
index 0000000..2deee17
--- /dev/null
+++ b/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/java/org/springframework/rewrite/BootUpgradeWithRewritePluginExample.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2021 - 2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.rewrite;
+
+import org.eclipse.jgit.api.Git;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.rewrite.plugin.polyglot.RewritePlugin;
+import org.springframework.rewrite.plugin.shared.PluginInvocationResult;
+import org.springframework.rewrite.utils.GitHub;
+
+import java.nio.file.Path;
+
+@SpringBootApplication
+public class BootUpgradeWithRewritePluginExample implements ApplicationRunner {
+
+ public static void main(String[] args) {
+ SpringApplication.run(BootUpgradeWithRewritePluginExample.class, args);
+ }
+
+ @Override
+ public void run(ApplicationArguments args) {
+ String repo = args.getNonOptionArgs().get(0);
+ String gitHash = args.getNonOptionArgs().get(1);
+ String token = args.getNonOptionArgs().get(2);
+ Path projectDir = Path.of(args.getNonOptionArgs().get(3));
+
+ Git clone = GitHub.clone(repo, projectDir, token);
+ GitHub.checkoutCommit(clone, gitHash);
+
+ PluginInvocationResult pluginInvocationResult = RewritePlugin.run()
+ .gradlePluginVersion("latest.release")
+ .recipes("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2")
+ .dependencies("org.openrewrite.recipe:rewrite-migrate-java:LATEST",
+ "org.openrewrite.recipe:rewrite-hibernate:LATEST",
+ "org.openrewrite.recipe:rewrite-java-dependencies:LATEST",
+ "org.openrewrite.recipe:rewrite-testing-frameworks:LATEST",
+ "org.openrewrite.recipe:rewrite-static-analysis:LATEST", "org.openrewrite:rewrite-kotlin:LATEST",
+ "org.openrewrite.recipe:rewrite-spring:LATEST")
+ .onDir(projectDir);
+
+ System.out.println(pluginInvocationResult.capturedOutput());
+ }
+
+}
\ No newline at end of file
diff --git a/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/resources/application.properties b/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/resources/application.properties
new file mode 100644
index 0000000..a5b211d
--- /dev/null
+++ b/spring-rewrite-commons-examples/plugin-invoker-boot-upgrade-examples/src/main/resources/application.properties
@@ -0,0 +1 @@
+spring.application.name=plugin-invoker-boot-upgrade-examples
diff --git a/spring-rewrite-commons-examples/pom.xml b/spring-rewrite-commons-examples/pom.xml
index db3dddb..7ac566a 100644
--- a/spring-rewrite-commons-examples/pom.xml
+++ b/spring-rewrite-commons-examples/pom.xml
@@ -20,6 +20,7 @@
boot-3-upgrade-atomic-example
boot-3-upgrade-iterative-example
list-applicable-recipes-example
+ plugin-invoker-boot-upgrade-examples
diff --git a/spring-rewrite-commons-test/pom.xml b/spring-rewrite-commons-test/pom.xml
new file mode 100644
index 0000000..fb6d52e
--- /dev/null
+++ b/spring-rewrite-commons-test/pom.xml
@@ -0,0 +1,50 @@
+
+
+ 4.0.0
+
+
+ org.springframework.rewrite
+ spring-rewrite-commons
+ 0.1.0-SNAPSHOT
+ ../pom.xml
+
+
+ spring-rewrite-commons-test
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+ org.springframework.rewrite
+ spring-rewrite-commons-utils
+ 0.1.0-SNAPSHOT
+
+
+ commons-io
+ commons-io
+ 2.11.0
+
+
+
+ org.openrewrite
+ rewrite-core
+ ${rewrite.version}
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+
+
+ org.junit-pioneer
+ junit-pioneer
+ ${junit-pioneer.version}
+ compile
+
+
+
\ No newline at end of file
diff --git a/spring-rewrite-commons-utils/pom.xml b/spring-rewrite-commons-utils/pom.xml
new file mode 100644
index 0000000..08406f0
--- /dev/null
+++ b/spring-rewrite-commons-utils/pom.xml
@@ -0,0 +1,29 @@
+
+
+ 4.0.0
+
+
+ org.springframework.rewrite
+ spring-rewrite-commons
+ 0.1.0-SNAPSHOT
+ ../pom.xml
+
+
+ spring-rewrite-commons-utils
+
+ Utils
+
+
+
+ org.springframework
+ spring-context
+
+
+ org.eclipse.jgit
+ org.eclipse.jgit
+ 5.13.0.202109080827-r
+
+
+
+
\ No newline at end of file
diff --git a/spring-rewrite-commons-utils/src/main/java/org/springframework/rewrite/utils/GitHub.java b/spring-rewrite-commons-utils/src/main/java/org/springframework/rewrite/utils/GitHub.java
new file mode 100644
index 0000000..6783fc4
--- /dev/null
+++ b/spring-rewrite-commons-utils/src/main/java/org/springframework/rewrite/utils/GitHub.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2021 - 2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.rewrite.utils;
+
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
+
+import java.nio.file.Path;
+
+/**
+ * @author Fabian Krüger
+ */
+public class GitHub {
+
+ public static Git clone(String repositoryUrl, Path targetDir, String token) {
+ try {
+ Git git = Git.cloneRepository()
+ .setCredentialsProvider(new UsernamePasswordCredentialsProvider(token, ""))
+ .setURI(repositoryUrl)
+ .setDirectory(targetDir.toFile())
+ .call();
+ return git;
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void checkoutCommit(Git git, String startingGitHash) {
+ try {
+ Ref ref = git.checkout().setName(startingGitHash).call();
+ }
+ catch (GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}