From c4d7f5f8e86d454713d964125767a6cdffc83d7e Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 16 May 2026 10:46:50 -0400 Subject: [PATCH] ci: Use github-script to determine workflow context Replace the previous shell-based "Determine Context" step with actions/github-script to robustly determine workflow_run context. The new script validates PR numbers, attempts to resolve missing pull request IDs by querying the GitHub API (matching by head owner:branch, head SHA or base repo), and sets outputs (event_name, head_branch, is_pr, pr_number, is_master_push) with improved logging and error handling. Also pins the github-script and checkout action SHAs for reproducibility. --- .github/workflows/publish-screenshots.yml | 107 ++++++++++++++++------ 1 file changed, 77 insertions(+), 30 deletions(-) diff --git a/.github/workflows/publish-screenshots.yml b/.github/workflows/publish-screenshots.yml index fa651164..beb18452 100644 --- a/.github/workflows/publish-screenshots.yml +++ b/.github/workflows/publish-screenshots.yml @@ -72,36 +72,83 @@ jobs: - name: Determine Context id: context - env: - WORKFLOW_EVENT: ${{ github.event.workflow_run.event }} - WORKFLOW_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} - run: | - event_name="${WORKFLOW_EVENT}" - head_branch="${WORKFLOW_HEAD_BRANCH}" - pr_number="$(jq -r '.workflow_run.pull_requests[0].number // empty' "${GITHUB_EVENT_PATH}")" - - if [ -n "${pr_number}" ] && ! [[ "${pr_number}" =~ ^[0-9]+$ ]]; then - echo "Invalid pr_number value: ${pr_number}" - exit 1 - fi - - is_pr=false - if [ "${event_name}" = "pull_request" ] && [ -n "${pr_number}" ]; then - is_pr=true - fi - - is_master_push=false - if [ "${event_name}" = "push" ] && [ "${head_branch}" = "master" ]; then - is_master_push=true - fi - - { - echo "event_name=${event_name}" - echo "head_branch=${head_branch}" - echo "is_pr=${is_pr}" - echo "pr_number=${pr_number}" - echo "is_master_push=${is_master_push}" - } >> "${GITHUB_OUTPUT}" + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const run = context.payload.workflow_run; + const eventName = run.event || ''; + const headBranch = run.head_branch || ''; + const headRepository = run.head_repository || {}; + const headRepositoryName = headRepository.full_name || ''; + + function normalizePrNumber(value) { + if (value === undefined || value === null || value === '') { + return ''; + } + + const prNumber = String(value); + if (!/^\d+$/.test(prNumber)) { + throw new Error(`Invalid PR number value: ${prNumber}`); + } + + return prNumber; + } + + let prNumber = ''; + if (eventName === 'pull_request') { + prNumber = normalizePrNumber(run.pull_requests?.[0]?.number); + + if (!prNumber) { + const headOwner = headRepository.owner?.login + || headRepository.owner?.name + || headRepositoryName.split('/')[0] + || ''; + const head = headOwner && headBranch ? `${headOwner}:${headBranch}` : ''; + + if (head) { + core.info(`workflow_run.pull_requests is empty; resolving PR from head ${head}.`); + const pullRequests = await github.paginate(github.rest.pulls.list, { + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head, + sort: 'updated', + direction: 'desc', + per_page: 100, + }); + const baseRepository = `${context.repo.owner}/${context.repo.repo}`; + const matchingSha = pullRequests.find((pr) => pr.head?.sha === run.head_sha); + const matchingBase = pullRequests.find((pr) => pr.base?.repo?.full_name === baseRepository); + const pullRequest = matchingSha || matchingBase || pullRequests[0]; + prNumber = normalizePrNumber(pullRequest?.number); + } + } + + if (!prNumber) { + throw new Error([ + 'Unable to determine PR number for pull_request workflow_run.', + `head_repository=${headRepositoryName || ''}`, + `head_branch=${headBranch || ''}`, + `head_sha=${run.head_sha || ''}`, + `payload_pull_requests=${run.pull_requests?.length || 0}`, + ].join(' ')); + } + } + + const isPr = eventName === 'pull_request' && prNumber !== ''; + const isMasterPush = eventName === 'push' && headBranch === 'master'; + + core.setOutput('event_name', eventName); + core.setOutput('head_branch', headBranch); + core.setOutput('is_pr', String(isPr)); + core.setOutput('pr_number', prNumber); + core.setOutput('is_master_push', String(isMasterPush)); + + core.info(`event_name=${eventName}`); + core.info(`head_branch=${headBranch}`); + core.info(`is_pr=${isPr}`); + core.info(`pr_number=${prNumber}`); + core.info(`is_master_push=${isMasterPush}`); - name: Checkout Screenshots Branch uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2