-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add rewrite support for RemoveUnusedImports
#131611
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| steps: | ||
| - label: part-7 | ||
| command: | | ||
| .buildkite/scripts/rewrite.sh | ||
| .ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7 | ||
| timeout_in_minutes: 300 | ||
| agents: | ||
| provider: gcp | ||
| image: family/elasticsearch-ubuntu-2404 | ||
| machineType: custom-32-98304 | ||
| buildDirectory: /dev/shm/bk |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| steps: | ||
| - label: part-7 | ||
| command: | | ||
| .buildkite/scripts/rewrite.sh | ||
| .ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7 | ||
| timeout_in_minutes: 300 | ||
| agents: | ||
| provider: gcp | ||
| image: family/elasticsearch-ubuntu-2404 | ||
| machineType: custom-32-98304 | ||
| buildDirectory: /dev/shm/bk |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| steps: | ||
| - label: part-7 | ||
| command: | | ||
| .buildkite/scripts/rewrite.sh | ||
| .ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7 | ||
| timeout_in_minutes: 300 | ||
| agents: | ||
| provider: gcp | ||
| image: family/elasticsearch-ubuntu-2404 | ||
| machineType: custom-32-98304 | ||
| buildDirectory: /dev/shm/bk |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| steps: | ||
| - label: part-7 | ||
| command: | | ||
| .buildkite/scripts/rewrite.sh | ||
| .ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7 | ||
| timeout_in_minutes: 300 | ||
| agents: | ||
| provider: gcp | ||
| image: family/elasticsearch-ubuntu-2404 | ||
| machineType: custom-32-98304 | ||
| buildDirectory: /dev/shm/bk |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/bin/bash | ||
|
|
||
| if [[ -z "${BUILDKITE_PULL_REQUEST:-}" ]]; then | ||
| echo "Not a pull request, skipping rewrite" | ||
| exit 0 | ||
| fi | ||
|
|
||
| if ! git diff --exit-code; then | ||
| echo "Changes are present before running rewrite, not running." | ||
| git status | ||
| exit 0 | ||
| fi | ||
|
|
||
| NEW_COMMIT_MESSAGE="[CI] Auto commit changes from rewrite" | ||
| PREVIOUS_COMMIT_MESSAGE="$(git log -1 --pretty=%B)" | ||
|
|
||
| echo "--- Running rewrite" | ||
| .ci/scripts/run-gradle.sh -Dscan.tag.NESTED rewriteRun | ||
|
|
||
| if git diff --exit-code; then | ||
| echo "No changes found after running rewrite. Don't need to auto commit." | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [[ "$NEW_COMMIT_MESSAGE" == "$PREVIOUS_COMMIT_MESSAGE" ]]; then | ||
| echo "Changes found after running rewrite." | ||
| echo "CI already attempted to commit these changes, but the file(s) seem to have changed again." | ||
| echo "Please review and fix manually." | ||
| exit 1 | ||
| fi | ||
|
|
||
| git config --global user.name elasticsearchmachine | ||
| git config --global user.email '[email protected]' | ||
|
|
||
| gh pr checkout "${BUILDKITE_PULL_REQUEST}" | ||
| git add -u . | ||
| git commit -m "$NEW_COMMIT_MESSAGE" | ||
| git push | ||
|
|
||
| # After the git push, the new commit will trigger a new build within a few seconds and this build should get cancelled | ||
| # So, let's just sleep to give the build time to cancel itself without an error | ||
| # If it doesn't get cancelled for some reason, then exit with an error, because we don't want this build to be green (we just don't want it to generate an error either) | ||
| sleep 300 | ||
| exit 1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,16 +238,16 @@ the [Spotless Gradle] plugin. All new projects are automatically formatted, | |
| while existing projects are gradually being opted-in. The formatting check | ||
| is run automatically via the `precommit` task, but it can be run explicitly with: | ||
|
|
||
| ./gradlew spotlessJavaCheck | ||
| - `./gradlew spotlessJavaCheck rewriteDryRun` | ||
|
|
||
| It is usually more useful, and just as fast, to just reformat the project. You | ||
| can do this with: | ||
|
|
||
| ./gradlew spotlessApply | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| - `./gradlew spotlessApply rewriteRun` | ||
|
|
||
| These tasks can also be run for specific subprojects, e.g. | ||
|
|
||
| ./gradlew server:spotlessJavaCheck | ||
| - `./gradlew server:spotlessJavaCheck server:rewriteDryRun` | ||
|
|
||
| Please follow these formatting guidelines: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.gradle.internal.conventions.precommit; | ||
|
|
||
| import org.gradle.api.Plugin; | ||
| import org.gradle.api.Project; | ||
| import org.openrewrite.gradle.RewriteExtension; | ||
|
|
||
| import org.openrewrite.gradle.RewritePlugin; | ||
|
|
||
| import static java.lang.Boolean.parseBoolean; | ||
| import static java.lang.System.getenv; | ||
|
|
||
| /** | ||
| * This plugin configures formatting for Java source using Spotless | ||
| * for Gradle. Since the act of formatting existing source can interfere | ||
| * with developers' workflows, we don't automatically format all code | ||
| * (yet). Instead, we maintain a list of projects that are excluded from | ||
| * formatting, until we reach a point where we can comfortably format them | ||
| * in one go without too much disruption. | ||
| * | ||
| * <p>Any new sub-projects must not be added to the exclusions list! | ||
| * | ||
| * <p>To perform a reformat, run: | ||
| * | ||
| * <pre> ./gradlew spotlessApply</pre> | ||
| * | ||
| * <p>To check the current format, run: | ||
| * | ||
| * <pre> ./gradlew spotlessJavaCheck</pre> | ||
| * | ||
| * <p>This is also carried out by the `precommit` task. | ||
| * | ||
| * <p>See also the <a href="https://github.com/diffplug/spotless/tree/master/plugin-gradle" | ||
| * >Spotless project page</a>. | ||
| */ | ||
| public class RewritePrecommitPlugin implements Plugin<Project> { | ||
|
|
||
| private static final boolean IS_NON_CI = parseBoolean(getenv("isCI")) == false; | ||
| private static final boolean SKIP_FORMATTING = parseBoolean(getenv("skipFormatting")); | ||
| private static final boolean CODE_CLEANUP = parseBoolean(getenv("codeCleanup")); | ||
|
|
||
| @SuppressWarnings({ "checkstyle:DescendantToken", "checkstyle:LineLength" }) | ||
| @Override | ||
| public void apply(Project project) { | ||
| project.getPluginManager().withPlugin("java-base", javaBasePlugin -> { | ||
| project.getPlugins().apply(PrecommitTaskPlugin.class); | ||
| project.getPlugins().apply(RewritePlugin.class); | ||
| project.getRepositories().mavenCentral(); // spotless & rewrite need mavenCentral | ||
| project.getTasks().named("precommit").configure(precommitTask -> precommitTask.dependsOn( "rewriteDryRun")); | ||
| project.getTasks().named("check").configure(check -> check.dependsOn("rewriteDryRun")); | ||
| if (!SKIP_FORMATTING && IS_NON_CI && CODE_CLEANUP) { | ||
| project.getTasks().named("assemble").configure(check -> check.dependsOn("rewriteRun")); | ||
| } | ||
| rewrite(project); | ||
| }); | ||
| } | ||
|
|
||
| private static void rewrite(Project project) { | ||
| RewriteExtension rewriteExtension = project.getExtensions().getByType(RewriteExtension.class); | ||
| rewriteExtension.activeRecipe( | ||
| "org.openrewrite.java.RemoveUnusedImports" | ||
| //"org.openrewrite.staticanalysis.RemoveUnusedLocalVariables", | ||
| //"org.openrewrite.staticanalysis.RemoveUnusedPrivateFields", | ||
| //"org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods" | ||
| ); | ||
| rewriteExtension.exclusion("**OpenSearchTestCaseTests.java"); | ||
| rewriteExtension.setExportDatatables(true); | ||
| rewriteExtension.setFailOnDryRunResults(true); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ plugins { | |
| id 'elasticsearch.eclipse' | ||
| id 'elasticsearch.versions' | ||
| id 'elasticsearch.formatting' | ||
| //id 'elasticsearch.rewrite' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not working? |
||
| } | ||
|
|
||
| group = "org.elasticsearch.gradle" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| org.gradle.welcome=never | ||
| org.gradle.warning.mode=none | ||
| org.gradle.parallel=true | ||
| # https://github.com/openrewrite/rewrite-gradle-plugin/issues/212 | ||
| #org.gradle.workers.max=2 | ||
| # We need to declare --add-exports to make spotless working seamlessly with jdk16 | ||
| org.gradle.jvmargs=-XX:+HeapDumpOnOutOfMemoryError -Xss2m --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens java.base/java.time=ALL-UNNAMED | ||
|
|
||
|
|
@@ -16,6 +18,8 @@ org.gradle.java.installations.auto-detect=false | |
|
|
||
| # log some dependency verification info to console | ||
| org.gradle.dependency.verification.console=verbose | ||
| # fixme undo lenient | ||
| org.gradle.dependency.verification=lenient | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does
not include dependencies ? |
||
|
|
||
| # allow user to specify toolchain via the RUNTIME_JAVA_HOME environment variable | ||
| org.gradle.java.installations.fromEnv=RUNTIME_JAVA_HOME | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.