Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
214 changes: 209 additions & 5 deletions .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ on:
description: Open pull request number to validate against its base
required: true
type: number
head_sha:
description: Exact pull request head revision to validate
required: true
type: string

permissions:
contents: read
Expand All @@ -23,6 +27,9 @@ jobs:
name: content
runs-on: ubuntu-latest
timeout-minutes: 10
outputs:
target_ref: ${{ steps.revisions.outputs.base_branch }}
target_sha: ${{ steps.revisions.outputs.base_sha }}
permissions:
contents: read
pull-requests: read
Expand All @@ -33,9 +40,13 @@ jobs:
env:
EVENT_NAME: ${{ github.event_name }}
PR_NUMBER: ${{ inputs.pr_number }}
EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
EVENT_BASE_SHA: ${{ github.event.pull_request.base.sha }}
EVENT_BASE_REF: ${{ github.event.pull_request.base.ref }}
EVENT_BASE_REPOSITORY: ${{ github.event.pull_request.base.repo.full_name }}
EVENT_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
EVENT_HEAD_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }}
EXPECTED_HEAD_SHA: ${{ inputs.head_sha }}
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
Expand All @@ -46,32 +57,80 @@ jobs:
exit 1
fi

pr="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")"
if ! pr="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")"; then
echo "::error::Could not fetch PR #$PR_NUMBER metadata"
exit 1
fi
if [[ "$(jq -r '.state' <<<"$pr")" != "open" ]]; then
echo "::error::PR #$PR_NUMBER is not open"
exit 1
fi

base_sha="$(jq -r '.base.sha' <<<"$pr")"
pr_number="$PR_NUMBER"
base_branch="$(jq -r '.base.ref // empty' <<<"$pr")"
base_repository="$(jq -r '.base.repo.full_name // empty' <<<"$pr")"
head_sha="$(jq -r '.head.sha' <<<"$pr")"
head_repository="$(jq -r '.head.repo.full_name // empty' <<<"$pr")"

