1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHub GraphQL API からコミット・PR数を取得する

Last updated at Posted at 2022-04-16

「最近チームが開発に時間をあてられているのかなぁ」と思い、GitHub からコミットやPullRequest数を取得してみようと思い立った。

この記事では

「GitHubのGraphQL APIを使って特定のorganization x メンバーの欲しい情報(コミット数やPullRequest数など)を取得する」

を目的に、実際に試した手順を紹介します。

確認環境

  • Mac OS
  • GraphQL Playground (version 1.8.10)

GraphQL の実行環境を用意する

実行環境は GraphQL Playground を利用します。

https://github.com/graphql/graphql-playground

GitHub の GraphQL を呼び出すために必要なエンドポイントや Personal access token の取得方法は、公式ドキュメントから確認します。
https://docs.github.com/ja/graphql/guides/forming-calls-with-graphql

セットアップが完了し、実際に GraphQL を実行する際には次のような画面になります。

GitHub Graph QL の認証・認可を通すためには、左下の「HTTP HEADERS」に次の様な形式のJSONを指定します。
ghp_... の部分は GitHub の Personal access token が指定されます。

{
  "Authorization": "bearer ghp_..."
}

GraphQL の仕様を確認する

Playground には GraphQLのエンドポイントから API の仕様を取得する機能が備わっている。
中央右隅の「DOCS」からクエリやオブジェクトを検索することができます。
検索フォームに commitPullRequest などを打ち込んで必要なオブジェクトやクエリの形式を確認します。

より詳細な仕様は公式サイトでも提供されています。

https://docs.github.com/ja/graphql/reference

GraphQL からコミット・PR数などを取得する

実際にコミット数やPR数を取得するためのクエリを紹介しますので、是非自分のGitHubでの活動を取得してみてください。

特定アカウントのコミット数・PR数・PRレビュー数を取得する
user.contributionsCollection で取得可能。デフォルトの取得範囲は1年間。
https://docs.github.com/ja/graphql/reference/objects#user

{
  user(login: "アカウント名") {
    contributionsCollection {
      totalCommitContributions
      totalPullRequestContributions
      totalPullRequestReviewContributions
    }
  }
}

特定アカウントのコミット数を期間を指定して取得する
from, to のオプションで指定可能。

{
  user(login: "アカウント名") {
    contributionsCollection(from: "2022-03-01T00:00:00" to: "2022-04-01T00:00:00") {
      totalCommitContributions
    }
  }
}

特定アカウントのOrganizationのコミット数を取得する
organizationID のオプションで指定可能。名前ではなくIDな点に注意。

{
  user(login: "アカウント名") {
    contributionsCollection(organizationID: "MDEyO...................") {
      totalCommitContributions
    }
  }
}

OrganaizationIDの取得は以下のクエリで取得できる(Personal access token に read:org 権限が必要)。

{
  organization(login: "Org名") {
    id
  }
}

--

以上で、GitHub の GraphQL の利用例の紹介は終わりです。少しでも参考になれば。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?