Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better progress output, Github API rate limit #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions migrate-lh-to-gh.rb
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -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'
Expand All @@ -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}"


# -----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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||=""
Expand All @@ -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
Expand All @@ -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}")
}
}

sleep 3 # avoid going over github API rate limit
}