if [[ ! "$EXPECTED_HEAD_SHA" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then
echo "::error::head_sha must be a full 40- or 64-character commit SHA"
exit 1
fi
if [[ "$head_sha" != "$EXPECTED_HEAD_SHA" ]]; then
echo "::error::PR #$PR_NUMBER head changed: expected $EXPECTED_HEAD_SHA, found $head_sha"
exit 1
fi

if [[ -z "$base_branch" || -z "$base_repository" ]]; then
echo "::error::PR #$PR_NUMBER is missing its target branch or repository"
exit 1
fi
if [[ "$base_repository" != "$GITHUB_REPOSITORY" ]]; then
echo "::error::PR #$PR_NUMBER targets unexpected repository $base_repository"
exit 1
fi

if ! target_commits="$(gh api --method GET "repos/$base_repository/commits" \
-f "sha=$base_branch" -F per_page=1)"; then
echo "::error::Could not resolve current tip for $base_repository/$base_branch"
exit 1
fi
base_sha="$(jq -r '.[0].sha // empty' <<<"$target_commits")"
else
pr_number="$EVENT_PR_NUMBER"
base_branch="$EVENT_BASE_REF"
base_repository="$EVENT_BASE_REPOSITORY"
base_sha="$EVENT_BASE_SHA"
head_sha="$EVENT_HEAD_SHA"
head_repository="$EVENT_HEAD_REPOSITORY"
fi

if [[ -z "$base_sha" || -z "$head_sha" || -z "$head_repository" ]]; then
if [[ -z "$pr_number" || -z "$base_branch" || -z "$base_repository" \
|| -z "$base_sha" || -z "$head_sha" || -z "$head_repository" ]]; then
echo "::error::Could not resolve the pull request base and head"
exit 1
fi
if [[ ! "$base_sha" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ \
|| ! "$head_sha" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then
echo "::error::PR #$pr_number resolved an invalid base or head SHA"
exit 1
fi

{
echo "pr_number=$pr_number"
echo "base_repository=$base_repository"
echo "base_branch=$base_branch"
echo "base_ref=$base_sha"
echo "base_sha=$base_sha"
echo "head_sha=$head_sha"
echo "head_repository=$head_repository"
} >> "$GITHUB_OUTPUT"
{
echo "### PR #$pr_number validation revisions"
echo
echo "- Target: \`$base_repository/$base_branch\` at \`$base_sha\`"
echo "- Head: \`$head_repository\` at \`$head_sha\`"
} >> "$GITHUB_STEP_SUMMARY"

- name: Resolve pushed branch revisions
id: branch_revisions
Expand All @@ -91,6 +150,7 @@ jobs:
- name: Check out base
uses: actions/checkout@v4
with:
repository: ${{ steps.revisions.outputs.base_repository || github.repository }}
ref: ${{ steps.revisions.outputs.base_ref || steps.branch_revisions.outputs.base_ref }}
path: base
fetch-depth: 0
Expand All @@ -109,6 +169,7 @@ jobs:
env:
BASE_SHA: ${{ steps.revisions.outputs.base_sha }}
HEAD_SHA: ${{ steps.revisions.outputs.head_sha || steps.branch_revisions.outputs.head_sha }}
PR_NUMBER: ${{ steps.revisions.outputs.pr_number }}
run: |
set -euo pipefail

Expand All @@ -125,8 +186,15 @@ jobs:
git -C "$GITHUB_WORKSPACE/candidate" fetch --no-tags \
"$GITHUB_WORKSPACE/base" "$BASE_SHA:refs/remotes/base/target"
git -C "$GITHUB_WORKSPACE/candidate" diff --check "$BASE_SHA...$HEAD_SHA"
merged_tree="$(git -C "$GITHUB_WORKSPACE/candidate" merge-tree \
--write-tree "$HEAD_SHA" refs/remotes/base/target)"
if ! merged_tree="$(git -C "$GITHUB_WORKSPACE/candidate" merge-tree \
--write-tree "$HEAD_SHA" refs/remotes/base/target)"; then
if [[ -n "$PR_NUMBER" ]]; then
echo "::error::PR #$PR_NUMBER conflicts with target $BASE_SHA"
else
echo "::error::Changes conflict with target $BASE_SHA"
fi
exit 1
fi
if [[ ! "$merged_tree" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then
echo "::error::Could not resolve PR merge tree: $merged_tree"
exit 1
Expand All @@ -140,3 +208,139 @@ jobs:
- name: Validate repository content
working-directory: candidate
run: python3 scripts/validate_brand_assets.py

verify-dispatched-head:
name: verify-dispatched-head
if: always() && !cancelled() && github.event_name == 'workflow_dispatch'
needs: content
runs-on: ubuntu-latest
permissions:
pull-requests: read
steps:
- name: Confirm dispatched PR head is unchanged
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ inputs.pr_number }}
EXPECTED_HEAD_SHA: ${{ inputs.head_sha }}
run: |
set -euo pipefail

if ! pr="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")"; then
echo "::error::Could not verify PR #$PR_NUMBER head after validation"
exit 1
fi
if [[ "$(jq -r '.state' <<<"$pr")" != "open" ]]; then
echo "::error::PR #$PR_NUMBER closed during validation"
exit 1
fi
current_head="$(jq -r '.head.sha // empty' <<<"$pr")"
if [[ "$current_head" != "$EXPECTED_HEAD_SHA" ]]; then
echo "::error::PR #$PR_NUMBER head changed during validation: expected $EXPECTED_HEAD_SHA, found $current_head"
exit 1
fi
echo "PR #$PR_NUMBER remained open at head $EXPECTED_HEAD_SHA after validation."
{
echo "### Dispatch head verification"
echo
echo "- PR #$PR_NUMBER remained open at head \`$EXPECTED_HEAD_SHA\` after validation."
} >> "$GITHUB_STEP_SUMMARY"

publish-head-status:
name: publish-head-status
if: always() && !cancelled() && github.event_name == 'workflow_dispatch' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
needs: [content, verify-dispatched-head]
runs-on: ubuntu-latest
concurrency:
group: publish-pr-${{ inputs.pr_number }}
cancel-in-progress: false
permissions:
contents: read
pull-requests: read
statuses: write
steps:
- name: Publish dispatched PR status
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ inputs.pr_number }}
EXPECTED_HEAD_SHA: ${{ inputs.head_sha }}
EXPECTED_TARGET_REF: ${{ needs.content.outputs.target_ref }}
EXPECTED_TARGET_SHA: ${{ needs.content.outputs.target_sha }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
CONTENT_RESULT: ${{ needs.content.result }}
VERIFY_RESULT: ${{ needs.verify-dispatched-head.result }}
STATUS_CONTEXT: PR validation / dispatched-merge
RUN_ATTEMPT: ${{ github.run_attempt }}
RUN_ID: ${{ github.run_id }}
SERVER_URL: ${{ github.server_url }}
run: |
set -euo pipefail

if [[ ! "$PR_NUMBER" =~ ^[1-9][0-9]*$ \
|| ! "$EXPECTED_HEAD_SHA" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ \
|| ! "$EXPECTED_TARGET_SHA" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ \
|| -z "$DEFAULT_BRANCH" || -z "$EXPECTED_TARGET_REF" ]]; then
echo "::error::Cannot publish status with missing or invalid pinned revisions"
exit 1
fi

if [[ "$EXPECTED_TARGET_REF" != "$DEFAULT_BRANCH" ]]; then
echo "::error::Pinned target $EXPECTED_TARGET_REF is not default branch $DEFAULT_BRANCH"
exit 1
fi

case "$CONTENT_RESULT:$VERIFY_RESULT" in
success:success)
state=success
result_description="validation passed"
;;
failure:success|success:failure|failure:failure)
state=failure
if [[ "$CONTENT_RESULT" == "failure" && "$VERIFY_RESULT" == "failure" ]]; then
result_description="content validation and PR verification failed"
elif [[ "$CONTENT_RESULT" == "failure" ]]; then
result_description="content validation failed"
else
result_description="PR verification failed"
fi
;;
*)
echo "No dispatched PR status published for content=$CONTENT_RESULT verify=$VERIFY_RESULT"
exit 0
;;
esac

