How to query for all commits visible to a user?


#1

Hi! I’m trying to do something similar to the Github timeline view on Github’s front page.

In the context of a user, I want to fetch all commits in createdAt desc order.

How can I query for that?

I’ve come up with this query, which very roughly works, but it’s hacky – rather than search for all commits, it grabs the most recent ‘n’ commits from every repository, so it will have gaps for branches that have lots of commits. It also hits a limit and starts returning empty nodes if I crank up the numbers of objects requested to something more realistic in order to capture all activity.

{
  user(login: "defunkt") {
    organizations(first: 2) {
      edges {
        node {
          name
          repositories(last: 2, orderBy: {field: PUSHED_AT, direction: DESC}) {
            edges {
              node {
                name
                refs(last: 2, refPrefix: "refs/heads/") {
                  edges {
                    node {
                      id
                      name
                      target {
                        ... on Commit {
                          id
                          url
                          messageHeadline
                          author {
                            name
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Is there a more direct way of searching for all commits visible to a given user?


#2

Hm, not that I can think of off of the top of my head. One improvement to your current query may be using the recently added visibility argument to the repositories connection. Something along the lines of:

{
  user(login: "defunkt") {
    repositories(affiliation: [OWNER, ORGANIZATION_MEMBER, COLLABORATOR], last: 1) {
      edges {
        node {
          name
          refs(last: 1, refPrefix: "refs/heads/") {
            edges {
              node {
                id
                name
                target {
                  ... on Commit {
                    id
                    url
                    messageHeadline
                    author {
                      name
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Which will return all of the commits of all the repositories that defunkt has any affiliation with, and that the currently authenticated user can also see.

Since this isn’t quite what you’re looking for (I can imagine the deep pagination would make this very difficult to work with), I’d suggest creating a new issue, outlining your intended use case and tagging it as a schema request as described in:

That way we may be able to provide a more adequate field or connection in the schema for what you’re looking to do.

I hope this helps,
Brooks