ci: extract get-pr-info job to avoid duplicate PR lookups

This commit is contained in:
Johnpaul
2026-01-16 22:25:50 +01:00
parent af69acddc1
commit 821cf4acbf

View File

@@ -187,12 +187,54 @@ jobs:
# when using pull_request event, we have permission to comment directly
# if its a forked repo, we need to use workflow_run event in a separate workflow (pr-playwright-deploy.yaml)
# Post starting comment for non-forked PRs
comment-on-pr-start:
# Get PR info once for reuse by comment jobs
get-pr-info:
runs-on: ubuntu-latest
if: |
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false) ||
(github.event_name == 'workflow_run')
outputs:
pr_number: ${{ steps.get-pr.outputs.result }}
branch: ${{ steps.get-branch.outputs.branch }}
steps:
- name: Get PR number
id: get-pr
uses: actions/github-script@v7
with:
script: |
if (context.eventName === 'pull_request') {
return context.payload.pull_request.number;
}
// First check pull_requests from payload (most reliable)
const prs = context.payload.workflow_run.pull_requests;
if (prs && prs.length > 0) {
return prs[0].number;
}
// Fallback: branch-based lookup with pagination
const head = `${context.repo.owner}:${context.payload.workflow_run.head_branch}`;
for await (const response of github.paginate.iterator(
github.rest.pulls.list,
{ owner: context.repo.owner, repo: context.repo.repo, state: 'open', head, per_page: 100 }
)) {
if (response.data.length > 0) {
return response.data[0].number;
}
}
console.log('No PR found');
return '';
- name: Get branch name
id: get-branch
run: echo "branch=${{ github.head_ref || github.event.workflow_run.head_branch }}" >> $GITHUB_OUTPUT
# Post starting comment for non-forked PRs
comment-on-pr-start:
needs: get-pr-info
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
@@ -201,20 +243,6 @@ jobs:
with:
ref: ${{ github.event.workflow_run.head_branch || github.ref }}
- name: Get PR number for workflow_run
id: get-pr
if: github.event_name == 'workflow_run'
uses: actions/github-script@v7
with:
script: |
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
});
const pr = prs.find(p => p.head.sha === context.payload.workflow_run.head_sha);
return pr?.number || '';
- name: Get start time
id: start-time
run: echo "time=$(date -u '+%m/%d/%Y, %I:%M:%S %p')" >> $GITHUB_OUTPUT
@@ -223,24 +251,18 @@ jobs:
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
PR_NUMBER="${{ github.event.pull_request.number || steps.get-pr.outputs.result }}"
BRANCH="${{ github.head_ref || github.event.workflow_run.head_branch }}"
chmod +x scripts/cicd/pr-playwright-deploy-and-comment.sh
./scripts/cicd/pr-playwright-deploy-and-comment.sh \
"$PR_NUMBER" \
"$BRANCH" \
"${{ needs.get-pr-info.outputs.pr_number }}" \
"${{ needs.get-pr-info.outputs.branch }}" \
"starting" \
"${{ steps.start-time.outputs.time }}"
# Deploy and comment for non-forked PRs only
deploy-and-comment:
needs: [playwright-tests, merge-reports]
needs: [playwright-tests, merge-reports, get-pr-info]
runs-on: ubuntu-latest
if: |
always() && (
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false) ||
(github.event_name == 'workflow_run')
)
if: always()
permissions:
pull-requests: write
contents: read
@@ -250,20 +272,6 @@ jobs:
with:
ref: ${{ github.event.workflow_run.head_branch || github.ref }}
- name: Get PR number for workflow_run
id: get-pr
if: github.event_name == 'workflow_run'
uses: actions/github-script@v7
with:
script: |
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
});
const pr = prs.find(p => p.head.sha === context.payload.workflow_run.head_sha);
return pr?.number || '';
- name: Download all playwright reports
uses: actions/download-artifact@v4
with:
@@ -277,10 +285,8 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}
GITHUB_SHA: ${{ github.event.pull_request.head.sha || github.event.workflow_run.head_sha }}
run: |
PR_NUMBER="${{ github.event.pull_request.number || steps.get-pr.outputs.result }}"
BRANCH="${{ github.head_ref || github.event.workflow_run.head_branch }}"
bash ./scripts/cicd/pr-playwright-deploy-and-comment.sh \
"$PR_NUMBER" \
"$BRANCH" \
"${{ needs.get-pr-info.outputs.pr_number }}" \
"${{ needs.get-pr-info.outputs.branch }}" \
"completed"
#### END Deployment and commenting (non-forked PRs only)