Skip to content

Commit 4da564a

Browse files
authored
[CI] Automatically apply spotless changes in pull request CI (#118701) (#118709)
(cherry picked from commit f900ae6)
1 parent b0fce46 commit 4da564a

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

.buildkite/pipelines/pull-request/part-1.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
steps:
22
- label: part-1
3-
command: .ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart1
3+
command: |
4+
.buildkite/scripts/spotless.sh # This doesn't have to be part of part-1, it was just a convenient place to put it
5+
.ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart1
46
timeout_in_minutes: 300
57
agents:
68
provider: gcp

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ config:
33
skip-labels: []
44
steps:
55
- label: precommit
6-
command: .ci/scripts/run-gradle.sh -Dignore.tests.seed precommit
6+
command: |
7+
.buildkite/scripts/spotless.sh
8+
.ci/scripts/run-gradle.sh -Dignore.tests.seed precommit
79
timeout_in_minutes: 300
810
agents:
911
provider: gcp

.buildkite/scripts/spotless.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 spotless"
5+
exit 0
6+
fi
7+
8+
if ! git diff --exit-code; then
9+
echo "Changes are present before running spotless, not running"
10+
git status
11+
exit 0
12+
fi
13+
14+
NEW_COMMIT_MESSAGE="[CI] Auto commit changes from spotless"
15+
PREVIOUS_COMMIT_MESSAGE="$(git log -1 --pretty=%B)"
16+
17+
echo "--- Running spotless"
18+
.ci/scripts/run-gradle.sh -Dscan.tag.NESTED spotlessApply
19+
20+
if git diff --exit-code; then
21+
echo "No changes found after running spotless. 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 spotless"
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

0 commit comments

Comments
 (0)