10

I'm trying to use the new Github GraphQL api (v4) and I don't seem to be able to figure out how to get the last x commits for master. I've used repository and ref but they still don't give me what I need.

The query below almost gives me what I need:

query{
  repository(owner: "typelevel", name: "cats") {
    refs(refPrefix:"refs/heads/", last: 5) {
      edges{
        node {
          associatedPullRequests(states: MERGED, last: 5) {
            edges{
              node {
                title
                baseRef {
                  name
                  prefix
                }
                baseRefName
                commits(last: 10) {
                  edges {
                    node {
                      commit {
                        abbreviatedOid
                        message
                        
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

but:

  1. doesn't seems to exactly match what is in the repo
  2. limited to PRs
  3. seems too unwieldy

I also tried using defaultBranchRef but that didn't work either:

query{
  repository(owner: "typelevel", name: "cats") {
    defaultBranchRef {
      name
      prefix
      associatedPullRequests(states: [MERGED], last: 5) {
        edges {
          node {
            title
          }
        }
      }
    }
  }
}

I've been testing the queries using the explorer app on the Github api page.

Any ideas?

3 Answers 3

15

I was able to get this working with:

query {
  repository(owner: "typelevel", name: "cats") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          history(first: 10) {
            pageInfo {
              hasNextPage
              endCursor
            }
            edges {
              node {
                oid
                messageHeadline
              }
            }
          }
        }
      }
    }
  }
}

by modifying this query linked to on the Github Platform Community.

Sign up to request clarification or add additional context in comments.

3 Comments

Good catch, probably easier than passing an id. +1
both of the links went 404 now.
@LukAron Such is the internet I guess. The Github Platform Community should be somewhere within: github.community I think. I tried searching for the previous post there but couldn't find anything close to it.
2

Would using history be better in this case?

See this thread

A "ref" (short for reference) is anything that points to a git commit. This could be a local branch, a tag, a remote branch, etc. So master, for example, would be considered a ref.

In that vein, you can use the ref field on the Repository type to get a reference that targets a commit.
From that commit, you can get all of the commit's parents. If you target master, you can get the main history of the git repository.

query {
  node(id: "MDEwOlJlcG9zaXRvcnk4NDM5MTQ3") {
    ... on Repository {
      ref(qualifiedName: "master") {
        target {
          ... on Commit {
            id
            history(first: 30) {
              totalCount
              pageInfo {
                hasNextPage
              }

              edges {
                node {
                  oid
                  message
                  author {
                    name
                    email
                    date
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

2 Comments

Interesting syntax! I didn't know you could do that. I'm not sure how to get the node Id for master in this case?
@ssanj many object has an ID , docs.github.com/en/free-pro-team@latest/graphql/reference/…, any object that implement node interface will have the ID, to find the ID of your object, you have to find it first, other than ID, you could use some key to find them, for example , to find a repository, you can do a query with starting point repository(owner: "typelevel", name: "cats"){ id } in this way you get the ID
0

Here's the shortest way that works on any default branch even if it's not named master

Last 10 commits

{
  repository(owner: "fregante", name: "webext-fun") {
    defaultBranchRef {
      target {
        ... on Commit {
          history(first: 10) {
            nodes {
              oid
            }
          }
        }
      }
    }
  }
}

Last 1 commit (HEAD)

{
  repository(owner: "fregante", name: "webext-fun") {
    defaultBranchRef {
      target {
        oid
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.