Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last x commits from Github repo using Github Api V4

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?

like image 294
ssanj Avatar asked Jul 30 '17 07:07

ssanj


Video Answer


2 Answers

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.

like image 128
ssanj Avatar answered Nov 04 '22 16:11

ssanj


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
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
like image 35
VonC Avatar answered Nov 04 '22 17:11

VonC