Skip to content

Instantly share code, notes, and snippets.

@tkarpinski
Forked from henare/github_issues_to_csv.rb
Created April 12, 2012 18:09
Show Gist options
  • Save tkarpinski/2369729 to your computer and use it in GitHub Desktop.
Save tkarpinski/2369729 to your computer and use it in GitHub Desktop.
Exports Github issues to CSV (API v3)
require 'json'
require 'open-uri'
require 'csv'
require 'date'
# Github credentials to access your private project
USERNAME="myusername"
PASSWORD="mypassword"
# Project you want to export issues from
USER="someuser"
PROJECT="someproject"
# Your local timezone offset to convert times
TIMEZONE_OFFSET="+10"
BASE_URL="https://github.com/api/v2/json/issues"
csv = CSV.new(File.open(File.dirname(__FILE__) + "/issues.csv", 'w'))
puts "Initialising CSV file..."
# CSV Headers
header = [
"Summary",
"Description",
"Date created",
"Date modified",
"Issue type",
"Priority",
"Status",
"Reporter"
]
# We need to add a column for each comment, so this dictates how many comments for each issue you want to support
20.times { header << "Comments" }
csv << header
puts "Getting issues from Github..."
closed_issues = JSON.parse(open("#{BASE_URL}/list/#{USER}/#{PROJECT}/closed", 'r', { :http_basic_authentication => [USERNAME, PASSWORD] }).read)
open_issues = JSON.parse(open("#{BASE_URL}/list/#{USER}/#{PROJECT}/open", 'r', { :http_basic_authentication => [USERNAME, PASSWORD] }).read)
all_issues = closed_issues['issues'] + open_issues['issues']
puts "Processing #{all_issues.size} issues..."
all_issues.each do |issue|
puts "Processing issue #{issue['number']}..."
# Work out the type based on our existing labels
case
when issue['labels'].to_s =~ /Bug/i
type = "Bug"
when issue['labels'].to_s =~ /Feature/i
type = "New feature"
when issue['labels'].to_s =~ /Task/i
type = "Task"
end
# Work out the priority based on our existing labels
case
when issue['labels'].to_s =~ /HIGH/i
priority = "Critical"
when issue['labels'].to_s =~ /MEDIUM/i
priority = "Major"
when issue['labels'].to_s =~ /LOW/i
priority = "Minor"
end
# Needs to match the header order above, date format are based on Jira default
row = [
issue['title'],
issue['body'],
DateTime.parse(issue['created_at']).new_offset(TIMEZONE_OFFSET).strftime("%d/%b/%y %l:%M %p"),
DateTime.parse(issue['updated_at']).new_offset(TIMEZONE_OFFSET).strftime("%d/%b/%y %l:%M %p"),
type,
priority,
issue['state'],
issue['user']
]
if issue['comments'] > 0
puts "Getting #{issue['comments']} comments for issue #{issue['number']} from Github..."
# Get the comments
comments = JSON.parse(open("#{BASE_URL}/comments/#{USER}/#{PROJECT}/#{issue['number']}", 'r', { :http_basic_authentication => [USERNAME, PASSWORD] }).read)
comments['comments'].each do |c|
# Date format needs to match hard coded format in the Jira importer
comment_time = DateTime.parse(c['created_at']).new_offset(TIMEZONE_OFFSET).strftime("%m/%d/%y %r")
# Map usernames for the comments importer
comment_user = case c['user']
when "Foo"
"foo"
when "baruser"
"bar"
when "myfunnyusername"
"firstname"
end
# Put the comment in a format Jira can parse, removing #s as Jira thinks they're comments
comment = "Comment: #{comment_user}: #{comment_time}: #{c['body'].gsub('#','')}"
row << comment
end
end
csv << row
end
@tkarpinski
Copy link
Author

this does not reimplement comments from the original gist. that wouldn't be too hard using the gem.

@tkarpinski
Copy link
Author

its also worth noting that this works on both windows and *nix with Ruby 1.9.3

@mikermcneil
Copy link

If you're a complete ruby n00b like me, maybe this'll save you some time.

On a clean Mac with an old version of ruby (1.8.x or something), here's what I did:

# First download the script and save it to: ~/Desktop/github_issues_to_csv.rb
# Then customize it with your credentials and the target repo

# then update ruby
curl -L get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm requirements
rvm install 1.9.3

# install the octokit gem (package containing the github sdk for ruby)
sudo gem install octokit
ruby ~/Desktop/github_issues_to_csv.rb &

# There you have it:
tail -f ~/Desktop/issues3.csv

@jkmaxwell
Copy link

don't sudo gem install!

@kevinpschaaf
Copy link

I updated this to support comment import in my fork here: https://gist.github.com/2962886

Saved a lot of time. Thanks, tkarpinski (and henare).

@mikermcneil
Copy link

mikermcneil commented Jun 21, 2012 via email

@laurencesmithever
Copy link

Hi,

Having a little problem. When I run github_issues_to_csv.rb I get the following:


C:\Ruby193>ruby github_issues_to_csv.rb
Initialising CSV file...
Getting issues from Github...
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:799:in connect': SSL_connect returned=1 e rrno=0 state=SSLv3 read server certificate B: certificate verify failed (Faraday ::Error::ConnectionFailed) from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:799:inblock in connect'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:54:in timeout' from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:99:intimeout'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:799:in connect' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:755:indo_start'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:744:in start' from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1284:inrequest'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1026:in get' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/adapt er/net_http.rb:72:inperform_request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/adapt
er/net_http.rb:37:in call' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday_middleware-0.9.0/lib/fa raday_middleware/response_middleware.rb:30:incall'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/respo
nse.rb:8:in call' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday_middleware-0.9.0/lib/fa raday_middleware/response/follow_redirects.rb:79:inperform_with_redirection'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday_middleware-0.9.0/lib/fa
raday_middleware/response/follow_redirects.rb:65:in call' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/respo nse.rb:8:incall'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday_middleware-0.9.0/lib/fa
raday_middleware/request/encode_json.rb:23:in call' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/conne ction.rb:226:inrun_request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/conne
ction.rb:87:in get' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/octokit-1.22.0/lib/octokit/requ est.rb:61:inrequest'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/octokit-1.22.0/lib/octokit/requ
est.rb:11:in get' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/octokit-1.22.0/lib/octokit/clie nt/issues.rb:42:inlist_issues'
from github_issues_to_csv.rb:42:in `

'


Can you help?

Thanks.

@okolehao
Copy link

okolehao commented Feb 9, 2014

@laurencesmithever probably too late, but you add this line: (note that introduces a security problem)

Octokit.configure do |c|
c.connection_options = { ssl: { verify: false } }
end

@dylanjw
Copy link

dylanjw commented Aug 18, 2015

I get this error:

Initialising CSV file...
Getting issues from Github...
Processing 570 issues...
Processing issue 564...
./github_issues_to_csv.rb:85:in `parse': no implicit conversion of Time into String (TypeError)
    from ./github_issues_to_csv.rb:85:in `block in <main>'
    from ./github_issues_to_csv.rb:55:in `each'
    from ./github_issues_to_csv.rb:55:in `<main>'

@spyhunter99
Copy link

i get the same as @dylanjw

@spyhunter99
Copy link

spyhunter99 commented Apr 6, 2017

It can be fixed by updating the two lines that parse the date time to this

    issue['created_at'],
    issue['updated_at'],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment