diff --git a/.github/workflows/serverless-patch.sh b/.github/workflows/serverless-patch.sh new file mode 100755 index 000000000..cd746c97c --- /dev/null +++ b/.github/workflows/serverless-patch.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +set -exuo pipefail + +merge_commit_sha=$(jq -r '.pull_request.merge_commit_sha' "$GITHUB_EVENT_PATH") +pull_request_id=$(jq -r '.pull_request.number' "$GITHUB_EVENT_PATH") + +# generate patch file +cd "$GITHUB_WORKSPACE/elasticsearch-js" +git format-patch -1 --stdout "$merge_commit_sha" > /tmp/patch.diff + +# apply patch file +cd "$GITHUB_WORKSPACE/elasticsearch-serverless-js" +git checkout -b "apply-patch-$pull_request_id" +git apply -C1 --recount --reject /tmp/patch.diff || exit 0 + +comment="Patch applied from elastic/elasticsearch-js#$pull_request_id" + +# check for rejected patches +tick='\`' # just trying to satisfy shellcheck here +has_rejects='' +for f in ./**/*.rej; do + has_rejects=' --draft' + comment="$comment + +## Rejected patch $tick$f$tick must be resolved: + +$tick$tick$tick +$(cat "$f") +$tick$tick$tick +" +done + +# open a PR +gh pr create \ + -t "Apply PR changes from elastic/elasticsearch-js#$pull_request_id" \ + --body "$comment" \ + "$has_rejects" diff --git a/.github/workflows/serverless-patch.yml b/.github/workflows/serverless-patch.yml index 8f204bbea..9acda04b6 100644 --- a/.github/workflows/serverless-patch.yml +++ b/.github/workflows/serverless-patch.yml @@ -36,17 +36,5 @@ jobs: repository: elastic/elasticsearch-serverless-js ref: main path: elasticsearch-serverless-js - - name: Generate patch file - run: | - cd $GITHUB_WORKSPACE/elasticsearch-js - git format-patch -1 --stdout ${{ github.event.pull_request.merge_commit_sha }} > /tmp/patch.diff - - name: Apply patch file - run: | - cd $GITHUB_WORKSPACE/elasticsearch-serverless-js - git checkout -b apply-patch-${{ github.event.pull_request.id }} - git apply -C1 --recount --reject /tmp/patch.diff || exit 0 - comment='Patch applied from elastic/elasticsearch-js#${{ github.event.pull_request.id }}' - for f in $(find . -name '*.rej'); do - comment="$comment\n\n## Rejected patch \`$f`\:\n\`\`\`\n$(cat $f)\n\`\`\`" - done - gh pr create -t "Apply PR changes from elastic/elasticsearch-js#${{ github.event.pull_request.id }}" --body "$comment" + - name: Apply patch from stack to serverless + run: ./.github/serverless-patch.sh