デモを見るとやりたいことはわかると思う。
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をたたくようにした。
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)"` }