Github Actions + Github CLIでコメントを更新する

Github Actions でコメントを更新する方法として、 marocchino/sticky-pull-request-commentを使う方法があります。 が、Github CLI を使えば自前でできるのでそのメモ (ちょっとめんどくさいけど)

仕組み

  • update したいコメントに invisible な footer を仕込む
  • 仕込んだ footer を探す
  • あれば更新

みたいな感じです。

雰囲気: https://github.com/freyja1103/ci-upsert-comment/pull/1

コメントをさがす

gh api repos/repoName/issues/number/comments で JSON を拾いつつ、コメント ID を抽出するために jq でいい感じにフィルタリングする

  • select だけだと存在しない場合に空の配列が代入されるのでmap() | first // ""で空文字にする
- name: Find existing comment
    id: existing_comment

    run: |
        COMMENT_ID=$(gh api repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments | jq -r --arg footer "$FOOTER" 'map(select(.body | contains($footer)) | .id) | first // ""' | head -n1)

        if [[ -n "$COMMENT_ID" ]]; then
        echo "COMMENT_ID=$COMMENT_ID" >> $GITHUB_ENV
        fi

コメントを更新する

コメント ID があれば PATCH で更新

- name: Update existing comment
    if: env.COMMENT_ID != ''
    run: |
        gh api --method PATCH repos/${{ github.repository }}/issues/comments/"$COMMENT_ID" \
        -f body="
        test (updated)
        ${FOOTER}
        "

普通に投稿する

- name: Post new comment if none exists
    if: env.COMMENT_ID == ''
    run: |
        gh pr comment ${{ github.event.pull_request.number }} --body "
        test
        ${FOOTER}
        "