if ! pr="$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")"; then
echo "::error::Could not verify PR #$PR_NUMBER before status publication"
exit 1
fi
pr_state="$(jq -r '.state // empty' <<<"$pr")"
pr_base_repository="$(jq -r '.base.repo.full_name // empty' <<<"$pr")"
pr_base_ref="$(jq -r '.base.ref // empty' <<<"$pr")"
pr_head_sha="$(jq -r '.head.sha // empty' <<<"$pr")"
if [[ "$pr_state" != "open" || "$pr_base_repository" != "$GITHUB_REPOSITORY" \
|| "$pr_base_ref" != "$EXPECTED_TARGET_REF" || "$pr_head_sha" != "$EXPECTED_HEAD_SHA" ]]; then
echo "::error::PR #$PR_NUMBER revision binding is stale or invalid"
exit 1
fi

if ! target_commits="$(gh api --method GET "repos/$GITHUB_REPOSITORY/commits" \
-f "sha=$DEFAULT_BRANCH" -F per_page=1)"; then
echo "::error::Could not verify current target tip for $DEFAULT_BRANCH"
exit 1
fi
live_target_sha="$(jq -r '.[0].sha // empty' <<<"$target_commits")"
if [[ "$live_target_sha" != "$EXPECTED_TARGET_SHA" ]]; then
echo "::error::Target branch advanced: expected $EXPECTED_TARGET_SHA, found $live_target_sha"
exit 1
fi

description="target $EXPECTED_TARGET_SHA; attempt $RUN_ATTEMPT; $result_description"
target_url="$SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$RUN_ID/attempts/$RUN_ATTEMPT"
if ! status_response="$(gh api --method POST \
"repos/$GITHUB_REPOSITORY/statuses/$EXPECTED_HEAD_SHA" \
-f "state=$state" -f "context=$STATUS_CONTEXT" \
-f "description=$description" -f "target_url=$target_url")"; then
echo "::error::Could not publish $state status for PR #$PR_NUMBER head $EXPECTED_HEAD_SHA"
exit 1
fi
echo "Published $state for PR #$PR_NUMBER at $EXPECTED_HEAD_SHA against $EXPECTED_TARGET_SHA (run $RUN_ID attempt $RUN_ATTEMPT)."
Loading
Loading