Skip to content

Commit 8e9674a

Browse files
author
Vincent Potucek
committed
Add rewrite support
1 parent 61575f0 commit 8e9674a

File tree

17 files changed

+215
-12
lines changed

17 files changed

+215
-12
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
steps:
2+
- label: part-7
3+
command: |
4+
.buildkite/scripts/rewrite.sh
5+
.ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7
6+
timeout_in_minutes: 300
7+
agents:
8+
provider: gcp
9+
image: family/elasticsearch-ubuntu-2404
10+
machineType: custom-32-98304
11+
buildDirectory: /dev/shm/bk
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
steps:
2+
- label: part-7
3+
command: |
4+
.buildkite/scripts/rewrite.sh
5+
.ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7
6+
timeout_in_minutes: 300
7+
agents:
8+
provider: gcp
9+
image: family/elasticsearch-ubuntu-2404
10+
machineType: custom-32-98304
11+
buildDirectory: /dev/shm/bk
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
steps:
2+
- label: part-7
3+
command: |
4+
.buildkite/scripts/rewrite.sh
5+
.ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7
6+
timeout_in_minutes: 300
7+
agents:
8+
provider: gcp
9+
image: family/elasticsearch-ubuntu-2404
10+
machineType: custom-32-98304
11+
buildDirectory: /dev/shm/bk
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
steps:
2+
- label: part-7
3+
command: |
4+
.buildkite/scripts/rewrite.sh
5+
.ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart7
6+
timeout_in_minutes: 300
7+
agents:
8+
provider: gcp
9+
image: family/elasticsearch-ubuntu-2404
10+
machineType: custom-32-98304
11+
buildDirectory: /dev/shm/bk

.buildkite/pipelines/pull-request/precommit.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ steps:
66
command: |
77
.buildkite/scripts/spotless.sh
88
.ci/scripts/run-gradle.sh -Dignore.tests.seed precommit
9-
timeout_in_minutes: 300
9+
.buildkite/scripts/rewrite.sh
10+
.ci/scripts/run-gradle.sh -Dignore.tests.seed precommit
11+
timeout_in_minutes: 400
1012
agents:
1113
provider: gcp
1214
image: family/elasticsearch-ubuntu-2404

.buildkite/scripts/rewrite.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
if [[ -z "${BUILDKITE_PULL_REQUEST:-}" ]]; then
4+
echo "Not a pull request, skipping rewrite"
5+
exit 0
6+
fi
7+
8+
if ! git diff --exit-code; then
9+
echo "Changes are present before running rewrite, not running."
10+
git status
11+
exit 0
12+
fi
13+
14+
NEW_COMMIT_MESSAGE="[CI] Auto commit changes from rewrite"
15+
PREVIOUS_COMMIT_MESSAGE="$(git log -1 --pretty=%B)"
16+
17+
echo "--- Running rewrite"
18+
.ci/scripts/run-gradle.sh -Dscan.tag.NESTED rewriteRun
19+
20+
if git diff --exit-code; then
21+
echo "No changes found after running rewrite. Don't need to auto commit."
22+
exit 0
23+
fi
24+
25+
if [[ "$NEW_COMMIT_MESSAGE" == "$PREVIOUS_COMMIT_MESSAGE" ]]; then
26+
echo "Changes found after running rewrite."
27+
echo "CI already attempted to commit these changes, but the file(s) seem to have changed again."
28+
echo "Please review and fix manually."
29+
exit 1
30+
fi
31+
32+
git config --global user.name elasticsearchmachine
33+
git config --global user.email '[email protected]'
34+
35+
gh pr checkout "${BUILDKITE_PULL_REQUEST}"
36+
git add -u .
37+
git commit -m "$NEW_COMMIT_MESSAGE"
38+
git push
39+
40+
# After the git push, the new commit will trigger a new build within a few seconds and this build should get cancelled
41+
# So, let's just sleep to give the build time to cancel itself without an error
42+
# 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)
43+
sleep 300
44+
exit 1

