Reading Repository Contents

schema-request

#1

I’m looking to satisfy a few security related requirements to source control suing the GraphQL API. There are 3 distinct things I need to obtain from the graph.

  1. I need a way to read the files and paths within a repository.

  2. I need a method to read the contents of a file. I tried doing the following, but it wasn’t working as I received nulls back:

    query{
    viewer {
    login
    name
    repository(name:“myrepo”) {
    id
    descriptionHTML
    object(expression: “branch:readme.md”) {
    id
    }
    }
    }
    }
    }

  3. I need a method of traversing the commits to see what has changed with each commit that occurred between two dates. The changes may or may not be in a pull request.

I’m assuming this is possible but that I’m approaching these queries from the root level or the repository level and I need to be looking at it differently. Thanks in advance!

(Note: I’ve originally posted this at SO at https://stackoverflow.com/questions/46227669/github-graphql-to-read-repository-contents)


#2

Hi @jasongaylord!

I’ve got a quick and easy solution for number two here:

{
  repository(owner: "nickvanw", name:"ircx") {
    object(expression: "master:README.md") {
      id
      ... on Blob{
        text
      }
    }
  }
}

Instead of going through viewer, I’ve used the root repository which allows you to look up a repository by the owner and name. The expression here looks for the contents of README.md in master and returns the text in the Blob. You should be able to play around with it more to get additional information if you need it.

Unfortunately, I’m not sure that we’ve implemented enough in GraphQL to provide you all of the information that you need, we’ve received other requests for reading all of the files and paths (here), but it has not been implemented externally as of yet. I would recommend dropping back to our v3 REST API right now if you need that information.

Sorry I couldn’t be more helpful for everything - let me know if there is anything that I missed.