Skip to content

Commit 43ed65a

Browse files
dustinbyrneeli-r-ph
authored andcommitted
ci: reuse Python SDK upgrade PRs unless queued (#1021)
* ci: reuse Python SDK upgrade PRs unless queued * ci: protect reused upgrade PRs from stale and queued writes (cherry picked from commit 945bbbc)
1 parent be748d9 commit 43ed65a

2 files changed

Lines changed: 515 additions & 12 deletions

File tree

‎.github/workflows/posthog-upgrade.yml‎

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ on:
1717
permissions:
1818
contents: read
1919

20+
concurrency:
21+
group: posthog-upgrade-${{ github.event.inputs.package_name }}
22+
cancel-in-progress: false
23+
2024
jobs:
2125
posthog-upgrade:
2226
name: Upgrade PostHog package
@@ -81,16 +85,117 @@ jobs:
8185
fi
8286
done
8387
84-
- name: Generate branch name
85-
id: generate-branch-name
88+
- name: Generate pull request details
89+
id: generate-pr-details
8690
shell: bash
8791
env:
92+
GH_TOKEN: ${{ steps.upgrader.outputs.token }}
8893
PACKAGE_NAME: ${{ github.event.inputs.package_name }}
8994
PACKAGE_VERSION: ${{ github.event.inputs.package_version }}
9095
run: |
91-
echo "branch_name=${PACKAGE_NAME}-${PACKAGE_VERSION}" >> "$GITHUB_OUTPUT"
96+
TITLE_PREFIX="chore(deps): update ${PACKAGE_NAME} to "
97+
98+
skip_update() {
99+
echo "::notice::$1"
100+
echo "skip=true" >> "$GITHUB_OUTPUT"
101+
exit 0
102+
}
103+
104+
# Serialize writers and reject stale reruns using Python package ordering.
105+
check_version() {
106+
local current_title="$1" current_version older
107+
if [[ "$current_title" != "$TITLE_PREFIX"* ]]; then
108+
echo "::error::Cannot determine the destination PR's package version." >&2
109+
exit 1
110+
fi
111+
current_version="${current_title#"$TITLE_PREFIX"}"
112+
older=$(uv run --no-project --with packaging==25.0 python - "$PACKAGE_VERSION" "$current_version" <<'PY'
113+
import sys
114+
from packaging.version import Version
115+
116+
print("true" if Version(sys.argv[1]) < Version(sys.argv[2]) else "false")
117+
PY
118+
) || {
119+
echo "::error::Cannot compare package versions; leaving remote branches unchanged." >&2
120+
exit 1
121+
}
122+
if [ "$older" = "true" ]; then
123+
skip_update "Version $PACKAGE_VERSION is older than PR version $current_version; leaving it unchanged."
124+
fi
125+
}
126+
127+
queue_status() {
128+
local pr="$1" status
129+
if status=$(gh api "repos/PostHog/posthog/issues/${pr}/comments?per_page=100" \
130+
--paginate --slurp \
131+
--jq '[.[][] | select(.user.login == "trunk-io[bot]") | .body] | last // ""'); then
132+
printf '%s' "$status" \
133+
| grep -oE "Running tests on this pull request|waiting to start tests" | head -n 1 || true
134+
else
135+
echo "::warning::Could not read the queue status of PR #${pr}; treating it as queued." >&2
136+
echo "unknown"
137+
fi
138+
}
139+
140+
EXISTING=$(gh pr list \
141+
--repo PostHog/posthog \
142+
--state open \
143+
--search "\"${TITLE_PREFIX}\" in:title sort:created-desc" \
144+
--limit 100 \
145+
--json title,number,headRefName,headRepositoryOwner \
146+
--jq ".[] | select(.title | startswith(\"${TITLE_PREFIX}\")) | select(.headRepositoryOwner.login == \"PostHog\") | [.number, .headRefName, .title] | @tsv" | head -n 1)
147+
IFS=$'\t' read -r EXISTING_PR EXISTING_BRANCH EXISTING_TITLE <<< "$EXISTING"
148+
149+
# Updating a queued PR's branch restarts the merge queue's checks.
150+
# Leave queued PRs alone, including when their status cannot be read.
151+
QUEUED=""
152+
if [ -n "$EXISTING_PR" ]; then
153+
check_version "$EXISTING_TITLE"
154+
QUEUED=$(queue_status "$EXISTING_PR")
155+
fi
156+
157+
if [ -n "$EXISTING_BRANCH" ] && [ -z "$QUEUED" ]; then
158+
BRANCH_NAME="$EXISTING_BRANCH"
159+
elif [ -n "$QUEUED" ]; then
160+
echo "::notice::PR #${EXISTING_PR} is in the merge queue; checking a separate branch instead."
161+
BRANCH_NAME="${PACKAGE_NAME}-upgrade-${PACKAGE_VERSION}"
162+
# A rerun can collide with an already queued version-specific PR.
163+
if ! DESTINATIONS=$(gh pr list \
164+
--repo PostHog/posthog \
165+
--state open \
166+
--head "$BRANCH_NAME" \
167+
--limit 100 \
168+
--json title,number,headRefName,isCrossRepository \
169+
--jq '.[] | select(.isCrossRepository == false) | [.number, .headRefName, .title] | @tsv'); then
170+
skip_update "Could not check the fallback branch for an existing PR; leaving it unchanged."
171+
fi
172+
while IFS=$'\t' read -r DESTINATION_PR DESTINATION_BRANCH DESTINATION_TITLE; do
173+
[ -n "$DESTINATION_PR" ] || continue
174+
[ "$DESTINATION_BRANCH" = "$BRANCH_NAME" ] || continue
175+
check_version "$DESTINATION_TITLE"
176+
if [ -n "$(queue_status "$DESTINATION_PR")" ]; then
177+
skip_update "Fallback PR #${DESTINATION_PR} is queued or its status is unknown; leaving it unchanged."
178+
fi
179+
done <<< "$DESTINATIONS"
180+
else
181+
BRANCH_NAME="${PACKAGE_NAME}-upgrade"
182+
fi
183+
184+
echo "skip=false" >> "$GITHUB_OUTPUT"
185+
echo "branch_name=$BRANCH_NAME" >> "$GITHUB_OUTPUT"
186+
echo "title=${TITLE_PREFIX}${PACKAGE_VERSION}" >> "$GITHUB_OUTPUT"
187+
{
188+
echo 'body<<EOF'
189+
echo '## Changes'
190+
echo
191+
echo "PostHog Python SDK version $PACKAGE_VERSION has been released. This updates the \`posthoganalytics\` and \`posthog\` dependency declarations to the same SDK version."
192+
echo
193+
echo '[GitHub releases](https://github.com/PostHog/posthog-python/releases) • [`posthoganalytics` on PyPI](https://pypi.org/project/posthoganalytics/#history) • [`posthog` on PyPI](https://pypi.org/project/posthog/#history)'
194+
echo 'EOF'
195+
} >> "$GITHUB_OUTPUT"
92196
93197
- name: Create main repo pull request
198+
if: steps.generate-pr-details.outputs.skip == 'false'
94199
id: main-repo-pr
95200
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
96201
env:
@@ -99,18 +204,14 @@ jobs:
99204
token: ${{ steps.upgrader.outputs.token }}
100205
# PostHog/posthog requires signed commits, so commit through the GitHub API.
101206
sign-commits: true
102-
commit-message: "chore(deps): update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}"
103-
branch: ${{ steps.generate-branch-name.outputs.branch_name }}
207+
commit-message: ${{ steps.generate-pr-details.outputs.title }}
208+
branch: ${{ steps.generate-pr-details.outputs.branch_name }}
104209
delete-branch: true
105-
title: "chore(deps): update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}"
106-
body: |
107-
## Changes
108-
109-
PostHog Python SDK version ${{ github.event.inputs.package_version }} has been released. This updates the `posthoganalytics` and `posthog` dependency declarations to the same SDK version.
110-
111-
[GitHub releases](https://github.com/PostHog/posthog-python/releases) • [`posthoganalytics` on PyPI](https://pypi.org/project/posthoganalytics/#history) • [`posthog` on PyPI](https://pypi.org/project/posthog/#history)
210+
title: ${{ steps.generate-pr-details.outputs.title }}
211+
body: ${{ steps.generate-pr-details.outputs.body }}
112212

113213
- name: Output pull request result
214+
if: steps.generate-pr-details.outputs.skip == 'false'
114215
shell: bash
115216
env:
116217
PACKAGE_NAME: ${{ github.event.inputs.package_name }}
@@ -119,7 +220,21 @@ jobs:
119220
run: |
120221
echo "PostHog pull request for $PACKAGE_NAME version $PACKAGE_VERSION ready: $PR_URL"
121222
223+
- name: Update pull request metadata
224+
if: steps.generate-pr-details.outputs.skip == 'false'
225+
env:
226+
GH_TOKEN: ${{ steps.upgrader.outputs.token }}
227+
PR_BODY: ${{ steps.generate-pr-details.outputs.body }}
228+
PR_NUMBER: ${{ steps.main-repo-pr.outputs.pull-request-number }}
229+
PR_TITLE: ${{ steps.generate-pr-details.outputs.title }}
230+
run: |
231+
gh pr edit "$PR_NUMBER" \
232+
--repo PostHog/posthog \
233+
--title "$PR_TITLE" \
234+
--body "$PR_BODY"
235+
122236
- name: Assign reviewers
237+
if: steps.generate-pr-details.outputs.skip == 'false'
123238
env:
124239
GH_TOKEN: ${{ steps.upgrader.outputs.token }}
125240
PR_NUMBER: ${{ steps.main-repo-pr.outputs.pull-request-number }}

0 commit comments

Comments
 (0)