【问题标题】:Github Actions: Auto commenting a console output on a pull requestGithub Actions:自动评论拉取请求的控制台输出
【发布时间】:2021-09-25 08:49:23
【问题描述】:

我正在尝试设置一个 github 操作,以在构建拉取请求后自动评论大小增量。本质上,我需要将“计算代码大小增量”命令的输出包含在根据拉取请求自动创建的注释中。

下面是当前操作的 2 个步骤:

- name: Diff revision
    id: diff_rev
    shell: bash
    working-directory: cobrax
    run: |
      echo "::set-output name=delta_code::$(python3 codesizes.py diff build/zephyr/zephyr.elf)\n"
      
  - name: Auto Comment
    uses: bubkoo/auto-comment@v1
    with:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      pullRequestOpened: >
        ???? @{{ author }}
        Thank you for raising your pull request.
        This is the code delta:
        ${{join(steps.diff_rev.outputs.delta_code, '\n')}}
        Please make sure you have followed our contributing guidelines. We will review it as soon as possible

控制台显示如下结果:

Using git merge-base against master branch    
Comparing code size of current .elf against: 30ac63fd5ea039c7b874a999f928f41220ad883f
    +---------+--------+-------+-------+
    |  Region |  .text | .data |  .bss |
    +---------+--------+-------+-------+
    |   Local | 113298 |  2412 | 31355 |
    | Against | 113298 |  2412 | 31355 |
    |   Delta |      0 |     0 |     0 |
    +---------+--------+-------+-------+\n

你可以清楚地看到,我使用 set-output 来存储结果(或者,我认为这就是它的作用),然后在评论中使用输出变量。但这显然行不通。

bubkoo/auto-comment 动作的输出是:

octokit.reactions.deleteLegacy() is deprecated, see https://developer.github.com/v3/reactions/#delete-a-reaction-legacy
(node:3588) UnhandledPromiseRejectionWarning: HttpError: Not Found
    at /__w/_actions/bubkoo/auto-comment/v1/dist/index.js.cache.js:1:76457
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:3588) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3588) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

并且,注释显示为:“[...] 这是代码增量:针对主分支使用 git merge-base 请确保 [...]”。短语“使用 git merge-base 反对 master 分支确实会附加到评论中,但显然不是整个控制台输出。

我做错了什么,你将如何使用 github 操作来做这件事?

感谢您的帮助。

最好的问候。

【问题讨论】:

    标签: github github-actions pull-request


    【解决方案1】:

    您的问题是输出包含多行,并且只有第一行分配给变量。正如 GitHub 支持社区上的 post 所见,您可以将换行符转义为单行,该行在使用时会自动扩展。要考虑的另一件事是,您应该在 .yml 中留出一行空格,以便稍后在 GitHub 评论中开始新的一行。

    你的 sn-p 应该像这样工作:

    - name: Diff revision
      id: diff_rev
      shell: bash
      working-directory: cobrax
      run: |
        delta=$(python3 codesizes.py diff build/zephyr/zephyr.elf)
        delta="${delta//'%'/'%25'}"
        delta="${delta//$'\n'/'%0A'}"
        delta="${delta//$'\r'/'%0D'}"
        echo "::set-output name=delta_code::$delta"
    
    - name: Auto Comment
      uses: bubkoo/auto-comment@v1
      with:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        pullRequestOpened: >
          ? @{{ author }}
          
          Thank you for raising your pull request.
          
          This is the code delta:
          
          ```
          
          ${{ steps.diff_rev.outputs.delta_code }}
          
          ```
          
          Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
    

    $(git log) 的示例:

    完整的auto-comment.yml:

    name: Auto Comment
    
    on:
      pull_request:
        branches: [ main ]
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
    
          - name: Diff revision
            id: diff_rev
            shell: bash
            run: |
              delta=$(git log)
              delta="${delta//'%'/'%25'}"
              delta="${delta//$'\n'/'%0A'}"
              delta="${delta//$'\r'/'%0D'}"
              echo "::set-output name=delta_code::$delta"
    
          - name: Auto Comment
            uses: bubkoo/auto-comment@v1
            with:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              pullRequestOpened: >
                ? @{{ author }}
                
                Thank you for raising your pull request.
                
                This is the code delta:
                
                ```
                
                ${{ steps.diff_rev.outputs.delta_code }}
                
                ```
                
                Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
    

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2022-10-24
      • 2020-04-09
      • 2017-08-02
      • 2013-04-18
      • 2022-06-21
      • 2020-01-05
      • 1970-01-01
      • 2022-07-13
      相关资源
      最近更新 更多