DistroHopper/.github/workflows/sync-upstream.yml
2026-12-04 13:24:18 +01:00

197 lines
9.1 KiB
YAML

name: Sync upstream quickemu
# Keep the vendored quickemu files in sync with upstream quickemu:
# quickemu quickget chunkcheck quickreport
# Upstream changes are merged on top of DistroHopper's local patches and are
# opened as a pull request (branch ci/sync-upstream) for review. When that PR
# is already open its branch is resumed, so manual conflict resolutions and
# review commits are never overwritten. Conflicts keep their <<<<<<< markers
# in the PR for manual resolution and are never committed to the default
# branch, even when direct_commit is requested (that opt-in only runs when the
# sync is conflict-free and no review PR is in flight). Local patches are
# preserved via the baseline snapshots in upstream/baseline
# (see scripts/sync-upstream.sh).
on:
schedule:
- cron: '0 */4 * * *' # every 4 hours
workflow_dispatch:
inputs:
direct_commit:
description: 'Commit directly to the default branch instead of opening a pull request'
type: boolean
default: false
permissions:
contents: write
pull-requests: write
concurrency:
group: sync-upstream
cancel-in-progress: false
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
# If a ci/sync-upstream PR is already open, resume its branch so the sync
# runs on top of any commits made during review (e.g. resolved conflicts)
# instead of resetting and force-pushing over them.
- name: Resume open upstream sync PR branch
id: review
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
PR_BRANCH="ci/sync-upstream"
# The direct-commit target and PR base is the repository's default
# branch, not whichever branch this run was dispatched from.
DEFAULT_BRANCH="$(gh repo view "${GITHUB_REPOSITORY}" --json defaultBranchRef \
--jq '.defaultBranchRef.name' 2>/dev/null || true)"
# Fall back to the remote HEAD without relying on a local
# refs/remotes/origin/HEAD symbolic ref (actions/checkout does not
# create one), which would make `git symbolic-ref` exit 128.
if [ -z "${DEFAULT_BRANCH}" ]; then
DEFAULT_BRANCH="$(git ls-remote --symref origin HEAD 2>/dev/null \
| sed -n 's|^ref: refs/heads/\(.*\)\tHEAD|\1|p' || true)"
fi
if [ -z "${DEFAULT_BRANCH}" ]; then
echo "::error::Could not determine the repository's default branch."
exit 1
fi
echo "DEFAULT_BRANCH=${DEFAULT_BRANCH}" >> "$GITHUB_ENV"
# Only an in-repository PR may be resumed. A bare branch name can
# also match fork PRs, so scope --head to the repo owner and drop any
# cross-repository result; fail the job if the lookup errors instead
# of treating the failure as "no open PR".
PR_JSON="$(gh pr list --repo "${GITHUB_REPOSITORY}" --head "${GITHUB_REPOSITORY_OWNER}:${PR_BRANCH}" --state open \
--json number,headRefOid,isCrossRepository --jq '[.[] | select(.isCrossRepository == false)] | .[0] // empty')"
if [ -n "${PR_JSON}" ]; then
PR_NUMBER="$(printf '%s' "${PR_JSON}" | jq -r '.number')"
git fetch -q origin "${PR_BRANCH}"
git checkout -q -B "${PR_BRANCH}" "origin/${PR_BRANCH}"
echo "FOUND_PR=1" >> "$GITHUB_ENV"
echo "PR_NUMBER=${PR_NUMBER}" >> "$GITHUB_ENV"
echo "Resumed ${PR_BRANCH} (PR #${PR_NUMBER}); review commits are preserved."
else
# No review in flight: operate on the repository's default branch.
git fetch -q origin "${DEFAULT_BRANCH}"
git checkout -q -B "${DEFAULT_BRANCH}" "origin/${DEFAULT_BRANCH}"
echo "FOUND_PR=0" >> "$GITHUB_ENV"
echo "PR_NUMBER=" >> "$GITHUB_ENV"
echo "No open in-repo ${PR_BRANCH} PR; syncing against ${DEFAULT_BRANCH}."
fi
- name: Sync vendored files from upstream
id: sync
env:
GH_TOKEN: ${{ github.token }}
UPSTREAM_STATE_FILE: ${{ runner.temp }}/upstream-sync-state
run: |
chmod a+x scripts/sync-upstream.sh
./scripts/sync-upstream.sh
- name: Open a pull request (or commit directly)
env:
GH_TOKEN: ${{ github.token }}
STATE_FILE: ${{ runner.temp }}/upstream-sync-state
DIRECT_COMMIT: ${{ inputs.direct_commit || 'false' }}
run: |
set -euo pipefail
# shellcheck disable=SC1090
source "${STATE_FILE}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
BRANCH="${DEFAULT_BRANCH:-${GITHUB_REF_NAME:-$(git ls-remote --symref origin HEAD 2>/dev/null | sed -n 's|^ref: refs/heads/\(.*\)\tHEAD|\1|p' || true)}}"
PREV_SHA="$(git show "HEAD:upstream/stamp.json" 2>/dev/null | jq -r '.sha // empty' || true)"
FOUND_PR="${FOUND_PR:-0}"
PR_NUMBER="${PR_NUMBER:-}"
CONFLICTS="${CONFLICTS:-0}"
PR_BRANCH="ci/sync-upstream"
stage_changes() {
for f in quickemu quickget chunkcheck quickreport; do
[ -e "${f}" ] && git add "${f}"
done
git add upstream/
}
# Nothing to sync at all: discard the stamp-only bookkeeping.
if [ "${CHANGED:-0}" = "0" ] && [ "${FIRST_RUN:-0}" != "1" ]; then
git checkout -- upstream/ >/dev/null 2>&1 || true
git clean -fdq upstream/ >/dev/null 2>&1 || true
echo "No upstream changes to apply."
exit 0
fi
if [ "${FIRST_RUN:-0}" = "1" ] && [ "${CHANGED:-0}" = "0" ]; then
TITLE="chore(upstream): record quickemu baseline (${UPSTREAM_SHORT})"
PR_INTRO="Initial baseline snapshots for the vendored quickemu files; no vendored code changed."
else
TITLE="chore(upstream): apply quickemu upstream ${UPSTREAM_SHORT}"
PR_INTRO="Auto-generated by \`.github/workflows/sync-upstream.yml\` — applies upstream quickemu changes to the vendored files while preserving DistroHopper's local patches."
fi
if [ -n "${PREV_SHA}" ] && [ "${PREV_SHA}" != "${UPSTREAM_SHA}" ]; then
COMPARE_LINK="https://github.com/${UPSTREAM_REPO}/compare/${PREV_SHA}...${UPSTREAM_SHA}"
else
COMPARE_LINK="https://github.com/${UPSTREAM_REPO}/commits/${UPSTREAM_BRANCH}"
fi
PR_BODY="$(printf '%s\n' \
"## Upstream sync: ${UPSTREAM_REPO} (${UPSTREAM_BRANCH} \`${UPSTREAM_SHORT}\`)" \
"" \
"${PR_INTRO}" \
"" \
"- Changed: ${CHANGED_FILES:-none}" \
"- Conflicts (manual resolution required): ${CONFLICT_FILES:-none}" \
"- Upstream diff: ${COMPARE_LINK}" \
"" \
"Resolve any \`<<<<<<< / >>>>>>>\` conflict markers in the files listed above, then merge." \
)"
# Direct commit is an explicit opt-in, and only safe when there is
# nothing conflicting and no review PR to overwrite. Conflicting
# syncs always go through the PR path so conflict markers can never
# reach the default branch.
if [ "${DIRECT_COMMIT:-false}" = "true" ] && [ "${CONFLICTS}" = "0" ] && [ "${FOUND_PR}" = "0" ]; then
git checkout -B "${BRANCH}"
stage_changes
git commit -m "${TITLE}"
git push origin "${BRANCH}"
echo "Committed ${TITLE} directly to ${BRANCH}."
exit 0
fi
if [ "${DIRECT_COMMIT:-false}" = "true" ]; then
echo "::warning::direct_commit requested but conflicts=${CONFLICTS} open_pr=${FOUND_PR}; routing through a pull request instead."
fi
if [ "${FOUND_PR}" = "1" ]; then
# We are already on the open review branch. Add a new commit on top
# of the review history (no reset, no force push) so manual conflict
# resolutions and review feedback are preserved, then update the
# existing pull request.
stage_changes
git commit -m "${TITLE}" || true
git push origin "${PR_BRANCH}" \
|| { git pull --rebase origin "${PR_BRANCH}" && git push origin "${PR_BRANCH}"; }
gh pr edit "${PR_NUMBER}" --title "${TITLE}" --body "${PR_BODY}"
echo "Updated PR #${PR_NUMBER}"
else
# No review in flight: (re)create the automation branch from the
# default branch and open a new pull request.
git checkout -B "${PR_BRANCH}"
stage_changes
git commit -m "${TITLE}" || true
if git ls-remote --exit-code origin "${PR_BRANCH}" >/dev/null 2>&1; then
git push -f origin "${PR_BRANCH}" # stale branch, no open PR: safe to reset
else
git push -u origin "${PR_BRANCH}"
fi
gh pr create --base "${BRANCH}" --head "${PR_BRANCH}" --title "${TITLE}" --body "${PR_BODY}"
echo "Opened PR from ${PR_BRANCH} -> ${BRANCH}"
fi