How to use GitHub V3 API to get commit count for a repo?

前端 未结 7 1936
小鲜肉
小鲜肉 2020-12-05 03:26

I am trying to count commits for many large github repos using the API, so I would like to avoid getting the entire list of commits (this way as an example:

相关标签:
7条回答
  • 2020-12-05 03:56

    Simple solution: Look at the page number. Github paginates for you. so you can easily calculate the number of commits by just getting the last page number from the Link header, subtracting one (you'll need to add up the last page manually), multiplying by the page size, grabbing the last page of results and getting the size of that array and adding the two numbers together. It's a max of two API calls!

    Here is my implementation of grabbing the total number of commits for an entire organization using the octokit gem in ruby:

    @github = Octokit::Client.new access_token: key, auto_traversal: true, per_page: 100
    
    Octokit.auto_paginate = true
    repos = @github.org_repos('my_company', per_page: 100)
    
    # * take the pagination number
    # * get the last page
    # * see how many items are on it
    # * multiply the number of pages - 1 by the page size
    # * and add the two together. Boom. Commit count in 2 api calls
    def calc_total_commits(repos)
        total_sum_commits = 0
    
        repos.each do |e| 
            repo = Octokit::Repository.from_url(e.url)
            number_of_commits_in_first_page = @github.commits(repo).size
            repo_sum = 0
            if number_of_commits_in_first_page >= 100
                links = @github.last_response.rels
    
                unless links.empty?
                    last_page_url = links[:last].href
    
                    /.*page=(?<page_num>\d+)/ =~ last_page_url
                    repo_sum += (page_num.to_i - 1) * 100 # we add the last page manually
                    repo_sum += links[:last].get.data.size
                end
            else
                repo_sum += number_of_commits_in_first_page
            end
            puts "Commits for #{e.name} : #{repo_sum}"
            total_sum_commits += repo_sum
        end
        puts "TOTAL COMMITS #{total_sum_commits}"
    end
    

    and yes I know the code is dirty, this was just thrown together in a few minutes.

    0 讨论(0)
提交回复
热议问题