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

4

我想进行一些数据研究,并使用Github GraphQL API从搜索结果中下载存储库内容。

我已经找到如何进行简单的搜索查询,但问题是: 如何从搜索结果中下载存储库内容?

下面是当前返回存储库名称和描述的代码(在此处尝试运行):

{
  search(query: "example", type: REPOSITORY, first: 20) {
    repositoryCount
    edges {
      node {
        ... on Repository {
          name
          descriptionHTML
        }
      }
    }
  }
}
2个回答

10

你可以使用以下方法获取仓库默认分支上最新提交的 tarball/zipball URL:

{
  repository(owner: "google", name: "gson") {

    defaultBranchRef {
      target {
        ... on Commit {
          tarballUrl
          zipballUrl
        }
      }
    }
  }
}

使用搜索查询,您可以使用以下内容:

{
  search(query: "example", type: REPOSITORY, first: 20) {
    repositoryCount
    edges {
      node {
        ... on Repository {
          defaultBranchRef {
            target {
              ... on Commit {
                zipballUrl
              }
            }
          }
        }
      }
    }
  }
}
使用curl,jq和xargs下载搜索结果中的所有zip文件的脚本:

使用下载该搜索的所有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 {}

如何获取特定分支的 zip 压缩包? - tharinduwijewardane

0

@tharinduwijewardane

顺便提一下,你可以通过这个查询下载特定分支的zip文件。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接