Github 操作会在我的 fork 上发布评论,但不会在原始 repo 上发布评论。为什么?

时间:2021-04-20 07:29:29

标签: github token benchmarking github-actions

有一个 githubs actions 比较每个 PR 的基准性能之间的差异。 gh 操作回购链接 - https://github.com/smrpn/criterion-compare-action/tree/move_to_actions

这是负责发表评论的代码-

const resultsAsMarkdown = convertToMarkdown(myOutput);

  // An authenticated instance of `@octokit/rest`
  const octokit = github.getOctokit(myToken);

  const contextObj = { ...context.issue };

  try {
    await octokit.issues.createComment({
      owner: contextObj.owner,
      repo: contextObj.repo,
      issue_number: contextObj.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);
    console.table(resultsAsObject);
    console.log("Failed to comment", e);
    core.debug(e);
    core.debug("Failed to comment");
  }

这将结果作为对我的 repo 分支的评论发布 - comment

但不是在原始回购中 - error

这是我的 .yml 文件 -

on: [pull_request]
name: Benchmarks
jobs:
  runBenchmark:
    name: run benchmark
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          profile: minimal
      - name: Cache cargo
        uses: actions/cache@v2.1.4
        with:
          path: |
            target
            ~/.cargo/git
            ~/.cargo/registry
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - uses: smrpn/criterion-compare-action@move_to_actions 
        with:
          cwd: benches
          token: ${{ secrets.GITHUB_TOKEN }}

我希望这个 PR 在原始 repo 上发表评论。这里的错误是什么?什么是修复?

1 个答案:

答案 0 :(得分:1)

这是一篇博文,详细介绍了从分叉存储库运行 PR 操作时的安全性:https://securitylab.github.com/research/github-actions-preventing-pwn-requests/

该博客文章包含一个示例,说明应如何处理此类操作。在 PR 上构建和基准性能具有潜在危险,因此必须使用 public class DataFetcher { ArrayList<DataType> dataList; FirebaseFirestore firestore = FirebaseFirestore.getInstance(); public ArrayList<DataType> DatabaseCaller(String userInput) { //formulate query based on userInput //hit database and fetch dataList return dataList; } } 触发器来完成;出于安全原因,pull_request 无权添加评论。相反,pull_request 触发器工作流应该执行构建和性能基准测试,然后将结果作为工件上传。

然后将在 pull_request 上触发不同的工作流程。此工作流具有更高的权限(具体来说,它可以添加评论)。它应该下载从 workflow_run 工作流上传的工件并将它们作为评论发布到 PR。