Skip to content

Instantly share code, notes, and snippets.

@webcracy
Created October 12, 2014 20:24
Show Gist options
  • Save webcracy/4fb605cadd666dc34df4 to your computer and use it in GitHub Desktop.
Save webcracy/4fb605cadd666dc34df4 to your computer and use it in GitHub Desktop.
Export GitHub issues to CSV
#! /usr/bin/env ruby
# coding: UTF-8
require 'rubygems'
require 'octokit'
## SETUP
# Generate a token in https://github.com/settings/tokens/new
# Only the `repo` scope is needed
GH_TOKEN = ENV["GH_TOKEN"] || "<your auth token>" # put your token in the file or use ENV var
GH_REPO = ARGV[0] || "account/repo" # setup a default repo and/or pass repo as argument
CSV_SEPARATOR = ";"
CSV_FILENAME = "#{GH_REPO.split("/").last}_issues-#{Time.now.strftime("%Y-%m-%d-%Hh%M")}.csv"
## USAGE
## `$ gem install octokit`
## `$ chmod +x issues_to_csv.rb`
## `$ ./issues_to_csv.rb youruser/yourrepo`
Octokit.auto_paginate = true
# State the fields we want in our CSV export
wanted_fields = [:number, :state, :milestone, :title, :body]
# Fetch issues from GitHub
issues = Octokit.issues GH_REPO, :state => "all", :access_token => GH_TOKEN
# Collect issues formatted for the CSV export
csv = issues.collect do |issue|
# Fetch and format the value of our wanted fields
wanted_fields.collect do |field|
case field
when :body # Remove quotes from content and wrap the whole content in quotes instead
"\"#{issue[field].gsub("\"", "'")}\""
when :milestone # A milestone is an object and we need its `:title` property
issue[field].title if issue[field]
else # Regular fields are just strings
issue[field].to_s
end
end.join(CSV_SEPARATOR) # Join the formatted values with the separator of our choice
end
# Add header columns to CSV array
csv.unshift wanted_fields.collect(&:capitalize).join(";")
# Convert the CSV array to a string
csv_string = csv.join("\n")
# Write the CSV file
IO.write CSV_FILENAME, csv_string
puts "OK! Issues from '#{GH_REPO}' successfully exported to #{CSV_FILENAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment