From dd4634ef6fdbf3cfe4a08326638940d67f2744cf Mon Sep 17 00:00:00 2001 From: dcvz Date: Thu, 1 Aug 2024 15:23:28 +0200 Subject: [PATCH] Use alternative way to get PR number --- .github/workflows/update-pr-artifacts.yml | 43 ++++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/.github/workflows/update-pr-artifacts.yml b/.github/workflows/update-pr-artifacts.yml index c00606f..3acdf6a 100644 --- a/.github/workflows/update-pr-artifacts.yml +++ b/.github/workflows/update-pr-artifacts.yml @@ -12,22 +12,31 @@ jobs: steps: - name: Get PR Number id: pr-number - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - # Get list of PR's associated to the commit that triggered the workflow run - prs=$(gh api -H "Accept: application/vnd.github+json" "/repos/${{ github.event.pull_request.head.repo.full_name }}/commits/${{ github.event.workflow_run.head_sha }}/pulls") - - # Find the PR against the current repo - pr_number=$(echo "$prs" | jq -r '.[] | select(.base.repo.full_name == "${{ github.repository }}") | .number' | head -n1) - - if [ -z "$pr_number" ]; then - echo "No pull request found for this workflow run" - exit 1 - else - echo "pr_number=$pr_number" >> $GITHUB_OUTPUT - echo "PR number: $pr_number" - fi + uses: actions/github-script@v7 + with: + result-encoding: string + script: | + const { owner, repo } = context.repo; + + const findPRNumber = async () => { + const pulls = await github.rest.pulls.list({ owner, repo }); + for await (const { data } of github.paginate.iterator(pulls)) { + for (const pull of data) { + if (pull.head.sha === '${{ github.event.workflow_run.head_sha }}' && pull.user.id === '${{ github.event.sender.id }}') { + return pull.number; + } + } + } + + return null; + }; + + const prNumber = await findPRNumber(); + if (!prNumber) { + core.error(`No matching pull request found`); + } else { + return prNumber; + } - name: Create Artifacts Content id: artifacts-content uses: actions/github-script@v7 @@ -51,5 +60,5 @@ jobs: with: section-name: 'artifacts' repo-token: '${{ secrets.GITHUB_TOKEN }}' - pr-number: ${{ steps.pr-number.outputs.pr_number }} + pr-number: ${{ steps.pr-number.outputs.result }} section-value: '${{ steps.artifacts-content.outputs.result }}'