Skip to content

Instantly share code, notes, and snippets.

@joepindar
Forked from MichaelCurrin/README.md
Created July 5, 2020 12:52
Show Gist options
  • Save joepindar/c0099e41b30ba9231fa672ba39599eac to your computer and use it in GitHub Desktop.
Save joepindar/c0099e41b30ba9231fa672ba39599eac to your computer and use it in GitHub Desktop.
Github GraphQL - Get all file contents in repository

Get Github Files

Get the metadata and contents of all files in a target Github repo using GraphQL

Made for

Github V4 GraphQL API

About the query

I included some useful attributes about files in a Github repo and covered them in the query file. This can be modified to work with any repo you have read access to.

The output includes:

  • blob - a text or binary file.
  • tree - directory path which has no content.

Fields

Notes

  • mode field
    • Usually 16384 or 33188
  • text field
    • From the schema: "UTF8 text data or null if the Blob is binary".
    • Includes "\n" for line breaks. Note your code might have "\n" characters in it too.
  • isBinary field
    • Useful if you want to separate file types or not try and count lines in a binary.
    • Binary might be images or compiled files.

Notes

  • Some extra fields are included but commented out - use them if you want.
  • I could not summary for number of files or find number of lines so you have to work it out yourself.
  • I don't know what else could be used for expression but the docs say "A Git revision expression suitable for rev-parse".

How to use the query

Explorer

Try the query out in the explorer.

  1. Go to the explorer and sign in.
  2. Paste the GQL query in the main pane.
  3. Paste sample JSON into the query variables pane
  4. Press the play button to run.

Command-line

Using curl or a library in Python, Ruby, etc.

Sample output

Simplified JSON output
{
  "entries": [
    {
      "name": ".gitignore",
      "type": "blob",
      "object": {
        "byteSize": 32,
        "text": "node_modules/\npackage-lock.json\n"
      }
    },
    {
      "name": ".vscode",
      "type": "tree",
      "object": {}
    },
    {
      "name": "CONTRIBUTING.md",
      "type": "blob",
      "object": {
        "byteSize": 1520,
        "text": "..."
      }
    }
  ]
}

Resources

query RepoFiles($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
object(expression: "master:") {
... on Tree {
entries {
name
type
#mode
object {
... on Blob {
byteSize
text
# isBinary
}
}
}
}
}
}
}
{ "owner": "MichaelCurrin", "name": "python-twitter-guide" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment