Github Action 无法对 PR 发表评论

问题描述

我正在使用 github 操作来比较基准测试结果并将它们作为对 PR 的评论发布。 这是操作文件 - https://github.com/smrpn/criterion-compare-action/blob/move_to_actions/main.js

它说 -

try {
    await octokit.issues.createComment({
      ...context.repo,issue_number: context.payload.pull_request.number,body: resultsAsMarkdown,});
  } catch (e) {
    // If we can't post to the comment,display results here.
    // forkedRepos only have READ ONLY access on GITHUB_TOKEN
    // https://github.community/t5/GitHub-Actions/quot-Resource-not-accessible-by-integration-quot-for-adding-a/td-p/33925
    const resultsAsObject = convertToTableObject(myOutput);
    
    fs.writeFile('benchResults.txt',resultsAsObject,(err) => {
        if (err) throw err;
    });
    console.table(resultsAsObject);
    console.log("Failed to comment\n",e);
    core.debug(e);
    core.debug("Failed to comment");
  }

我正在使用为此目的制作的另一个令牌(评论基准测试结果)- BENCHMARK_TOKEN。 但是当 PR 来自分叉回购时,它不会发表评论。 这就是现在的问题 - https://github.com/hackerchai/casbin-rs/runs/2648902413#step:5:614

修复是什么? 我想要 github bot 对每个 PR 的评论

解决方法

您可以使用 pull_request_target 事件评论在分叉上打开的拉取请求。查看更多详情here

请注意,为了不运行不安全的代码,它在 fork 的基础 上运行。 这是example of using the event

这是来自 GitHub 文档的警告:

警告: pull_request_target 事件被授予读/写存储库令牌并且可以访问机密,即使它是从分叉触发的。尽管工作流在拉取请求基础的上下文中运行,但您应该确保您不会通过此事件检出、构建或运行拉取请求中不受信任的代码。此外,任何缓存都与基本分支共享相同的范围,并且为了帮助防止缓存中毒,如果缓存内容可能被更改,则不应保存缓存。有关更多信息,请参阅 GitHub 安全实验室网站上的“保持 GitHub 操作和工作流程安全:防止 pwn 请求”。