diff --git a/.github/workflows/merge-upstream.yml b/.github/workflows/merge-upstream.yml new file mode 100644 index 000000000..889b67c4b --- /dev/null +++ b/.github/workflows/merge-upstream.yml @@ -0,0 +1,91 @@ +name: Merge upstream updates + +on: + schedule: + - cron: '37 */4 * * *' + workflow_dispatch: + +permissions: + contents: write + +concurrency: + group: merge-upstream-intel-vm-suspend + cancel-in-progress: false + +jobs: + sync: + if: github.repository == 'reims-go/reims-vgpu' + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - name: Merge upstream into the maintenance branch + shell: bash + env: + GH_TOKEN: ${{ github.token }} + TARGET_BRANCH: codex/intel-vm-suspend + UPSTREAM_REPO: steelbrain/reims-vgpu + UPSTREAM_BRANCH: master + GIT_AUTHOR_NAME: YXC0915 + GIT_AUTHOR_EMAIL: 62410385+yxc0915@users.noreply.github.com + GIT_COMMITTER_NAME: 'github-actions[bot]' + GIT_COMMITTER_EMAIL: '41898282+github-actions[bot]@users.noreply.github.com' + GIT_TERMINAL_PROMPT: '0' + run: | + set -euo pipefail + + summary() { + printf '%s\n' "$1" | tee -a "$GITHUB_STEP_SUMMARY" + } + + # Check ancestry on GitHub first so routine polls do not fetch QEMU history. + target_ref=${TARGET_BRANCH//\//%2F} + upstream_ref=${UPSTREAM_BRANCH//\//%2F} + status=$(gh api "repos/$GITHUB_REPOSITORY/compare/$target_ref...${UPSTREAM_REPO%%/*}:$upstream_ref?per_page=1" --jq .status) + case "$status" in + identical|behind) + summary "$TARGET_BRANCH already contains $UPSTREAM_REPO:$UPSTREAM_BRANCH." + exit 0 + ;; + ahead|diverged) ;; + *) + echo "::error::Unexpected upstream comparison status: $status" + exit 1 + ;; + esac + + git init --bare --quiet "$RUNNER_TEMP/upstream-sync.git" + cd "$RUNNER_TEMP/upstream-sync.git" + git remote add origin "https://github.com/$GITHUB_REPOSITORY.git" + git remote add upstream "https://github.com/$UPSTREAM_REPO.git" + git fetch --no-tags --refmap= origin "refs/heads/$TARGET_BRANCH:refs/remotes/origin/target" + git fetch --no-tags --refmap= upstream "refs/heads/$UPSTREAM_BRANCH:refs/remotes/upstream/source" + target=$(git rev-parse refs/remotes/origin/target) + source=$(git rev-parse refs/remotes/upstream/source) + + # Recheck ancestry using the exact commits that were fetched. + if git merge-base --is-ancestor "$source" "$target"; then + summary "$TARGET_BRANCH already contains upstream commit $source." + exit 0 + fi + if git merge-base --is-ancestor "$target" "$source"; then + merged=$source + elif tree=$(git merge-tree --write-tree "$target" "$source"); then + merged=$(git commit-tree "$tree" -p "$target" -p "$source" \ + -m "Merge $UPSTREAM_REPO:$UPSTREAM_BRANCH into $TARGET_BRANCH") + else + result=$? + printf '%s\n' "$tree" | sed 's/^/ /' + if [[ "$result" != 1 ]]; then + exit "$result" + fi + echo "::warning::Upstream merge has conflicts. The maintenance branch was not updated." + summary "Skipped merging $source into $TARGET_BRANCH at $target because of merge conflicts. See the job log." + exit 0 + fi + + # A normal push rejects a competing branch update and leaves its work intact. + auth=$(printf 'x-access-token:%s' "$GH_TOKEN" | base64 -w0) + echo "::add-mask::$auth" + git -c "http.https://github.com/.extraheader=AUTHORIZATION: basic $auth" \ + push origin "$merged:refs/heads/$TARGET_BRANCH" + summary "Merged upstream commit $source into $TARGET_BRANCH at $merged."