Search code examples
rubygithub-apioctokit

How to fetch all the deployed commits from octokit github client for ruby?


I am using the ruby Ocktokit to fetch deployments (list_deployments). At present it only lists the latest 30. I need to filter this on the basis of payload and need to access all deployments till date.

The Github Api provides a link in the header to access the next page. Is there something similar in Ocktokit?

client = Octokit::Client.new(
  :access_token => ENV.fetch("GITHUB_TOKEN"),
)

repo = "repo_name"
env  = "env_name"
options = {
    :environment => env,
    :task => "task_name"
  }

deployments = client.deployments(repo, options)

Solution

  • Octokit provides pagination but also auto-pagination.

    You may be able to do something like:

    client.auto_paginate = true
    deployments = client.deployments 'username/repository' # same as list_deployments
    deployments.length
    

    Update: I tested this locally and pagination in this manner, while documented, doesn't work as expected for deployments. You'll need to fetch the deployments manualy.

    The deployment documentation indicated that listing all deployments should be available in the latest version.

    If that doesn't work you may need to do it manually:

    # fetch your first list of deployments
    deployments = client.deployments 'username/repository'
    
    while true
      begin
        deployments.concat client.last_response.rels[:next].get.data
        puts deployments.length
        break if deployments.length > 500
      rescue StandardError
        puts 'quitting'
      end
    end
    
    puts deployments.length