GitHub Action pull_request
attribute empty in workflow_run
event object for PR from forked repo
#25220
Replies: 12 comments 36 replies
-
I have the same problem. I’ve done other tests and I think the problem is the fork. Did you have any success after this post? |
Beta Was this translation helpful? Give feedback.
-
Hi, Yes I did. When I figured that I can’t get a PR number reliably from the Details:
This method has worked for me ever since. You can check out the files here: |
Beta Was this translation helpful? Give feedback.
-
This issue persists, and is still not even documented by GitHub. It should not be considered “solved”, the solution provided above is merely a workaround. An alternative (perhaps easier and more comprehensive) workaround is to use the Get Workflow Origin action. It’s a bit of a kludge: it works around this issue by enumerating all PRs and finding the matching SHA for the workflow_run. But it works and requires minimal extra effort. See my implementation of this plugin for reference. |
Beta Was this translation helpful? Give feedback.
-
I would be nice if this info is available natively from the workflow_run event rather than resorting to finding the PR number |
Beta Was this translation helpful? Give feedback.
-
Actually even from the REST API the pull request array is empty and it's not even a fork.
A run that ran on a pull request In the payload below notice {
"id": 6223317359,
"name": "Java CI with Gradle",
"node_id": "WFR_kwLOEufiyM8AAAABcvBJbw",
"head_branch": "windows-failure",
"head_sha": "0a1e93ee775f4311bf61aa78171890c1937c7c6d",
"path": ".github/workflows/gradle.yml",
"display_title": "Understand windows failure",
"run_number": 242,
"event": "pull_request",
"status": "completed",
"conclusion": "success",
"workflow_id": 20103782,
"check_suite_id": 16285471685,
"check_suite_node_id": "CS_kwDOEufiyM8AAAADyrCTxQ",
"url": "https://api.github.com/repos/bric3/jufmt/actions/runs/6223317359",
"html_url": "https://github.com/bric3/jufmt/actions/runs/6223317359",
"pull_requests": [],
"created_at": "2023-09-18T13:35:21Z",
"updated_at": "2023-09-18T13:42:56Z",
"actor": {
"login": "bric3",
"id": 803621,
"node_id": "MDQ6VXNlcjgwMzYyMQ==",
"avatar_url": "https://avatars.githubusercontent.com/u/803621?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bric3",
"html_url": "https://github.com/bric3",
"followers_url": "https://api.github.com/users/bric3/followers",
"following_url": "https://api.github.com/users/bric3/following{/other_user}",
"gists_url": "https://api.github.com/users/bric3/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bric3/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bric3/subscriptions",
"organizations_url": "https://api.github.com/users/bric3/orgs",
"repos_url": "https://api.github.com/users/bric3/repos",
"events_url": "https://api.github.com/users/bric3/events{/privacy}",
"received_events_url": "https://api.github.com/users/bric3/received_events",
"type": "User",
"site_admin": false
},
"run_attempt": 1,
"referenced_workflows": [],
"run_started_at": "2023-09-18T13:35:21Z",
"triggering_actor": {
"login": "bric3",
"id": 803621,
"node_id": "MDQ6VXNlcjgwMzYyMQ==",
"avatar_url": "https://avatars.githubusercontent.com/u/803621?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bric3",
"html_url": "https://github.com/bric3",
"followers_url": "https://api.github.com/users/bric3/followers",
"following_url": "https://api.github.com/users/bric3/following{/other_user}",
"gists_url": "https://api.github.com/users/bric3/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bric3/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bric3/subscriptions",
"organizations_url": "https://api.github.com/users/bric3/orgs",
"repos_url": "https://api.github.com/users/bric3/repos",
"events_url": "https://api.github.com/users/bric3/events{/privacy}",
"received_events_url": "https://api.github.com/users/bric3/received_events",
"type": "User",
"site_admin": false
},
"jobs_url": "https://api.github.com/repos/bric3/jufmt/actions/runs/6223317359/jobs",
"logs_url": "https://api.github.com/repos/bric3/jufmt/actions/runs/6223317359/logs",
"check_suite_url": "https://api.github.com/repos/bric3/jufmt/check-suites/16285471685",
"artifacts_url": "https://api.github.com/repos/bric3/jufmt/actions/runs/6223317359/artifacts",
"cancel_url": "https://api.github.com/repos/bric3/jufmt/actions/runs/6223317359/cancel",
"rerun_url": "https://api.github.com/repos/bric3/jufmt/actions/runs/6223317359/rerun",
"previous_attempt_url": null,
"workflow_url": "https://api.github.com/repos/bric3/jufmt/actions/workflows/20103782",
"head_commit": {
"id": "0a1e93ee775f4311bf61aa78171890c1937c7c6d",
"tree_id": "e2e909cfe4c4b248531a510b444557f68f3ce506",
"message": "chore: Moves figlet test in lib, and use mockito in cli",
"timestamp": "2023-09-18T13:34:58Z",
"author": {
"name": "Brice Dutheil",
"email": "[email protected]"
},
"committer": {
"name": "Brice Dutheil",
"email": "[email protected]"
}
},
"repository": {
"id": 317186760,
"node_id": "MDEwOlJlcG9zaXRvcnkzMTcxODY3NjA=",
"name": "jufmt",
"full_name": "bric3/jufmt",
"private": false,
"owner": {
"login": "bric3",
"id": 803621,
"node_id": "MDQ6VXNlcjgwMzYyMQ==",
"avatar_url": "https://avatars.githubusercontent.com/u/803621?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bric3",
"html_url": "https://github.com/bric3",
"followers_url": "https://api.github.com/users/bric3/followers",
"following_url": "https://api.github.com/users/bric3/following{/other_user}",
"gists_url": "https://api.github.com/users/bric3/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bric3/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bric3/subscriptions",
"organizations_url": "https://api.github.com/users/bric3/orgs",
"repos_url": "https://api.github.com/users/bric3/repos",
"events_url": "https://api.github.com/users/bric3/events{/privacy}",
"received_events_url": "https://api.github.com/users/bric3/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/bric3/jufmt",
"description": "Java Unicode formatter",
"fork": false,
"url": "https://api.github.com/repos/bric3/jufmt",
"forks_url": "https://api.github.com/repos/bric3/jufmt/forks",
"keys_url": "https://api.github.com/repos/bric3/jufmt/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/bric3/jufmt/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/bric3/jufmt/teams",
"hooks_url": "https://api.github.com/repos/bric3/jufmt/hooks",
"issue_events_url": "https://api.github.com/repos/bric3/jufmt/issues/events{/number}",
"events_url": "https://api.github.com/repos/bric3/jufmt/events",
"assignees_url": "https://api.github.com/repos/bric3/jufmt/assignees{/user}",
"branches_url": "https://api.github.com/repos/bric3/jufmt/branches{/branch}",
"tags_url": "https://api.github.com/repos/bric3/jufmt/tags",
"blobs_url": "https://api.github.com/repos/bric3/jufmt/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/bric3/jufmt/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/bric3/jufmt/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/bric3/jufmt/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/bric3/jufmt/statuses/{sha}",
"languages_url": "https://api.github.com/repos/bric3/jufmt/languages",
"stargazers_url": "https://api.github.com/repos/bric3/jufmt/stargazers",
"contributors_url": "https://api.github.com/repos/bric3/jufmt/contributors",
"subscribers_url": "https://api.github.com/repos/bric3/jufmt/subscribers",
"subscription_url": "https://api.github.com/repos/bric3/jufmt/subscription",
"commits_url": "https://api.github.com/repos/bric3/jufmt/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/bric3/jufmt/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/bric3/jufmt/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/bric3/jufmt/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/bric3/jufmt/contents/{+path}",
"compare_url": "https://api.github.com/repos/bric3/jufmt/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/bric3/jufmt/merges",
"archive_url": "https://api.github.com/repos/bric3/jufmt/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/bric3/jufmt/downloads",
"issues_url": "https://api.github.com/repos/bric3/jufmt/issues{/number}",
"pulls_url": "https://api.github.com/repos/bric3/jufmt/pulls{/number}",
"milestones_url": "https://api.github.com/repos/bric3/jufmt/milestones{/number}",
"notifications_url": "https://api.github.com/repos/bric3/jufmt/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/bric3/jufmt/labels{/name}",
"releases_url": "https://api.github.com/repos/bric3/jufmt/releases{/id}",
"deployments_url": "https://api.github.com/repos/bric3/jufmt/deployments"
},
"head_repository": {
"id": 317186760,
"node_id": "MDEwOlJlcG9zaXRvcnkzMTcxODY3NjA=",
"name": "jufmt",
"full_name": "bric3/jufmt",
"private": false,
"owner": {
"login": "bric3",
"id": 803621,
"node_id": "MDQ6VXNlcjgwMzYyMQ==",
"avatar_url": "https://avatars.githubusercontent.com/u/803621?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bric3",
"html_url": "https://github.com/bric3",
"followers_url": "https://api.github.com/users/bric3/followers",
"following_url": "https://api.github.com/users/bric3/following{/other_user}",
"gists_url": "https://api.github.com/users/bric3/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bric3/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bric3/subscriptions",
"organizations_url": "https://api.github.com/users/bric3/orgs",
"repos_url": "https://api.github.com/users/bric3/repos",
"events_url": "https://api.github.com/users/bric3/events{/privacy}",
"received_events_url": "https://api.github.com/users/bric3/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/bric3/jufmt",
"description": "Java Unicode formatter",
"fork": false,
"url": "https://api.github.com/repos/bric3/jufmt",
"forks_url": "https://api.github.com/repos/bric3/jufmt/forks",
"keys_url": "https://api.github.com/repos/bric3/jufmt/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/bric3/jufmt/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/bric3/jufmt/teams",
"hooks_url": "https://api.github.com/repos/bric3/jufmt/hooks",
"issue_events_url": "https://api.github.com/repos/bric3/jufmt/issues/events{/number}",
"events_url": "https://api.github.com/repos/bric3/jufmt/events",
"assignees_url": "https://api.github.com/repos/bric3/jufmt/assignees{/user}",
"branches_url": "https://api.github.com/repos/bric3/jufmt/branches{/branch}",
"tags_url": "https://api.github.com/repos/bric3/jufmt/tags",
"blobs_url": "https://api.github.com/repos/bric3/jufmt/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/bric3/jufmt/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/bric3/jufmt/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/bric3/jufmt/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/bric3/jufmt/statuses/{sha}",
"languages_url": "https://api.github.com/repos/bric3/jufmt/languages",
"stargazers_url": "https://api.github.com/repos/bric3/jufmt/stargazers",
"contributors_url": "https://api.github.com/repos/bric3/jufmt/contributors",
"subscribers_url": "https://api.github.com/repos/bric3/jufmt/subscribers",
"subscription_url": "https://api.github.com/repos/bric3/jufmt/subscription",
"commits_url": "https://api.github.com/repos/bric3/jufmt/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/bric3/jufmt/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/bric3/jufmt/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/bric3/jufmt/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/bric3/jufmt/contents/{+path}",
"compare_url": "https://api.github.com/repos/bric3/jufmt/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/bric3/jufmt/merges",
"archive_url": "https://api.github.com/repos/bric3/jufmt/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/bric3/jufmt/downloads",
"issues_url": "https://api.github.com/repos/bric3/jufmt/issues{/number}",
"pulls_url": "https://api.github.com/repos/bric3/jufmt/pulls{/number}",
"milestones_url": "https://api.github.com/repos/bric3/jufmt/milestones{/number}",
"notifications_url": "https://api.github.com/repos/bric3/jufmt/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/bric3/jufmt/labels{/name}",
"releases_url": "https://api.github.com/repos/bric3/jufmt/releases{/id}",
"deployments_url": "https://api.github.com/repos/bric3/jufmt/deployments"
}
}, |
Beta Was this translation helpful? Give feedback.
-
This is still a problem. Interestingly, the
You can access the PR ID in the following steps with The things you have to do to simply comment on a PR originated from a fork is unreal. |
Beta Was this translation helpful? Give feedback.
-
I just came across the issue and found out (yet) another workaround. I prefer to avoid using artifacts to send the PR number information as those could be altered. It's inspired on @danepowell 's workaround in the GH Action Get Workflow Origin. It lists latest PRs in repository and looks for the one whose head commit SHA matches the workflow run head SHA (which we have in the event payload). It's a bit hacky, given we can't specify in the API call that we want the PR whose head SHA is that one and we have to look in the list of latest PRs in the repository. Also, the action is not maintained anymore 😢 So you'd have to copy that piece into a GitHub script step. Then I found out that using GitHub's issues & PRs search API we can look for the PR given the head SHA Here's the magic script: 🪄 - name: Find associated pull request
id: pr
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7
with:
script: |
const response = await github.rest.search.issuesAndPullRequests({
q: 'repo:${{ github.repository }} is:pr sha:${{ github.event.workflow_run.head_sha }}',
per_page: 1,
})
const items = response.data.items
if (items.length < 1) {
console.error('No PRs found')
return
}
const pullRequestNumber = items[0].number
console.info("Pull request number is", pullRequestNumber)
return pullRequestNumber And here's how the PR number is used - name: Find bundle size PR comment
uses: peter-evans/find-comment@d5fe37641ad8451bdd80312415672ba26c86575e # v3
id: fc
with:
issue-number: ${{ steps.pr.outputs.result }} # 👈
comment-author: 'github-actions[bot]'
body-includes: ${{ env.BUNDLE_SIZE_COMMENT_ID_PREFIX }}${{ matrix.version }} |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
jobs:
|
Beta Was this translation helpful? Give feedback.
-
I've commented on most of the answers posted here while investigating what the most appropriate approach is in 2024. In doing so I've also reached out to GitHub Security Labs (GHSL) staff and received feedback on these different solutions. TL;DR: Go with Solution B ( I have documented a detailed reference for comparing these solutions with additional insights/differences covered there. B and C also have full workflow examples. Solutions A and B are workflows using Solutions
Example - Solution BBesides the full real-world example in the earlier comparisons reference link, here are two more references that I shared in this discussion:
Here's a rough snippet for it's use in a workflow triggered by name: 'Documentation (Deploy)'
on:
workflow_run:
workflows: ['Documentation (PR)']
types:
- completed
permissions:
# Required by `pr-context` step:
contents: read
# Required by `marocchino/sticky-pull-request-comment` (write) + `pr-context` step (read):
pull-requests: write
jobs:
# ...
steps:
- name: 'Get PR context'
id: pr-context
env:
# Token required for GH CLI:
GH_TOKEN: ${{ github.token }}
# Best practice for scripts is to reference via ENV at runtime. Avoid using the expression syntax in the script content directly:
PR_TARGET_REPO: ${{ github.repository }}
# If the PR is from a fork, prefix it with `<owner-login>:`, otherwise only the PR branch name is relevant:
PR_BRANCH: |-
${{
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login)
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch)
|| github.event.workflow_run.head_branch
}}
# Query the PR number by repo + branch, then assign to step output:
run: |
gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" \
--json 'number' --jq '"number=\(.number)"' \
>> "${GITHUB_OUTPUT}"
# ...
- name: 'Comment on PR with preview link'
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ steps.pr-context.outputs.number }}
header: preview-comment
recreate: true
message: |
[Documentation preview for this PR](${{ steps.preview-netlify.outputs.deploy-url }}) is ready! :tada: If the |
Beta Was this translation helpful? Give feedback.
-
I almost thought I had a breakthrough, but alas of course GitHub's API inconsistencies ruin it. You can make a request like so to the Github API to show PRs associated with a given commit:
For this, we need to use the owner of the repo that is attempting to merge the PR, not our own. The sha/owner are easy enough: It would be especially nice if this worked since it still seems to find the pull request even if there's been another commit since. The problem, however, is that this does not seem to work consistently. I am still trying to work out why this seems to be happening. It seems to happen on a per-user basis, so I think it may somehow be related to user activity settings somehow? (though I do see the PR on their wall still in these cases). I have also personally only observed it on people contributing to the organization for the first time. But then I stumbled on something kinda interesting. It turns out that GitHub actually adds a ref for each pull request under
You then have the number with 0 API calls and obtained from your own repo. The caveat here though is that this will fail if there have been more commits on the PR. In some contexts though that might be desirable. You may also want to add some logic for the unlikely, but technically possible, case that you end up with people opening PRs for the same changes for some reason. |
Beta Was this translation helpful? Give feedback.
-
Esse problema ocorre porque, por motivos de segurança, o GitHub Actions limita as informações disponíveis no evento workflow_run quando a origem do PR é um repositório bifurcado (forked repo). Por padrão, ações que interagem com repositórios de terceiros são mais restritas para evitar possíveis abusos ou comprometimentos. Aqui estão as informações principais e possíveis soluções: Motivo do Problema Quando o PR vem de um fork, você pode usar github.event.workflow_run.head_repository para determinar o repositório de origem e recuperar informações adicionais via API REST. steps:
No workflow pull_request, defina o número do PR como uma saída (output) e use-o no workflow workflow_run. jobs: jobs: Se as informações esperadas no evento workflow_run estiverem ausentes, use a API para buscar os detalhes do PR diretamente: curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" |
Beta Was this translation helpful? Give feedback.
-
2024-03-06 EDIT
Issue not solved. Workarounds are available below. I'll unmark my answer since others have come up with other solutions. This will also hopefully get GitHub to work on a native fix for this problem, which is needed to do tasks like commenting on a PR from a 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 specificpull_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 thegithub.event
context is populatedLink 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 thegithub.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)
workflow_run
workflow (example of what I’m trying to do)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 theworkflow_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 theworkflow_run
workflow afterwardsBeta Was this translation helpful? Give feedback.
All reactions