From 84fe7831ebced2ce84e166dedaa27da026f9cb98 Mon Sep 17 00:00:00 2001 From: Brian Seeders Date: Mon, 16 Dec 2024 11:31:05 -0500 Subject: [PATCH] [CI] Automatically apply spotless changes in pull request CI (#118701) (#118711) (cherry picked from commit f900ae61bbc3ba9670a832206f4a6552013f67c4) --- .buildkite/pipelines/pull-request/part-1.yml | 4 +- .../pipelines/pull-request/precommit.yml | 4 +- .buildkite/scripts/spotless.sh | 44 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100755 .buildkite/scripts/spotless.sh diff --git a/.buildkite/pipelines/pull-request/part-1.yml b/.buildkite/pipelines/pull-request/part-1.yml index 3d467c6c41e43..7a09e2a162ff8 100644 --- a/.buildkite/pipelines/pull-request/part-1.yml +++ b/.buildkite/pipelines/pull-request/part-1.yml @@ -1,6 +1,8 @@ steps: - label: part-1 - command: .ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart1 + command: | + .buildkite/scripts/spotless.sh # This doesn't have to be part of part-1, it was just a convenient place to put it + .ci/scripts/run-gradle.sh -Dignore.tests.seed checkPart1 timeout_in_minutes: 300 agents: provider: gcp diff --git a/.buildkite/pipelines/pull-request/precommit.yml b/.buildkite/pipelines/pull-request/precommit.yml index f6548dfeed9b2..1763758932581 100644 --- a/.buildkite/pipelines/pull-request/precommit.yml +++ b/.buildkite/pipelines/pull-request/precommit.yml @@ -3,7 +3,9 @@ config: skip-labels: [] steps: - label: precommit - command: .ci/scripts/run-gradle.sh -Dignore.tests.seed precommit + command: | + .buildkite/scripts/spotless.sh + .ci/scripts/run-gradle.sh -Dignore.tests.seed precommit timeout_in_minutes: 300 agents: provider: gcp diff --git a/.buildkite/scripts/spotless.sh b/.buildkite/scripts/spotless.sh new file mode 100755 index 0000000000000..b9e6094edb2c7 --- /dev/null +++ b/.buildkite/scripts/spotless.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +if [[ -z "${BUILDKITE_PULL_REQUEST:-}" ]]; then + echo "Not a pull request, skipping spotless" + exit 0 +fi + +if ! git diff --exit-code; then + echo "Changes are present before running spotless, not running" + git status + exit 0 +fi + +NEW_COMMIT_MESSAGE="[CI] Auto commit changes from spotless" +PREVIOUS_COMMIT_MESSAGE="$(git log -1 --pretty=%B)" + +echo "--- Running spotless" +.ci/scripts/run-gradle.sh -Dscan.tag.NESTED spotlessApply + +if git diff --exit-code; then + echo "No changes found after running spotless. Don't need to auto commit." + exit 0 +fi + +if [[ "$NEW_COMMIT_MESSAGE" == "$PREVIOUS_COMMIT_MESSAGE" ]]; then + echo "Changes found after running spotless" + 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 'infra-root+elasticsearchmachine@elastic.co' + +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