graphqlgithub-apigithub-api-v4

Github GraphQL API Get all commits of a User


I'm trying to use the GitHub GraphQL API to get all the additions made by a user (additions can be found from their commits). I've been able to get the additions from pull requests, but I haven't found a way to do the same for commits. How can I get all the commits from a user?

This is my query (I am new to GraphQL):

query AllAdditions($username: String!, $from: DateTime, $to: DateTime) {
  user(login: $username) {
    name
    contributionsCollection(from: $from, to: $to) {
      commitContributionsByRepository(maxRepositories: 100) {
        repository {
          nameWithOwner
        }
        contributions(first: 30) {
          totalCount
          # I'm trying to get additions like this, but there is no 'commit' field    
          # nodes {
          #   commit {
          #     additions
          #   }
          # }
        }
      }
      pullRequestContributionsByRepository(maxRepositories: 100) {
        repository {
          nameWithOwner
        }
        contributions(first: 30) {
          nodes {
            pullRequest {
              additions
            }
          }
        }
      }
    }
  }
}

Solution

  • The Contributions provided by the API don't contain a reference to the commit and are not really useful for this purpose.

    I ended up with the following query to first get the repositories the user has contributed to and then the commits for each repository where the author email matches.

    Edit: I just found out that the repositoriesContributedTo endpoint only lists the repositories the user has contributed to in the last year. So this approach doesn't really work as intended but still could be useful. More information:

    You can test the query here: https://docs.github.com/de/graphql/overview/explorer.

    query (
      $cursorRepo: String,
      $cursorCommit: String,
      $user: String = "username",
      $emails: [String!] = ["author@example.com", "author@example2.com"]
    ) {
      user(login: $user) {
        repositoriesContributedTo(
          includeUserRepositories: true
          contributionTypes: COMMIT
          first: 100
          after: $cursorRepo
        ) {
          totalCount
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            name
            defaultBranchRef {
              target {
                ... on Commit {
                  history(author: {emails: $emails}, after: $cursorCommit) {
                    totalCount
                    pageInfo {
                      hasNextPage
                      endCursor
                    }
                    nodes {
                      ... on Commit {
                        oid
                        messageHeadline
                        committedDate
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    This works fine for me unless the checked user is a Linux Kernel contributor. That repository causes some problems and I only get HTTP 502 error codes from the server. It works for all the other projects I have contributed to, though.