简体   繁体   English

如何通过 GraphQL API 搜索下载 Github 存储库?

[英]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.我想进行一些数据研究,并想使用 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 :您可以使用以下命令在存储库的默认分支上获取最新提交的 tarball/zipball url:

{
  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 , & :使用下载该搜索的所有 zip 的脚本:

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

JFYI, you can download a zip of a specific branch by this query JFYI,您可以通过此查询下载特定分支的 zip

repository(owner: "owner", name: "repo name") {
  object(expression: "branch") {
    ... on Commit {
      zipballUrl
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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