Jacob
Jacob

Reputation: 1835

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

Upvotes: 2

Views: 2264

Answers (2)

frsc
frsc

Reputation: 471

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!] = ["[email protected]", "[email protected]"]
) {
  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.

Upvotes: 1

huzaifamandviwala
huzaifamandviwala

Reputation: 435

As mentioned by lee-dhom in GraphQL - Get all commits by all users:

{
  search(query: "org:test", type: REPOSITORY, last: 100) {
    nodes {
      ... on Repository {
        name
        defaultBranchRef {
          name
          target {
            ... on Commit {
              history(first: 100, since: "2013-07-11T00:00:00") {
                totalCount
                nodes {
                  ... on Commit {
                    committedDate
                    additions
                    author {
                      name
                      email
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

As for getting all orgs, there isn’t currently a way to do that via the GraphQL API because the search connection requires something in the search query. There’s also no top-level connection that allows you to list all users, orgs, or repositories.

I hope that helps!

Upvotes: 0

Related Questions