Skip to content

Commit fb606f5

Browse files
authored
ci: Reuse existing snapshot update PRs (spiceai#10640)
1 parent 0beb321 commit fb606f5

1 file changed

Lines changed: 52 additions & 19 deletions

File tree

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: 'Add benchmark snapshots to PR'
2-
description: 'Commits and pushes any updated benchmark snapshots to the current branch.'
1+
name: 'Add snapshots to PR'
2+
description: 'Commits and pushes updated snapshots to an automation PR.'
33
inputs:
44
token:
55
description: 'GitHub token with permissions to push to the repository'
@@ -21,8 +21,8 @@ runs:
2121
if ! command -v gh &> /dev/null; then
2222
(type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \
2323
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
24-
&& out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
25-
&& cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
24+
&& out=$(mktemp) && wget -nv -O"$out" https://cli.github.com/packages/githubcli-archive-keyring.gpg \
25+
&& cat "$out" | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
2626
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
2727
&& sudo mkdir -p -m 755 /etc/apt/sources.list.d \
2828
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
@@ -31,37 +31,70 @@ runs:
3131
else
3232
echo "GH CLI is already installed"
3333
fi
34-
- name: Upload benchmark snapshots to branch
34+
- name: Upload snapshots to branch
3535
shell: bash
3636
run: |
37-
BRANCH_ID=${{ github.run_id }}
38-
BRANCH_NAME="snapshot-update/${BRANCH_ID}"
37+
set -euo pipefail
38+
39+
EXISTING_PR_JSON="$(mktemp)"
40+
gh pr list --state open --base trunk --limit 100 --json number,title,headRefName,author,body > "${EXISTING_PR_JSON}"
41+
EXISTING_PR_NUMBER="$(jq -r --arg title "${PR_TITLE}" '
42+
[.[]
43+
| select(.title == $title)
44+
| select(
45+
.author.login == "github-actions[bot]"
46+
or .author.login == "spiceaibot"
47+
or ((.body // "") | contains("Automated snapshot update from workflow run"))
48+
or ((.body // "") | contains("<!-- spice-snapshot-update -->"))
49+
)]
50+
| sort_by(.number)
51+
| last
52+
| .number // empty
53+
' "${EXISTING_PR_JSON}")"
54+
EXISTING_PR_BRANCH="$(jq -r --argjson number "${EXISTING_PR_NUMBER:-0}" '
55+
.[] | select(.number == $number) | .headRefName
56+
' "${EXISTING_PR_JSON}")"
57+
58+
if [[ -n "${EXISTING_PR_BRANCH}" ]]; then
59+
BRANCH_NAME="${EXISTING_PR_BRANCH}"
60+
echo "Found existing snapshot update PR #${EXISTING_PR_NUMBER}; updating ${BRANCH_NAME}"
61+
else
62+
RAW_BRANCH_NAME="${GITHUB_WORKFLOW:-snapshot-update}-${GITHUB_JOB:-job}-${PR_TITLE}"
63+
BRANCH_SLUG="$(printf '%s' "${RAW_BRANCH_NAME}" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9._-]+/-/g; s/^-+//; s/-+$//; s/-+/-/g' | cut -c1-80)"
64+
BRANCH_NAME="snapshot-update/${BRANCH_SLUG:-${GITHUB_RUN_ID}}"
65+
fi
3966
4067
git config --global user.name 'Spice Snapshot Update Bot'
4168
git config --global user.email 'spiceaibot@spice.ai'
42-
git checkout -b ${BRANCH_NAME}
69+
git checkout -b "${BRANCH_NAME}"
4370
git add '*.snap'
4471
if git diff --cached --quiet; then
4572
echo "No changes to commit"
4673
else
47-
git commit -m "${{ inputs.title }}"
48-
if git ls-remote --exit-code --heads origin ${BRANCH_NAME}; then
49-
git pull --rebase origin ${BRANCH_NAME}
74+
git commit -m "${PR_TITLE}"
75+
if git ls-remote --exit-code --heads origin "${BRANCH_NAME}"; then
76+
if [[ "$(git rev-parse --is-shallow-repository)" == "true" ]]; then
77+
git fetch --unshallow origin
78+
fi
79+
git fetch origin "${BRANCH_NAME}"
80+
git rebase -X theirs "origin/${BRANCH_NAME}"
5081
fi
51-
git push origin ${BRANCH_NAME}
82+
git push origin "HEAD:${BRANCH_NAME}"
5283
53-
PR_URL="$(gh pr list --head "${BRANCH_NAME}" --state open --json url --jq .[].url)"
54-
if [[ -n "${PR_URL}" ]]; then
55-
echo "PR already exists -> ${PR_URL}"
84+
if [[ -n "${EXISTING_PR_NUMBER}" ]]; then
85+
PR_URL="$(gh pr view "${EXISTING_PR_NUMBER}" --json url --jq .url)"
86+
echo "Updated existing PR -> ${PR_URL}"
5687
exit 0
5788
else
58-
BODY="${{ inputs.description }}"
89+
BODY="${PR_DESCRIPTION}"
5990
if [[ -z "${BODY}" ]]; then
60-
BODY="Automated snapshot update from workflow run [#${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})."
91+
BODY="$(printf 'Automated snapshot update from workflow run [#%s](%s/%s/actions/runs/%s).\n\n<!-- spice-snapshot-update -->' "${GITHUB_RUN_ID}" "${GITHUB_SERVER_URL}" "${GITHUB_REPOSITORY}" "${GITHUB_RUN_ID}")"
6192
fi
62-
gh pr create --title "${{ inputs.title }}" --body "${BODY}" --base trunk --head ${BRANCH_NAME} --label kind/bug
63-
gh pr merge --squash --auto ${BRANCH_NAME} || echo "Auto-merge not available for this branch; PR created and requires manual merge."
93+
gh pr create --title "${PR_TITLE}" --body "${BODY}" --base trunk --head "${BRANCH_NAME}" --label kind/bug
94+
gh pr merge --squash --auto "${BRANCH_NAME}" || echo "Auto-merge not available for this branch; PR created and requires manual merge."
6495
fi
6596
fi
6697
env:
6798
GH_TOKEN: ${{ inputs.token }}
99+
PR_TITLE: ${{ inputs.title }}
100+
PR_DESCRIPTION: ${{ inputs.description }}

0 commit comments

Comments
 (0)