问题
I need to post comments to GitHub pull request after GitHub action completes, e.g. when a FOSS community member submits a PR. I know that when an action runs from a forked repo, the token has no write access to the parent repo, so it cannot post a comment.
Are there any viable workarounds that people have found for this?
回答1:
I couldn't find any ready-made actions, so here's a complete code to work around GitHub Actions' limitation, written in bash + jq + curl, and calling GitHub API directly.
- Pull request action creates an
.md
file with Github markdown comment content, and saves it as an artifact under some name. This action runs in the context of the forked repo, so it has no way to post a PR comment. - A regular cron job looks at all the open pull requests and recently completed action runs, looks for the posted artifacts, and copies their content as comments to the corresponding pull requests, updating existing comment on repeated runs.
The cron's job in-depth steps are:
- get all open pull requests
- get all recent workflow runs
- match pull requests and their current SHA with the last workflow run for the same SHA
- for each found match of
<pull-request-number>
and<workflow-run-id>
:- download artifact from the workflow run -- expects a single file with markdown content
- look through existing PR comments to see if we have posted a comment before (uses a hidden magical header to identify our comment)
- either create or update the comment with the new text (if changed)
来源:https://stackoverflow.com/questions/61419552/workaround-to-post-comments-from-github-actions-from-forked-repos