Skip to content

Commit 7adfb92

Browse files
Copilotpelikhan
andauthored
fix: sync-actions creates a PR instead of pushing directly to main (#7)
* Initial plan * fix: sync-actions creates a PR instead of pushing to main, polls until merged Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 9f9492d commit 7adfb92

1 file changed

Lines changed: 63 additions & 5 deletions

File tree

.github/workflows/sync-actions.yml

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ on:
88
required: false
99
default: 'latest'
1010
type: string
11+
outputs:
12+
pr_number:
13+
description: 'The number of the pull request created for the sync (empty if no changes).'
14+
value: ${{ jobs.sync.outputs.pr_number }}
1115
workflow_dispatch:
1216
inputs:
1317
ref:
@@ -22,6 +26,9 @@ jobs:
2226
runs-on: ubuntu-latest
2327
permissions:
2428
contents: write
29+
pull-requests: write
30+
outputs:
31+
pr_number: ${{ steps.create-pr.outputs.pr_number }}
2532

2633
steps:
2734
- name: Log workflow context
@@ -236,8 +243,8 @@ jobs:
236243
done
237244
echo "::endgroup::"
238245
239-
- name: Commit and push changes to main
240-
id: commit
246+
- name: Create branch, commit, and open pull request
247+
id: create-pr
241248
run: |
242249
echo "::group::Git Commit"
243250
cd gh-aw-actions
@@ -253,21 +260,72 @@ jobs:
253260
if git diff --staged --quiet; then
254261
echo "No changes detected — nothing to commit."
255262
echo "changed=false" >> "$GITHUB_OUTPUT"
263+
echo "pr_number=" >> "$GITHUB_OUTPUT"
256264
else
257265
echo "Changes staged for commit:"
258266
git diff --staged --stat
259267
echo ""
268+
BRANCH="sync/gh-aw-${{ steps.resolve-ref.outputs.resolved_ref }}"
269+
git checkout -b "$BRANCH"
260270
COMMIT_MSG="chore: sync actions from gh-aw@${{ steps.resolve-ref.outputs.resolved_ref }}"
261271
git commit -m "$COMMIT_MSG"
262-
git push origin main
272+
git push origin "$BRANCH"
273+
gh pr create \
274+
--title "$COMMIT_MSG" \
275+
--body "Automated sync of actions from [gh-aw](https://github.com/github/gh-aw) at \`${{ steps.resolve-ref.outputs.resolved_ref }}\`." \
276+
--base main \
277+
--head "$BRANCH"
278+
PR_NUMBER=$(gh pr view --head "$BRANCH" --json number --jq '.number')
279+
PR_URL="https://github.com/${{ github.repository }}/pull/$PR_NUMBER"
263280
echo ""
264-
echo "Changes committed and pushed to main. ✓"
281+
echo "Pull request #$PR_NUMBER created: $PR_URL ✓"
265282
echo "changed=true" >> "$GITHUB_OUTPUT"
283+
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
266284
fi
267285
echo "::endgroup::"
286+
env:
287+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
288+
289+
- name: Poll pull request until merged
290+
if: steps.create-pr.outputs.changed == 'true'
291+
run: |
292+
echo "::group::Polling PR #${{ steps.create-pr.outputs.pr_number }}"
293+
PR_NUMBER="${{ steps.create-pr.outputs.pr_number }}"
294+
TIMEOUT=1200
295+
INTERVAL=30
296+
ELAPSED=0
297+
298+
while [[ $ELAPSED -lt $TIMEOUT ]]; do
299+
if ! STATE=$(gh pr view "$PR_NUMBER" --repo "${{ github.repository }}" --json state --jq '.state' 2>&1); then
300+
echo "::warning::Failed to query PR state (elapsed: ${ELAPSED}s): $STATE"
301+
else
302+
echo "PR #$PR_NUMBER state: $STATE (elapsed: ${ELAPSED}s / ${TIMEOUT}s)"
303+
304+
if [[ "$STATE" == "MERGED" ]]; then
305+
echo "Pull request #$PR_NUMBER has been merged. ✓"
306+
echo "::endgroup::"
307+
exit 0
308+
fi
309+
310+
if [[ "$STATE" == "CLOSED" ]]; then
311+
echo "::error::Pull request #$PR_NUMBER was closed without merging."
312+
echo "::endgroup::"
313+
exit 1
314+
fi
315+
fi
316+
317+
sleep $INTERVAL
318+
ELAPSED=$((ELAPSED + INTERVAL))
319+
done
320+
321+
echo "::error::Timed out after ${TIMEOUT}s waiting for pull request #$PR_NUMBER to be merged."
322+
echo "::endgroup::"
323+
exit 1
324+
env:
325+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
268326

269327
- name: Create tag
270-
if: steps.resolve-ref.outputs.should_create_tag == 'true' && steps.commit.outputs.changed == 'true'
328+
if: steps.resolve-ref.outputs.should_create_tag == 'true' && steps.create-pr.outputs.changed == 'true'
271329
run: |
272330
echo "::group::Creating Tag"
273331
cd gh-aw-actions

0 commit comments

Comments
 (0)