4

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
        }
      }
    }
  }
}
semanser
  • 2,310
  • 2
  • 15
  • 33

2 Answers2

10

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 {}
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
0

@tharinduwijewardane

JFYI, you can download a zip of a specific branch by this query

repository(owner: "owner", name: "repo name") {
  object(expression: "branch") {
    ... on Commit {
      zipballUrl
    }
  }
}
MartinZ
  • 175
  • 2
  • 12