diff --git a/migrate-lh-to-gh.rb b/migrate-lh-to-gh.rb index 8925a93..9a56f03 100755 --- a/migrate-lh-to-gh.rb +++ b/migrate-lh-to-gh.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # Created by Thomas Balthazar, Copyright 2009 # This script is provided as is, and is released under the MIT license : http://www.opensource.org/licenses/mit-license.php # more information here : http://suitmymind.com/2009/04/18/move-your-tickets-from-lighthouse-to-github/ @@ -7,6 +8,19 @@ require 'yaml' require 'uri' +# just pass the cmd string to curl, but run it silently and print the +# response if there was an error +def curl(cmd) + begin + gh_ret = `curl -s #{cmd}` + rescue Exception + warn "Request failed:\n" + warn gh_return_value + raise + end + return gh_ret +end + # ----------------------------------------------------------------------------------------------- # --- Lighthouse configuration LIGHTHOUSE_ACCOUNT = 'YOUR_ACCOUNT_NAME' @@ -22,12 +36,13 @@ GITHUB_LOGIN = "YOUR_ACCOUNT_NAME" GITHUB_API_TOKEN = "YOUR_API_TOKEN" GITHUB_PROJECT = "YOUR_GITHUB_PROJECT_NAME" +GITHUB_ORG = "YOUR_ORGANIZATION_NAME" # do not modify -GITHUB_NEW_ISSUE_API_URL = "https://github.com/api/v2/yaml/issues/open/#{GITHUB_LOGIN}/#{GITHUB_PROJECT}" -GITHUB_ADD_LABEL_API_URL = "https://github.com/api/v2/yaml/issues/label/add/#{GITHUB_LOGIN}/#{GITHUB_PROJECT}" -GITHUB_ADD_COMMENT_API_URL = "https://github.com/api/v2/yaml/issues/comment/#{GITHUB_LOGIN}/#{GITHUB_PROJECT}" +GITHUB_NEW_ISSUE_API_URL = "https://github.com/api/v2/yaml/issues/open/#{GITHUB_ORG}/#{GITHUB_PROJECT}" +GITHUB_ADD_LABEL_API_URL = "https://github.com/api/v2/yaml/issues/label/add/#{GITHUB_ORG}/#{GITHUB_PROJECT}" +GITHUB_ADD_COMMENT_API_URL = "https://github.com/api/v2/yaml/issues/comment/#{GITHUB_ORG}/#{GITHUB_PROJECT}" # ----------------------------------------------------------------------------------------------- @@ -63,6 +78,8 @@ assignee = versions.last.assigned_user_name unless versions.last.attributes["assigned_user_name"].nil? # why gsub? -> curl -F 'title=@xxx' -> 'title= @xxx' cause =@xxx means xxx is a file to upload http://curl.haxx.se/docs/manpage.html#-F--form + puts "migrating issue \##{ticket.id} '#{ticket.title}'\n"; + title = ticket.title.gsub(/^@/," @") body = versions.first.body.gsub(/^@/," @") unless versions.first.body.nil? body||="" @@ -81,16 +98,22 @@ versions.delete_at(0) # create the GH issue and get its newly created id - gh_return_value = `curl -F 'login=#{GITHUB_LOGIN}' -F 'token=#{GITHUB_API_TOKEN}' -F 'title=#{title}' -F 'body=#{body}' #{GITHUB_NEW_ISSUE_API_URL}` - gh_issue_id = YAML::load(gh_return_value)["issue"]["number"] - + gh_return_value = curl("-F 'login=#{GITHUB_LOGIN}' -F 'token=#{GITHUB_API_TOKEN}' -F 'title=#{title}' -F 'body=#{body}' #{GITHUB_NEW_ISSUE_API_URL}") + + begin + gh_issue_id = YAML::load(gh_return_value)["issue"]["number"] + rescue Exception + warn "Failed to parse github result:\n#{gh_return_value}" + next + end + # add comments to the newly created GH issue versions.each { |version| # add the LH comment title to the comment comment = "**#{version.title.gsub(/^@/," @").gsub(/'/,"’")}**\n\n" comment+=version.body.gsub(/^@/," @").gsub(/'/,"’") unless version.body.nil? comment+="\n\n by " + version.user_name.gsub(/^@/," @").gsub(/'/,"’") unless version.user_name.nil? - `curl -F 'login=#{GITHUB_LOGIN}' -F 'token=#{GITHUB_API_TOKEN}' -F 'comment=#{comment}' #{GITHUB_ADD_COMMENT_API_URL}/#{gh_issue_id}` + curl("-F 'login=#{GITHUB_LOGIN}' -F 'token=#{GITHUB_API_TOKEN}' -F 'comment=#{comment}' #{GITHUB_ADD_COMMENT_API_URL}/#{gh_issue_id}") } # here you can specify the labels you want to be applied to your newly created GH issue @@ -110,6 +133,8 @@ gh_labels.each { |label| # labels containing . do not work ... -> replace . by • label.gsub!(/\./,"•") - `curl -F 'login=#{GITHUB_LOGIN}' -F 'token=#{GITHUB_API_TOKEN}' #{GITHUB_ADD_LABEL_API_URL}/#{URI.escape(label)}/#{gh_issue_id}` + curl("-F 'login=#{GITHUB_LOGIN}' -F 'token=#{GITHUB_API_TOKEN}' #{GITHUB_ADD_LABEL_API_URL}/#{URI.escape(label)}/#{gh_issue_id}") } -} \ No newline at end of file + + sleep 3 # avoid going over github API rate limit +}