简体   繁体   中英

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
        }
      }
    }
  }
}

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 {}

@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
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM