如何使用GitHub动作在PR中添加评论

发布于 2025-02-03 15:38:55 字数 2528 浏览 2 评论 0原文

在github api中,有一个 api 创建问题评论。我想在GitHub Action CI/CD管道中使用它。

我创建工作流程。

name: Workflow

on:
  push:
    branches:
    - main
  pull_request:

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Run jest coverage and log the result to GitHub summary
        run: |
          npm run test-coverage >> $GITHUB_STEP_SUMMARY
          echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV
          echo "" >> $GITHUB_STEP_SUMMARY

      - name: Post jest coverage comments
        env:
          PR_NUMBER: ${{ github.event.pull_request.number }}
          REPO: ${{ github.repository }}
          SUMMARY_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
        run: |
          curl \
            -X POST \
            -H "Accept: application/vnd.github.v3+json" \
            -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
            https://api.github.com/repos/$REPO/issues/$PR_NUMBER/comments \
            -d '{"body":"# Job Summary\n'$SUMMARY_URL'\n'$UNITTESTCOVERAGE'\n"}'

  deploy:
    ...

GitHub机器人没有显示开玩笑的覆盖结果,而是添加了评论。

Job Summary
...

/home/runner/work/_temp/_runner_file_commands/step_summary_d0****01-f**9-4**7-b**7-f0******dc3f

如何获得实际结果?


更新1

我删除了无关的步骤以清楚地表明它。

响应


更新2

i SET jest -coverage以文本格式创建覆盖范围报告(以及其他格式,在这里不重要),我想创建评论以显示文本格式报告。

package.json中,我添加了npm运行测试覆盖运行jest coverage Report。因此,npm运行测试覆盖将在终端中打印结果。

我使用npm运行测试覆盖>> $ github_step_summary将结果重定向到 github步骤摘要,因此我可以在 github步骤摘要中看到覆盖范围报告。

我想使用GitHub API来创建评论,以在PR中显示覆盖范围报告。我使用echo“ unittest -coverage = $ github_step_summary”>> $ github_env将环境变量设置unitTest -coverage$ github_step_summary unittest -coverage可以获取覆盖范围。

但是,步骤发布开玩笑评论无法获得覆盖范围报告。 Github API创建的评论代替了报告,显示了文件 /目录路径。

如何将覆盖范围报告添加到GitHub API的评论中?

In GitHub api, there is an api to create an issue comment. I want to use it in the GitHub actions CI/CD pipeline.

I create the workflow.

name: Workflow

on:
  push:
    branches:
    - main
  pull_request:

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Run jest coverage and log the result to GitHub summary
        run: |
          npm run test-coverage >> $GITHUB_STEP_SUMMARY
          echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV
          echo "" >> $GITHUB_STEP_SUMMARY

      - name: Post jest coverage comments
        env:
          PR_NUMBER: ${{ github.event.pull_request.number }}
          REPO: ${{ github.repository }}
          SUMMARY_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
        run: |
          curl \
            -X POST \
            -H "Accept: application/vnd.github.v3+json" \
            -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
            https://api.github.com/repos/$REPO/issues/$PR_NUMBER/comments \
            -d '{"body":"# Job Summary\n'$SUMMARY_URL'\n'$UNITTESTCOVERAGE'\n"}'

  deploy:
    ...

Instead of showing the jest coverage result, GitHub bot add the comment.

Job Summary
...

/home/runner/work/_temp/_runner_file_commands/step_summary_d0****01-f**9-4**7-b**7-f0******dc3f

How to do get the actual result?


Update 1

I removed unrelated steps to make it clear.

Response

Response from GitHub api


Update 2

I set jest --coverage to create coverage report in text format (and other formats, not important here), and I want to create a comment to show the text format report.

In package.json, I added npm run test-coverage to run Jest coverage report. Therefore, npm run test-coverage will print the result in terminal.

I used npm run test-coverage >> $GITHUB_STEP_SUMMARY to redirect the result to GitHub step summary, so I could see the coverage report in GitHub step summary.

I wanted to use GitHub api to create a comment to show the coverage report in the PR. I used echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV to set environment variable UNITTESTCOVERAGE to be $GITHUB_STEP_SUMMARY, and I wished UNITTESTCOVERAGE can get the coverage report.

However, the step Post jest coverage comments could not get the coverage report. Instead of the report, the comment created by GitHub api showed a file / directory path.

How to add the coverage report to a comment by GitHub api?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2025-02-10 15:38:56

环境变量$ github_step_summary是文件名。

您的代码的作用:

  • npm运行测试覆盖>> $ github_step_summary
    • 将覆盖报告写入步骤摘要文件
  • echo“ unittest -coverage = $ github_step_summary”>>> $ github_env
    • 将环境变量设置UnitTest -coveragegithub_step_summary(fileName)
    • 相同

  • echo“”>> $ github_step_summary
    • 将空线附加到步骤摘要

,您可能想要的是将文件的内容设置为“步骤摘要env变量”作为新ENV变量:

echo:echo echo“ unittest -coverage = $ (cat $ github_step_summary)”>> $ github_env

The environment variable $GITHUB_STEP_SUMMARY is a filename.

What your code does:

  • npm run test-coverage >> $GITHUB_STEP_SUMMARY
    • write the coverage report into the step summary file
  • echo "UNITTESTCOVERAGE=$GITHUB_STEP_SUMMARY" >> $GITHUB_ENV
    • set the environment variable UNITTESTCOVERAGE to the same as GITHUB_STEP_SUMMARY (a filename)
  • echo "" >> $GITHUB_STEP_SUMMARY
    • append empty line to the step summary

What you probably want, however, is to put set the contents of the file behind the step summary env variable as the new env variable:

echo "UNITTESTCOVERAGE=$(cat $GITHUB_STEP_SUMMARY)" >> $GITHUB_ENV

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文