【问题标题】:GitHub API v4 - Get contributors from specific repositoryGitHub API v4 - 从特定存储库获取贡献者
【发布时间】:2018-09-24 13:52:53
【问题描述】:

我正在尝试使用 GitHub API v4 (GraphQL) 构建查询以获取贡献者的数量。

目前我有类似的东西

query ($owner: String!, $name: String!) {
  repository(owner: $owner, name: $name) {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          history(first: 100) {
            nodes {
              author {
                name
              }
            }
            pageInfo {
              hasNextPage
              endCursor
            }
          }
        }
      }
    }
  }
}

我正在检查所有提交并获取作者的姓名(当时我试图获取贡献者的提交数量),但是对于具有大量提交的存储库,这需要很多时间。

回到我的问题,是否有一种方法只获取存储库中贡献者的数量?

【问题讨论】:

标签: graphql github-api github-graphql


【解决方案1】:

据我所知,这只能通过 REST API (v3) 实现,即每页仅请求一项,并提取响应标头中的总页数。

【讨论】:

    【解决方案2】:

    通过这个 graphql 查询,您可以获得: - 总发布数 - 总分支数 - 提交总数

    query{
      repository(owner:"kasadawa", name:"vmware-task") {
    
        Releases:refs(first: 0, refPrefix: "refs/tags/") {
          totalCount
        }
        Branches:refs(first: 0, refPrefix: "refs/heads/") {
          totalCount
    
        }
    
        object(expression:"master") {
    
          ... on Commit {
    
            history {
              totalCount
            }
          }
        }
      }
    }
    

    但是如果你想获得贡献者,你应该用 REST API 来做,因为目前没有简单的方法来用 GRAPHQL API 来实现它。

    这是一个使用 REST API 的解决方案。

    
    const options = {
        url: 'https://api.github.com/repos/vmware/contributors' ,
        'json': true ,
        headers: { 
            'User-Agent':'request',
             'Authorization': 'token API_KEY_GENERATED_FROM_GITHUB'
            }
        };
    
    
    var lastPageNum = 0 ; // global variable 
    
    request.getAsync(options).then((res) =>{
    
            this.checkHeaders(res.headers.status) // just check the header status 
    
            // check if there are more pages 
            if(!!res.headers.link){
    
                const [ , lastURL] = res.headers.link.split(','); // getting the last page link from the header 
                lastPageNum = +lastURL.match(/page=(\d+)/)[1]; // get the number from the url string
                options.url = licenseUrl + lastPageNum; 
                return request.getAsync(options) // make Request with the last page, in order to get the last page results, they could be less than 30
            }else{
                // if its single page just resolve on to the chain 
    
                return Promise.resolve(res.body.length);
            }
    
        })
        .then((lastPageRes)=>{
    
            return (lastPageNum !== 0 
                ? ( (lastPageNum-1)*30 + lastPageRes.body.length ) 
                : lastPageRes)
    
            })
        .catch() // handle errors 
    
    
    

    检查更新:https://github.community/t5/GitHub-API-Development-and/Get-contributor-count-with-the-graphql-api/td-p/18593

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2014-01-09
      • 2021-12-10
      • 2018-07-28
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 2012-11-06
      相关资源
      最近更新 更多