BUILDING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ For updated or newly added dependencies you need to add an entry to this verific
8080

8181
In case of updating a dependency, ensure to remove the unused entry of the outdated dependency manually from the `verification-metadata.xml` file.
8282

83-
You can also automate the generation of this entry by running your build using the `--write-verification-metadata` commandline option:
84-
```
85-
./gradlew --write-verification-metadata sha256 precommit
86-
```
83+
- Quick apply via:
84+
- `./gradlew --write-verification-metadata sha256 help`
85+
- Automate the generation of this entry by running your build using the `--write-verification-metadata` commandline option:
86+
- `./gradlew --write-verification-metadata sha256 precommit`
8787

8888
The `--write-verification-metadata` Gradle option is generally able to resolve reachable configurations,
8989
but we use detached configurations for a certain set of plugins and tasks. Therefore, please ensure you run this option with a task that
@@ -92,7 +92,7 @@ uses the changed dependencies. In most cases, `precommit` or `check` are good ca
9292
We prefer sha256 checksums as md5 and sha1 are not considered safe anymore these days. The generated entry
9393
will have the `origin` attribute been set to `Generated by Gradle`.
9494

95-
> [!Tip]
95+
> [!Tip]
9696
> A manual confirmation of the Gradle generated checksums is currently not mandatory.
9797
> If you want to add a level of verification you can manually confirm the checksum (e.g. by looking it up on the website of the library)
9898
> Please replace the content of the `origin` attribute by `official site` in that case.
@@ -229,7 +229,7 @@ In addition to snapshot builds JitPack supports building Pull Requests. Simply u
229229
3. Run the Gradle build as needed. Keep in mind the initial resolution might take a bit longer as this needs to be built
230230
by JitPack in the background before we can resolve the adhoc built dependency.
231231

232-
> [!Note]
232+
> [!Note]
233233
> You should only use that approach locally or on a developer branch for production dependencies as we do
234234
not want to ship unreleased libraries into our releases.
235235

