3
0

More than 1 year has passed since last update.

GitHub GraphQL APIをつかってGitHubコメントを取得する

Posted at

はじめに

@iron-samurai さんの記事

[Github API] レビューコメントを振り返るために、過去にもらった全コメントを引っこ抜いてJsonで吐き出してみた
を拝見し、自分もGitHubAPIを使って過去のコメントを一気に見たいと思いました。
本記事では、ghコマンドとGraphQLを使ってGitHubのコメントを取得する方法を記載します。

GitHub APIとは?

インテグレーションを作成し、データを取り出し、ワークフローを自動化するために、GitHubのREST APIで構築してください。

GitHubの様々な機能をAPIを使って行えるツールです。

GitHub GraphQL APIとは?

GitHubのGraphQL APIは、柔軟性と、フェッチしたいデータを正確に定義できる機能を提供します。

要はGitHub APIをREST形式ではなく、GraphQLでデータ取得をする方法になります。

環境整備

コマンド ラインから API 要求を行う場合、GitHub では、GitHub CLI を使用することをお勧めされています。
そのため、本記事でもGitHub CLI を使ってコメントを取得します。

# ツールインストール
brew install gh

# 認証
gh auth login

MacOSではない方は以下の中からご自身の環境を選択して、GitHub CLIをインストールしてください。
https://github.com/cli/cli#installation

クエリ

gh api graphql -F owner='組織名orアカウント名' -F name='リポジトリ名' -f query='
query($name: String!, $owner: String!){
  repository(owner: $owner, name: $name) {
    pullRequests(first: 1,orderBy: {field: CREATED_AT, direction: DESC}) {
      nodes {
        url
        title
        createdAt
        author {
          login
        }
        reviews(first: 10) {
          edges {
            node {
              comments(first: 25) {
                edges {
                  node {
                    databaseId
                    bodyText
                    diffHunk
                    createdAt
                    path
                    author {
                      login
                    }
                    replyTo {
                      databaseId
                      bodyText
                      diffHunk
                      createdAt
                      path
                      author {
                        login
                      }
                    }
                  }
                }
              }
            }
          }
        }
        comments(first: 10,orderBy: {field: UPDATED_AT, direction: DESC}) {
          nodes {
            createdAt
            bodyText
          }
        }
      }
    }
  }
}

対応する型

クエリのオブジェクト
repository Repository
pullRequests PullRequestConnection
reviews PullRequestReviewConnection
reviews.comments PullRequestReviewCommentConnection
comments IssueCommentConnection

オブジェクトについて
https://docs.github.com/ja/graphql/reference/objects

nodeとedgeの違い

Node

ノード はオブジェクトの総称です。 ノードは直接ルックアップすることもできますが、コネクションを通じて関連するノードにアクセスすることもできます。 スカラーを返さない node を指定する場合は、すべてのフィールドでスカラーが返されるまでサブフィールドを含める必要があります。

Edge

エッジは、ノード間のコネクションを表します。 コネクションに対してクエリを行うと、そのエッジをトラバースしてノードを取得することになります。 すべての edges フィールドには、node フィールドと cursor フィールドがあります。

commentとreviewコメントの違い

commentはプルリクにつくコメント

reviewのcommentはファイルにつくコメント

参考

ghコマンドのインストール
https://docs.github.com/ja/rest/quickstart?apiVersion=2022-11-28

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0