Pull Requestで最新のコメント以外は最小化するやつ

github.com

デモを見るとやりたいことはわかると思う。

https://github.com/user-attachments/assets/0da3da2f-f649-4563-84bf-b7c7cb7d9f82

terraformのGitHub Actionsのドキュメントでは古いコメントを削除しているが、Atlantisでは --hide-prev-plan-comments オプションで古いコメントを最小化するようになっており、その開発体験がよかったのでほかのツールでも同じようなコメントができるようなCLIを作成した。

使い方

-o にオーナー、-rリポジトリ名、-n にIssue/Pull Request番号を指定し、引数のファイルを読み込んでコメントする。 - を指定すると標準入力を読み込む。

# https://github.com/winebarrel/hello-world/pull/12
echo "hello my comment" | lastcmt -o winebarrel -r hello-world -n 12 -

実装について

コメントの先頭に <!-- lastcmt: {{ id }} --> というMarkdownコメントをつけて、lastcmtからの投稿を識別して最小化している。

GitHub APIについて

なぜかv3にはminimizeCommentのAPIがないのでshurcooL/githubv4でv4 APIをたたくようにした。

github.com

v4 APIだとIssueとPull Requestを単純に同じものとして扱えないので、やや冗長なクエリになった。

https://github.com/winebarrel/lastcmt/blob/main/client.go#L113-L140

   var q struct {
        Repository struct {
            IssueOrPullRequest struct {
                Issue struct {
                    Id       githubv4.ID
                    URL      string
                    Comments struct {
                        Nodes    []comment
                        PageInfo struct {
                            EndCursor   githubv4.String
                            HasNextPage bool
                        }
                    } `graphql:"comments(first: 100, after: $issueCursor)"`
                } `graphql:"... on Issue"`
                PullRequest struct {
                    Id       githubv4.ID
                    URL      string
                    Comments struct {
                        Nodes    []comment
                        PageInfo struct {
                            EndCursor   githubv4.String
                            HasNextPage bool
                        }
                    } `graphql:"comments(first: 100, after: $pullRequestCursor)"`
                } `graphql:"... on PullRequest"`
            } `graphql:"issueOrPullRequest(number: $number)"`
        } `graphql:"repository(owner: $owner, name: $repo)"`
    }