Search code examples
gitapigithubgraphqlgithub-api

How to download Github repositories via GraphQL API search?


I want to make some data researches and want to download repositories content from the search results with Github GraphQL API.

What I already found is how to make simple search query, but the question is: How to download repositories content from the search results?

Here is my current code that returns repositories name and description (try to run here):

{
  search(query: "example", type: REPOSITORY, first: 20) {
    repositoryCount
    edges {
      node {
        ... on Repository {
          name
          descriptionHTML
        }
      }
    }
  }
}

Solution

  • You can get the tarball/zipball url for the latest commit on the default branch of a repo with the following :

    {
      repository(owner: "google", name: "gson") {
    
        defaultBranchRef {
          target {
            ... on Commit {
              tarballUrl
              zipballUrl
            }
          }
        }
      }
    }
    

    Using a search query, you can use the following :

    {
      search(query: "example", type: REPOSITORY, first: 20) {
        repositoryCount
        edges {
          node {
            ... on Repository {
              defaultBranchRef {
                target {
                  ... on Commit {
                    zipballUrl
                  }
                }
              }
            }
          }
        }
      }
    }
    

    A script that download all zip of that search using , & :

    curl -s -H "Authorization: bearer YOUR_TOKEN" -d '
    {
        "query": "query { search(query: \"example\", type: REPOSITORY, first: 20) { repositoryCount edges { node { ... on Repository { defaultBranchRef { target { ... on Commit { zipballUrl } }}}}}}}"
    }
    ' https://api.github.com/graphql | jq -r '.data.search.edges[].node.defaultBranchRef.target.zipballUrl' | xargs -I{} curl -O {}