Skip to content

fix(scripts): query workflow runs by branch name instead of pull_requests field#3794

Open
aterga wants to merge 1 commit intomainfrom
fix/deploy-pr-workflow-detection
Open

fix(scripts): query workflow runs by branch name instead of pull_requests field#3794
aterga wants to merge 1 commit intomainfrom
fix/deploy-pr-workflow-detection

Conversation

@aterga
Copy link
Copy Markdown
Collaborator

@aterga aterga commented Apr 20, 2026

The pull_requests field in the GitHub API's workflow runs endpoint is often empty for recent runs, causing deploy-pr-to-beta to fail to locate the workflow run.

Query by the PR's head branch name instead, which is reliably populated.

Test plan

…ests field

The pull_requests field in the GitHub API's workflow runs endpoint is often
empty for recent runs, causing deploy-pr-to-beta to fail to locate the
workflow run. Query by the PR's head branch name instead, which is reliably
populated.

Fixes: deploy-pr-to-beta script unable to find canister-tests.yml runs for PRs
@aterga aterga requested a review from a team as a code owner April 20, 2026 22:17
Copilot AI review requested due to automatic review settings April 20, 2026 22:17
@aterga aterga enabled auto-merge April 20, 2026 22:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates deploy-pr-to-beta to locate the relevant GitHub Actions workflow run by PR head branch name instead of relying on the pull_requests field (often empty for recent runs), improving reliability when selecting runs to deploy from.

Changes:

  • Fetch PR head branch name via gh pr view and use it to filter workflow runs.
  • Adjust workflow runs API query and jq filtering to match runs by head_branch and workflow file path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread scripts/deploy-pr-to-beta
Comment on lines +206 to 209
| jq -r --arg BRANCH "$BRANCH" --arg WF "$WORKFLOW_FILE" '
[.workflow_runs[]
| select(.pull_requests[]?.number == ($PR|tonumber))
| select(.head_branch == $BRANCH)
| select(.path == (".github/workflows/" + $WF))
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering workflow runs only by .head_branch == $BRANCH can select the wrong run when multiple PRs (especially from forks) use the same branch name (e.g. main, feature/foo). That can lead to deploying artifacts from an unrelated PR. To uniquely identify the run, also match the PR’s head SHA (via gh pr view --json headRefOid) and/or the PR’s head repository (e.g. headRepository.nameWithOwner) against the run’s head_sha / head_repository fields.

Copilot uses AI. Check for mistakes.
Comment thread scripts/deploy-pr-to-beta

# Fetch the most recent workflow run for this PR (regardless of overall conclusion)
# Get the branch name for this PR
BRANCH=$(gh pr view "$PR_NUMBER" --json headRefName -q '.headRefName')
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gh pr view here relies on the current directory’s repo context, but the script’s API calls are hard-coded to $REPO. If someone runs this from a fork/other checkout, gh may fetch the wrong PR (or fail), producing an incorrect BRANCH. Pass --repo "$REPO" and add a small guard that BRANCH is non-empty/non-null before continuing.

Suggested change
BRANCH=$(gh pr view "$PR_NUMBER" --json headRefName -q '.headRefName')
BRANCH=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json headRefName -q '.headRefName')
if [ -z "$BRANCH" ] || [ "$BRANCH" = "null" ]; then
echo "Error: Could not determine branch name for PR #$PR_NUMBER in repository $REPO"
exit 1
fi

Copilot uses AI. Check for mistakes.
Comment thread scripts/deploy-pr-to-beta
Comment on lines 204 to +206
RUN_ID=$(curl -sf -H "$AUTH_HEADER" \
"https://api.github.com/repos/$REPO/actions/runs?event=push&per_page=100" \
| jq -r --arg PR "$PR_NUMBER" --arg WF "$WORKFLOW_FILE" '
"https://api.github.com/repos/$REPO/actions/runs?per_page=100" \
| jq -r --arg BRANCH "$BRANCH" --arg WF "$WORKFLOW_FILE" '
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo runs API supports server-side filtering by branch (and you can also query runs for a specific workflow file). Right now you fetch the last 100 runs across all branches and then filter in jq, which can miss the PR’s run if it’s not in that first page on a busy repo. Consider adding branch=$BRANCH (and ideally using the workflow-specific runs endpoint for $WORKFLOW_FILE) to make this robust.

Copilot uses AI. Check for mistakes.
@aterga aterga requested a review from sea-snake April 20, 2026 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants