Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        }
      }
    }
  }
}
like image 301
semanser Avatar asked Dec 23 '22 12:12

semanser


1 Answers

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,jq & xargs :

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 {}
like image 117
Bertrand Martel Avatar answered Dec 26 '22 10:12

Bertrand Martel