How to download Github repositories via GraphQL API search?

不打扰是莪最后的温柔 提交于 2019-12-12 11:25:47

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/47458143/how-to-download-github-repositories-via-graphql-api-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!