Pull_request attribute empty in workflow_run event object for PR from forked repo

Hello there,

I notice a weird issue with the workflow_run event object when it involves a PR from a forked repo.

Context:
-I have a workflow_run workflow that will run after a specific pull_request workflow
-I wanted to find the PR number of the pull_request using the github.event context.

Problem:
-If the PR is from a local branch inside the repo, the pull_request attribute of the github.event context is populated

Link to Action log: Merge pull request #24 from Thomas-Boi/develop · Thomas-Boi/devicon@d0884eb · GitHub

-If the PR is from a forked branch outside the repo, the pull_request attribute of the github.event context is empty

(no picture because GitHub prevented me from posting more than one picture as a new user)

Link to Action log: Merge pull request #24 from Thomas-Boi/develop · Thomas-Boi/devicon@d0884eb · GitHub

This makes it unstable to find the PR number using the github.event attribute. I wonder if this is intentional for security reasons.

Code and Replication:
PR workflow (example of what I’m trying to do)

name: Peek Icons
on:
  pull_request:
      types: [labeled]
jobs:
  build:
    name: Peek Icons
    runs-on: windows-2019
    env:
      PR_NUMBER: ${{ github.event.number }}
    steps:
        - run: echo $PR_NUMBER 

workflow_run workflow (example of what I’m trying to do)

name: Post the screenshots into a comment from Peek Icons workflow
on:
  workflow_run:
    workflows: ["Peek Icons"]
    types: 
      - completed
jobs:
  post_screenshots_in_comment:
    name: Post the screenshot
    runs-on: ubuntu-18.0
    env:
      EVENT_OBJ:  ${{ toJson(github.event) }}
    steps:
        - run: echo $EVENT_OBJ

To replicate this issue:
-Create a repo and add these workflows in the master branch.
-Fork that repo
-Create a branch in the original repo. Add any change to it, push, then open a PR into master.
-This will trigger the pull_request workflow and the workflow_run workflow afterwards.
-View the log in the Actions tab
-Create a branch in the forked repo. Add any change to it, push, then open a PR into the original branch.
-View the log in the Actions tab
-This will trigger the pull_request workflow and the workflow_run workflow afterwards

2 Likes

I have the same problem. I’ve done other tests and I think the problem is the fork.
When the Pull request comes from a fork repository, the PR object comes empty everywhere in the workflow.
I tried in several ways, but I couldn’t relate the pull_request number to that execution, workflow, suite, etc.

Did you have any success after this post?

1 Like

Hi,

Yes I did. When I figured that I can’t get a PR number reliably from the workflow_run workflow, I decided to get it from the trigger workflow instead.

Details:

  • Say workflow A (which has a pull_request event) will triggers workflow B (which has a workflow_run event)
  • In workflow A, I get the PR number from the github.event. This is reliable for pull_request events. I then save that number into a file and upload it as an artifact.
      - name: Save the PR number in an artifact
        shell: bash
        env:
          PR_NUM: ${{ github.event.number }}
        run: echo $PR_NUM > pr_num.txt

      - name: Upload the PR number
        uses: actions/upload-artifact@v2
        with:
          name: pr_num
          path: ./pr_num.txt
  • In workflow B, I get the artifact and the PR number inside from the workflow A using the following GitHub Apps:
      - name: Download workflow artifact
        uses: dawidd6/action-download-artifact@v2.11.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          workflow: peek_icons.yml
          run_id: ${{ github.event.workflow_run.id }}

      - name: Read the pr_num file
        id: pr_num_reader
        uses: juliangruber/read-file-action@v1.0.0
        with:
          path: ./pr_num/pr_num.txt

This method has worked for me ever since. You can check out the files here:

1 Like