@@ -261,7 +261,7 @@ allprojects {
261261
```
262262
4. Run the Gradle build as needed with `--write-verification-metadata` to ensure the Gradle dependency verification does not fail on your custom dependency.
263263

264-
> [!Note]
264+
> [!Note]
265265
> As Gradle prefers to use modules whose descriptor has been created from real meta-data rather than being generated,
266266
flat directory repositories cannot be used to override artifacts with real meta-data from other repositories declared in the build.
267267
> For example, if Gradle finds only `jmxri-1.2.1.jar` in a flat directory repository, but `jmxri-1.2.1.pom` in another repository

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,16 @@ the [Spotless Gradle] plugin. All new projects are automatically formatted,
238238
while existing projects are gradually being opted-in. The formatting check
239239
is run automatically via the `precommit` task, but it can be run explicitly with:
240240

241-
./gradlew spotlessJavaCheck
241+
- `./gradlew spotlessJavaCheck rewriteDryRun`
242242

243243
It is usually more useful, and just as fast, to just reformat the project. You
244244
can do this with:
245245

246-
./gradlew spotlessApply
246+
- `./gradlew spotlessApply rewriteRun`
247247

248248
These tasks can also be run for specific subprojects, e.g.
249249

250-
./gradlew server:spotlessJavaCheck
250+
- `./gradlew server:spotlessJavaCheck server:rewriteDryRun`
251251

252252
Please follow these formatting guidelines:
253253

build-conventions/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ gradlePlugin {
6363
id = 'elasticsearch.formatting'
6464
implementationClass = 'org.elasticsearch.gradle.internal.conventions.precommit.FormattingPrecommitPlugin'
6565
}
66+
rewrite {
67+
id = 'elasticsearch.rewrite'
68+
implementationClass = 'org.elasticsearch.gradle.internal.conventions.precommit.RewritePrecommitPlugin'
69+
}
6670
}
6771
}
6872

@@ -76,6 +80,7 @@ dependencies {
7680
api buildLibs.shadow.plugin
7781
api buildLibs.apache.rat
7882
api buildLibs.nmcp
83+
api buildLibs.rewrite.plugin
7984
compileOnly buildLibs.checkstyle
8085
constraints {
8186
api("org.eclipse.platform:org.eclipse.osgi:3.18.300") {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.conventions.precommit;
11+
12+
import org.gradle.api.Plugin;
13+
import org.gradle.api.Project;
14+
import org.openrewrite.gradle.RewriteExtension;
15+
16+
import org.openrewrite.gradle.RewritePlugin;
17+
18+
import static java.lang.Boolean.parseBoolean;
19+
import static java.lang.System.getenv;
20+
21+
/**
22+
* This plugin configures formatting for Java source using Spotless
23+
* for Gradle. Since the act of formatting existing source can interfere
24+
* with developers' workflows, we don't automatically format all code
25+
* (yet). Instead, we maintain a list of projects that are excluded from
26+
* formatting, until we reach a point where we can comfortably format them
27+
* in one go without too much disruption.
28+
*
29+
* <p>Any new sub-projects must not be added to the exclusions list!
30+
*
31+
* <p>To perform a reformat, run:
32+
*
33+
* <pre> ./gradlew spotlessApply</pre>
34+
*
35+
* <p>To check the current format, run:
36+
*
37+
* <pre> ./gradlew spotlessJavaCheck</pre>
38+
*
39+
* <p>This is also carried out by the `precommit` task.
40+
*
41+
* <p>See also the <a href="https://github.com/diffplug/spotless/tree/master/plugin-gradle"
42+
* >Spotless project page</a>.
43+
*/
44+
public class RewritePrecommitPlugin implements Plugin<Project> {
45+
46+
private static final boolean IS_NON_CI = parseBoolean(getenv("isCI")) == false;
47+
private static final boolean SKIP_FORMATTING = parseBoolean(getenv("skipFormatting"));
48+
private static final boolean CODE_CLEANUP = parseBoolean(getenv("codeCleanup"));
49+
50+
@SuppressWarnings({ "checkstyle:DescendantToken", "checkstyle:LineLength" })
51+
@Override
52+
public void apply(Project project) {
53+
project.getPluginManager().withPlugin("java-base", javaBasePlugin -> {
54+
project.getPlugins().apply(PrecommitTaskPlugin.class);
55+
project.getPlugins().apply(RewritePlugin.class);
56+
project.getRepositories().mavenCentral(); // spotless & rewrite need mavenCentral
57+
project.getTasks().named("precommit").configure(precommitTask -> precommitTask.dependsOn( "rewriteDryRun"));
58+
project.getTasks().named("check").configure(check -> check.dependsOn("rewriteDryRun"));
59+
if (!SKIP_FORMATTING && IS_NON_CI && CODE_CLEANUP) {
60+
project.getTasks().named("assemble").configure(check -> check.dependsOn("rewriteRun"));
61+
}
62+
rewrite(project);
63+
});
64+
}
65+
66+
private static void rewrite(Project project) {
67+
RewriteExtension rewriteExtension = project.getExtensions().getByType(RewriteExtension.class);
68+
rewriteExtension.activeRecipe(
69+
"org.openrewrite.java.RemoveUnusedImports"
70+
//"org.openrewrite.staticanalysis.RemoveUnusedLocalVariables",
71+
//"org.openrewrite.staticanalysis.RemoveUnusedPrivateFields",
72+
//"org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods"
73+
);
74+
rewriteExtension.exclusion("**OpenSearchTestCaseTests.java");
75+
rewriteExtension.setExportDatatables(true);
76+
rewriteExtension.setFailOnDryRunResults(true);
77+
}
78+
}

0 commit comments

Comments
 (0)