LoginSignup
0
0

More than 3 years have passed since last update.

Github API v4を使って今日のコミットを1リクエストで取得する

Last updated at Posted at 2020-03-02

あれ、今日何したっけ...?

っていうのもあれなのでGithubに今日の自分の頑張りを聞いてみる。

やってみる

以下を前提として、Github GraphQL Explorerでリクエストしてみる。

  • 今日 = 日本時間 2020/03/02
  • 対象ブランチ = staging
  • 今日、以下のコミットを積んだ
    • Repository1に $ git commit -m "feature: A機能に〇〇を追加"
    • Repository1に $ git commit -m "fix: B機能のバグfix"
    • Repository2に $ git commit -m "refactor: C機能のリファクタリング"
Query
{
      user(login: "<ログインユーザー名>") {
        # (1) 今日ContributeしたRepositoryの取得
        contributionsCollection(from: "2020-03-01T15:00:00Z", to: "2020-03-02T15:00:00Z") {
          commitContributionsByRepository {
            repository {
              name
              ref(qualifiedName: "staging") {
                target {
                  # (2) 今日のCommitの取得
                  ... on Commit {
                    history(
                      first: 100, 
                      author: {emails: ["<ログインユーザーのEmailアドレス>"]},
                      since: "2020-03-01T15:00:00Z",
                      until: "2020-03-02T15:00:00Z"
                    ) {
                      nodes {
                        message
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
結果
{
  "data": {
    "user": {
      "contributionsCollection": {
        "commitContributionsByRepository": [
          {
            "repository": {
              "name": "repository1",
              "ref": {
                "target": {
                  "history": {
                    "nodes": [
                      {
                        "message": "fix: B機能のバグfix"
                      },
                      {
                        "message": "feature: A機能に〇〇を追加"
                      },
                    ]
                  }
                }
              }
            }
          },
          {
            "repository": {
              "name": "repository2",
              "ref": {
                "target": {
                  "history": {
                    "nodes": [
                      {
                        "message": "refactor: C機能のリファクタリング"
                      }
                    ]
                  }
                }
              }
            }
          }
        ]
      }
    }
  }
}

ポイントの概説

(1) 今日ContributeしたRepositoryの取得

user.contributionsCollection.commitContributionsByRepository
でたどることができる

fromtoパラメーターで範囲を指定できるが、2020-03-01T15:00:00Zの形式に準拠してないとエラーになる。

(2) 今日のCommitの取得

repository.ref.targetとたどり、CommitInline Fragmentで取得する

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