Skip to content

Commit 3703673

Browse files
committed
gha: handle workflow re-runs
1 parent 379ba4c commit 3703673

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

.github/workflows/lockfiles.yaml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ jobs:
127127
headRepository,\
128128
headRefName,\
129129
baseRefName"
130+
NEW_PR_REF: "regen-lockfiles-${{ github.run_id }}"
130131
run: |
131-
if [[ "${{ inputs.pr }}" == new ]]; then
132+
NEW_PR_NUMBER="$(gh pr list --json number --head "${NEW_PR_REF}" --state open)"
133+
pr_list_rc = $?
134+
if [[ "${{ inputs.pr }}" == new ]] && [ ${pr_list_rc} -gt 0 ]; then
135+
CHECKOUT_REF="${GITHUB_REF}"
132136
echo "Planning new Pull Request metadata ..."
133137
PR=$(
134138
yq -e -p yaml . -o json -I 0 \
@@ -149,24 +153,38 @@ jobs:
149153
headRepository:
150154
id: "${GITHUB_REPOSITORY_ID}"
151155
name: "${GITHUB_REPOSITORY#*/}"
152-
headRefName: "regen-lockfiles-${GITHUB_RUN_ID}"
156+
headRefName: "${NEW_PR_REF}"
153157
baseRefName: "${GITHUB_REF_NAME}"
154158
HEREYAML
155159
)
156-
CHECKOUT_REF="${GITHUB_REF}"
160+
elif [[ "${{ inputs.pr }}" == new ]] && [ ${pr_list_rc} -eq 0 ]; then
161+
CHECKOUT_REF="${GITHUB_REF}" # will be force pushed
162+
echo "Planning new Pull Request metadata (PR #${NEW_PR_NUMBER} - workflow re-run ${GITHUB_RUN_ATTEMPT})..."
163+
PR=$(gh pr view "${NEW_PR_NUMBER}" --json "${PR_FIELDS}")
164+
pr_search_rc=$?
165+
if [ ${pr_search_rc} -gt 0 ]; then
166+
echo "Error looking up PR from prior workflow run."
167+
echo "JSON=${PR}"
168+
exit 2
169+
fi
170+
echo "Found Pull Request #${NEW_PR_NUMBER} by @$(jq -r .author.login <<< "${PR}")"
171+
echo "URL: $(jq -r .url <<< "${PR}")"
157172
else
158173
CHECKOUT_REF="refs/pull/${{ inputs.pr }}/merge"
159174
echo "Searching for Pull Request #${{ inputs.pr }} ..."
160175
PR=$(gh pr view "${{ inputs.pr }}" --json "${PR_FIELDS}")
161176
pr_search_rc=$?
162177
if [ ${pr_search_rc} -gt 0 ]; then
163178
echo "Pull Request #${{ inputs.pr }} not found!"
179+
echo "JSON=${PR}"
164180
exit 2
165181
elif (jq -e .closed <<< "${PR}" >/dev/null); then
166182
echo "Pull Request #${{ inputs.pr }} is closed!"
183+
echo "JSON=${PR}"
167184
exit 3
168185
elif ! (jq -e .maintainerCanModify <<< "${PR}" >/dev/null); then
169186
echo "Pull Request #${{ inputs.pr }} does not allow maintainer modification!"
187+
echo "JSON=${PR}"
170188
exit 4
171189
fi
172190
echo "Found Pull Request #${{ inputs.pr }} by @$(jq -r .author.login <<< "${PR}")"
@@ -416,13 +434,16 @@ jobs:
416434
submodules: 'true'
417435
ref: ${{ needs.pr.outputs.CHECKOUT_REF }}
418436

419-
- name: Create branch for new PR
420-
if: inputs.pr == 'new'
437+
- name: Configure git author, committer
421438
run: |
422439
git config --local author.name "${{ env.BOT_ST2_NAME }}"
423440
git config --local author.email "${{ env.BOT_ST2_EMAIL }}"
424441
git config --local committer.name "${{ env.BOT_GHA_NAME }}"
425442
git config --local committer.email "${{ env.BOT_GHA_EMAIL }}"
443+
444+
- name: Create branch for new PR
445+
if: inputs.pr == 'new'
446+
run: |
426447
git checkout -b "${{ needs.pr.outputs.PR_REF }}" FETCH_HEAD
427448
428449
- name: Download lockfiles and lockfile diff files
@@ -500,7 +521,7 @@ jobs:
500521
pants run scripts/lockfiles_to_reqs.py
501522
git add *requirements.txt st2*/requirements.txt contrib/runners/*/requirements.txt
502523
503-
- name: Commit and push
524+
- name: Commit and ${{ inputs.pr == 'new' && 'force-' || '' }}push
504525
if: env.CHANGED == 'true'
505526
# git push --force is for workflow re-runs, but only for new PRs.
506527
run: |
@@ -509,7 +530,7 @@ jobs:
509530
-u origin "${{ needs.pr.outputs.PR_REF }}"
510531
511532
- name: Create new PR
512-
if: inputs.pr == 'new' && env.CHANGED == 'true'
533+
if: inputs.pr == 'new' && fromJSON(needs.pr.outputs.JSON).number == 'new' && env.CHANGED == 'true'
513534
env:
514535
GH_TOKEN: ${{ github.token }}
515536
run: >
@@ -527,6 +548,17 @@ jobs:
527548
# so, setting reviewer to a team would mean managing a PAT or similar.
528549
# --reviewer "${{ github.repository_owner }}/Maintainers"
529550

551+
- name: Update new PR (workflow re-run ${{ github.run_attempt }})
552+
if: inputs.pr == 'new' && fromJSON(needs.pr.outputs.JSON).number != 'new' && env.CHANGED == 'true'
553+
env:
554+
GH_TOKEN: ${{ github.token }}
555+
run: >
556+
(
557+
echo '## New PR Update'; echo;
558+
gh pr edit "${{ fromJSON(needs.pr.outputs.JSON).number }}"
559+
--body-file "${PR_COMMENT}"
560+
) | tee -a "${GITHUB_STEP_SUMMARY}"
561+
530562
- name: Update or Add Comment on existing PR
531563
if: inputs.pr != 'new'
532564
env:

0 commit comments

Comments
 (0)