Created
December 15, 2010 04:05
-
-
Save tedkulp/741612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nestful' | |
require 'pivotal-tracker' | |
require 'yaml' | |
# Ensure that you have the active_resource gem installed on your local system: | |
# gem install nestful pivotal-tracker | |
# Change the script by filling in your credentials. Grab your key from Redmine | |
# under the 'My Account' section in the top right-hand portion. | |
# Create a file called 'credentials.yml' in the same directory as this script. | |
# Make sure it has the following setup: | |
# Project Name 1: | |
# project_id: 1 | |
# site: http://redmine.site.com | |
# user: redmine_user | |
# password: redmine_pass | |
# token: deadbeefdeadbeefdeadbeefdeadbeef | |
# Project Name 2: | |
# project_id: 2 | |
# site: http://redmine.site.com | |
# user: redmine_user | |
# password: redmine_pass | |
# token: deadbeefdeadbeefdeadbeefdeadbeef | |
# In your Redmine install, create a field called 'Pivotal Story Id' under Issues. | |
# Set it to a integer, add it to your Bug and Feature trackers. I also recommend | |
# checking 'For all projects' so you don't have to worry about forgetting it. | |
# Usage: ruby pivotmine.rb | |
# This should output an issue, change the output at the bottom to get it in a format | |
# you want. | |
config = YAML.load_file("credentials.yml") | |
config.each do |project_name, config| | |
PivotalTracker::Client.token = config["token"] | |
PivotalTracker::Client.use_ssl = true | |
projects = PivotalTracker::Project.all | |
pivotal_project = projects.select { |p| p.name == project_name }.first | |
@page = 1 | |
@per_page = 25 | |
@issue_ids = [] | |
def get_issues(config) | |
puts "Getting issues" | |
response = Nestful.get config['site'] + '/issues', | |
:user => config['user'], | |
:password => config['password'], | |
:params => { :project_id => config['project_id'], :per_page => @per_page, :page => @page }, | |
:format => :json | |
@issues = response['issues'] | |
puts "Found #{@issues.count} issues" | |
end | |
puts config.to_yaml | |
get_issues(config) | |
while @issues.count > 0 && @issues.count <= @per_page && !@issue_ids.include?(@issues.first['id']) | |
@issues.each do |issue| | |
next if @issue_ids.include?(issue['id']) | |
@issue_ids << issue['id'] | |
status = 'accepted' | |
if issue.has_key?('status') and issue['status'].has_key?('name') | |
status = issue['status']['name'].downcase | |
end | |
next if ['new', 'rejected'].include?(status) | |
story_type = 'feature' | |
if issue.has_key?('tracker') and issue['tracker'].has_key?('name') | |
story_type = issue['tracker']['name'].downcase | |
end | |
next unless ['feature', 'bug'].include?(story_type) | |
has_custom_field = false | |
pivotal_story_id = -1 | |
if issue.has_key?('custom_fields') | |
issue['custom_fields'].each do |val| | |
if val['name'] == 'Pivotal Story Id' | |
has_custom_field = true | |
if val['value'].strip != '' | |
pivotal_story_id = val['value'].strip.to_i | |
end | |
end | |
end | |
end | |
next if pivotal_story_id > -1 | |
response = Nestful.get config['site'] + '/projects/' + config['project_id'].to_s, | |
:user => config['user'], | |
:password => config['password'], | |
:format => :json | |
project = response['project'] | |
points = issue.has_key?(:spent_hours) ? issue.spent_hours : nil | |
addendum = "\n\n" + config['site'] + "/issues/#{issue['id']}" | |
description = [issue['description'], addendum].join("") | |
issue_hash = { :name => issue['subject'], :story_type => story_type, :estimate => points, | |
:description => description, :labels => "[#{project['identifier']}]" | |
} | |
begin | |
result = pivotal_project.stories.create(issue_hash) | |
if has_custom_field | |
count = 0 | |
issue['custom_fields'].each do |val| | |
if val['name'] == 'Pivotal Story Id' | |
issue['custom_fields'][count]['value'] = result.id.to_s | |
end | |
count = count + 1 | |
end | |
response = Nestful.put config['site'] + '/issues/' + issue['id'].to_s + '.json', | |
:user => config['user'], | |
:password => config['password'], | |
:params => {:issue => issue}, | |
:format => :json | |
end | |
rescue RestClient::InternalServerError | |
puts "Error: #{issue_hash.inspect}" | |
next | |
end | |
end | |
# start it all over again | |
@page += 1 | |
get_issues(config) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment