Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion .github/workflows/sync-main-to-edge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ on:

permissions:
contents: write
pull-requests: write

jobs:
plan:
Expand Down Expand Up @@ -127,9 +128,14 @@ jobs:
persist-credentials: false
fetch-depth: 0

# The aormsby action fast-forwards upstream -> target. It fails when the
# branches have diverged or when branch protection rejects the push.
# continue-on-error lets us fall back to opening a PR in that case instead
# of failing the whole job.
- name: Sync upstream changes
if: steps.check.outputs.exists == 'true'
id: sync
continue-on-error: true
uses: aormsby/Fork-Sync-With-Upstream-action@83d78e8c4bf524e79b091cf257deba4dee9e60df # v3.4.3
with:
target_sync_branch: ${{ matrix.target }}
Expand All @@ -138,10 +144,35 @@ jobs:
upstream_sync_repo: ${{ github.repository }}
test_mode: ${{ inputs.test_mode }}

# When the direct push could not happen (diverged history or branch
# protection), open a PR upstream -> target instead. Idempotent: skips if
# a PR is already open for the same head/base, and tolerates "no diff".
- name: Open PR if direct sync could not push
if: steps.check.outputs.exists == 'true' && steps.sync.outcome == 'failure'
env:
UPSTREAM: ${{ matrix.upstream }}
TARGET: ${{ matrix.target }}
REPO: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
existing=$(gh pr list --repo "$REPO" --base "$TARGET" --head "$UPSTREAM" \
--state open --json number -q '.[0].number' || true)
if [ -n "$existing" ]; then
echo "PR #$existing already open for $UPSTREAM -> $TARGET; nothing to do."
exit 0
fi
gh pr create --repo "$REPO" --base "$TARGET" --head "$UPSTREAM" \

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a re-usable action that's commonly used in the nsheaps org that you should reuse here

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/nsheaps/.github/blob/main/.github/workflows/sync-repo-settings.yaml#L62-L122

Something like this maybe should be put in it's own re-usable action

--title "sync: $UPSTREAM -> $TARGET" \
--body "Automated sync could not fast-forward \`$TARGET\` from \`$UPSTREAM\` (diverged history or branch protection). Merging via PR instead." \

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body should have a link back to this workflow captured the same way other nsheaps ones do.

|| echo "No PR created (possibly no diff between $UPSTREAM and $TARGET)."

- name: Report
if: steps.check.outputs.exists == 'true'
run: |
if [ "${{ steps.sync.outputs.has_new_commits }}" = "true" ]; then
if [ "${{ steps.sync.outcome }}" = "failure" ]; then
echo "Direct sync could not push ${{ matrix.upstream }} -> ${{ matrix.target }}; opened/ensured a PR instead."
elif [ "${{ steps.sync.outputs.has_new_commits }}" = "true" ]; then
echo "Synced new commits from ${{ matrix.upstream }} into ${{ matrix.target }}."
else
echo "${{ matrix.target }} already up-to-date with ${{ matrix.upstream }}."
Expand Down
Loading