Files
marqdo-app-harness/.github/workflows/pr-check-on-head-branch.yml
T
2026-11-22 11:23:51 +08:00

145 lines
7.0 KiB
YAML

name: Policy
on:
pull_request_target:
branches: [master, 'release/dsh-[0-9]+.[0-9]+.[0-9]+']
types: [opened, synchronize, reopened, ready_for_review, review_requested, auto_merge_enabled, labeled]
permissions:
contents: read
concurrency:
group: pr-check-on-head-branch-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
master:
name: Master Change
# Each check runs only while its repository variable is neither empty nor `off`; otherwise the job is skipped.
if: ${{ vars.MERGE_POLICY_MASTER_DIFF != '' && vars.MERGE_POLICY_MASTER_DIFF != 'off' && !github.event.pull_request.draft }}
runs-on: ubuntu-latest
timeout-minutes: 15
outputs:
base_sha: ${{ steps.check.outputs.base_sha }}
since: ${{ steps.check.outputs.since }}
steps:
# The head checkout is only the Git repository the check fetches into; it needs no files and runs no code.
- uses: actions/checkout@v7.0.1
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 1
filter: blob:none
sparse-checkout: README.md
sparse-checkout-cone-mode: false
- uses: actions/setup-node@v7.0.0
with:
node-version: '^24.22.3'
package-manager-cache: false
- id: check
uses: deepseek-harness/deepseek-harness-check@master
env:
CHECK_MODE: ${{ vars.MERGE_POLICY_MASTER_DIFF }}
with:
check: master-diff
base-branch: ${{ github.event.pull_request.base.ref }}
head-sha: ${{ github.event.pull_request.head.sha }}
pre-merge:
name: Pre-Merge Check
needs: master
if: ${{ vars.MERGE_POLICY_PRE_MERGE != '' && vars.MERGE_POLICY_PRE_MERGE != 'off' && needs.master.result == 'success' }}
runs-on: blacksmith-4vcpu-ubuntu-2404
timeout-minutes: 60
# This job installs and compiles the pull request: it reads contents only and receives no secret.
permissions:
contents: read
steps:
- uses: actions/checkout@v7.0.1
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 1
# Merging is the precondition of the build below, so a failure here warns and skips it in `report` mode and fails
# in `block` mode.
- id: merge
name: Merge the checked base-branch commit
env:
CHECK_MODE: ${{ vars.MERGE_POLICY_PRE_MERGE }}
BASE_SHA: ${{ needs.master.outputs.base_sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
stop() {
if [ "$CHECK_MODE" = block ]; then
echo "::error title=Pre-Merge Check::$1"
exit 1
fi
echo "::warning title=Pre-Merge Check::$1"
exit 0
}
if [[ ! "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]]; then
stop "the master diff check reported no base commit, so nothing was merged or compiled"
fi
# The checkout is already shallow, and a shallow fetch starts a background maintenance run that rewrites
# .git/shallow, so the fetch below would fail with "shallow file has changed since we read it".
# The history is bounded by depth, not by date: a date window marks a merge commit shallow when one parent
# predates it, which drops the other parent too and lets the merge base settle on an older commit. Within its
# depth every commit keeps all parents; 150 covers the base-branch commits a 72-hour-old base can be behind.
git -c maintenance.auto=false -c gc.auto=0 fetch --no-tags --no-write-fetch-head --depth=150 --filter=blob:none origin "$HEAD_SHA" "$BASE_SHA" ||
stop "the base commit $BASE_SHA could not be fetched, so nothing was merged or compiled"
if ! git merge-base "$HEAD_SHA" "$BASE_SHA" >/dev/null; then
stop "the pull request shares no commit with $BASE_SHA within 150 commits of history, so nothing was merged or compiled; merge master into it and push, then this check runs again"
fi
# The blobless history makes Git fetch missing blobs from origin on demand. Within one process, Git's cache of
# loose-object directories does not see the merged blobs it writes, so a single `git merge` asks origin for
# them while it checks out the result, and origin, which has never seen them, refuses the whole fetch.
# The merge is computed by `git merge-tree` and checked out by a separate process, which rereads the
# directories.
set +e
merge_output=$(git merge-tree --write-tree "$HEAD_SHA" "$BASE_SHA")
merge_status=$?
set -e
if [ "$merge_status" -eq 1 ]; then
echo "$merge_output"
stop "the pull request conflicts with $BASE_SHA; merge master into it and push, then this check runs again"
fi
if [ "$merge_status" -ne 0 ]; then
stop "git merge-tree could not merge $BASE_SHA (exit $merge_status), so nothing was compiled"
fi
merge_commit=$(git -c user.name=pr-check -c user.email=pr-check@users.noreply.github.com commit-tree "${merge_output%%$'\n'*}" -p "$HEAD_SHA" -p "$BASE_SHA" -m "Merge $BASE_SHA into $HEAD_SHA") ||
stop "the merged tree of $BASE_SHA could not be committed, so nothing was compiled"
git checkout --quiet --detach "$merge_commit" ||
stop "the merged tree of $BASE_SHA could not be checked out, so nothing was compiled"
echo "merged=true" >> "$GITHUB_OUTPUT"
- uses: pnpm/action-setup@v5.0.0
if: steps.merge.outputs.merged == 'true'
- uses: actions/setup-node@v7.0.0
if: steps.merge.outputs.merged == 'true'
with:
node-version: '^24.22.3'
package-manager-cache: false
- name: Run the tree's pre-merge check
if: steps.merge.outputs.merged == 'true'
env:
CHECK_MODE: ${{ vars.MERGE_POLICY_PRE_MERGE }}
BASE_SHA: ${{ needs.master.outputs.base_sha }}
run: |
# The check names one file and knows nothing else about how the merged tree builds itself.
if [ ! -f scripts/pre-merge-check.ts ]; then
if [ "$CHECK_MODE" = block ]; then
echo "::error title=Pre-Merge Check::this tree has no scripts/pre-merge-check.ts, so nothing was installed, compiled, or checked"
exit 1
fi
echo "::warning title=Pre-Merge Check::this tree has no scripts/pre-merge-check.ts, so nothing was installed, compiled, or checked"
exit 0
fi
if node scripts/pre-merge-check.ts; then
exit 0
fi
# A check that crashes reports nothing, so the step reports its exit the way the mode asks.
if [ "$CHECK_MODE" = block ]; then
echo "::error title=Pre-Merge Check::scripts/pre-merge-check.ts exited without reporting, so the merged tree is unchecked"
exit 1
fi
echo "::warning title=Pre-Merge Check::scripts/pre-merge-check.ts exited without reporting, so the merged tree is unchecked"