Skip to content

Instantly share code, notes, and snippets.

@llighterr
Created February 12, 2015 13:13
Show Gist options
  • Save llighterr/95d3b055acb8cf6aa308 to your computer and use it in GitHub Desktop.
Save llighterr/95d3b055acb8cf6aa308 to your computer and use it in GitHub Desktop.
# ruby version used 1.9.3
require 'rubygems'
require 'active_resource'
require 'ghee' # GitHub's API gem
GH_LOGIN = '' # set to yours
GH_PASSWORD = '' # set to yours
GH_REPOSITORY_OWNER = 'john'
GH_REPOSITORY_NAME = 'application'
RM_SITE_URL = 'https://54.209.57.192'
RM_LOGIN = 'ihor'
RM_PASSWORD = 'pass' # is needed for Redmine API authorization
RM_PROJECT_NAME = 'Project'
SIMPLE_PASSWORD = 'kX45mrrTs' # is needed to create users in Redmine
STATE_HASH = { 'open' => 1, 'closed' => 5 }
LABELS = 'labels'
NAME = 'name'
LOGIN = 'login'
USER = 'user'
BODY = 'body'
class AbstractResource < ActiveResource::Base
self.site = RM_SITE_URL
self.user = RM_LOGIN
self.password = RM_PASSWORD
self.format = :xml
end
class Project < AbstractResource
# params option doesn't work in my case
def issue_categories(scope = :all)
IssueCategory.find(scope, :params => {:project_id => self.id})
end
end
class Membership < AbstractResource
self.site = "#{RM_SITE_URL}/projects/:project_id"
def project
Project.find(self.prefix_options[:project_id])
end
def project=(project)
self.prefix_options[:project_id] = project.id
end
end
class IssueCategory < Membership; end
class Issue < AbstractResource; end
class User < AbstractResource; end
class Role < AbstractResource; end
gh = Ghee.basic_auth(GH_LOGIN, GH_PASSWORD)
puts 'Fetching GitHub\'s issues...'
# I can't dry it because gh object returns hash from query
open_issues = gh.repos(GH_REPOSITORY_OWNER, GH_REPOSITORY_NAME).issues
closed_issues = gh.repos(GH_REPOSITORY_OWNER, GH_REPOSITORY_NAME).
issues(state: 'closed').all
puts 'Done.'
project = Project.all.detect { |p| p.name == RM_PROJECT_NAME }
all_issues = open_issues + closed_issues
gh_labels = []
gh_users = []
puts 'Collecting GitHub\'s labels and users...'
# collect those labels and users which where mentioned in GitHub's repository
all_issues.each do |i|
gh_labels << i[LABELS][0][NAME] if i[LABELS].any?
gh_users << i[USER][LOGIN]
end
gh_labels.uniq!
gh_users.uniq!
puts 'Done.'
puts "\nCreating Redmine's categories..."
gh_labels.each do |label|
cat = IssueCategory.new(name: label, project_id: project.id)
if cat.save
puts "Category #{cat.name} for project #{project.name} successfully created."
else
cat.errors.full_messages
end
end
other_category = IssueCategory.new(name: 'Other', project_id: project.id)
other_category.save
puts 'Done.'
puts "\nCreating Redmine's users..."
rm_users = gh_users.map do |u|
gh_user = gh.users(u)
full_name_str = gh_user[NAME]
login = gh_user[LOGIN]
full_name = full_name_str ? full_name_str.split : [login, login]
email = "#{login}@mail.com"
first_name = full_name.first
last_name = full_name.last
rm_user = User.new(firstname: first_name, lastname: last_name, mail: email)
rm_user.login = login
rm_user.password = SIMPLE_PASSWORD
rm_user.password_confirmation = SIMPLE_PASSWORD
if rm_user.save
puts "User #{rm_user.firstname} successfully created."
else
puts rm_user.errors.full_messages
end
role = Role.first # Manager role
membership = Membership.new
membership.user_id = rm_user.id
membership.project = project
membership.role_ids = [role.id]
if membership.save
puts "#{rm_user.firstname} become #{role.name}."
else
puts membership.errors.full_messages
end
rm_user
end
puts 'Done.'
puts "\nCreating Redmine's issues..."
1.upto(all_issues.size) do |i|
gh_issue = gh.repos(GH_REPOSITORY_OWNER, GH_REPOSITORY_NAME).issues(i)
category = gh_issue[LABELS].any? ? gh_issue[LABELS][0][NAME] :
other_category.name
user = rm_users.detect { |u| u.login == gh_issue[USER][LOGIN] }
Issue.user = user.login # re-setting user login for,
Issue.password = SIMPLE_PASSWORD # so this user is creating issue
rm_issue = Issue.new(
:tracker_id => 2, # Feature tracker
:subject => gh_issue['title'],
:description => gh_issue[BODY],
:project_id => project.id,
:status_id => STATE_HASH[gh_issue['state']],
:category_id => project.issue_categories.
detect { |ic| ic.name == category }.id
)
if rm_issue.save
puts "GitHub's issue ##{i} matches Redmine's issue ##{rm_issue.id}"
else
puts rm_issue.errors.full_messages
end
# creating comments if they are present
if gh_issue['comments'] > 0
comments = gh.repos(GH_REPOSITORY_OWNER, GH_REPOSITORY_NAME).
issues(gh_issue['number']).comments
comments.each do |comment|
Issue.user = comment[USER][LOGIN] # re-setting user login for,
Issue.password = SIMPLE_PASSWORD # so this user is creating issue
rm_issue.update_attribute(:notes, comment[BODY])
end
end
end
puts 'All the data was imported successfully!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment