-
Notifications
You must be signed in to change notification settings - Fork 1
/
rally-trello.rb
executable file
·165 lines (140 loc) · 5.29 KB
/
rally-trello.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env ruby
require 'yaml'
require 'rally_api'
require 'trello'
require 'slop'
CONFIG_FILE = "config.yml"
RALLY_URL = "https://rally1.rallydev.com/slm"
DEFAULT_LIST = "To Do"
def read_config
# Read config file
if !File.exists? CONFIG_FILE
puts "Config file (#{CONFIG_FILE}) doesn't exist. Please create it, usin #{CONFIG_FILE}.example as a guide)."
abort
end
@config = YAML.load_file(CONFIG_FILE)
# Parse command line options
@opts = Slop.parse help: true do
banner "Usage: #{File.basename($0)} [options]"
on :w, :rally_workspace=, 'Rally workspace name'
on :p, :rally_project=, 'Rally project name'
on :i, :rally_iteration=, 'Rally iteration name (required)'
on :d, :rally_defects, 'Import defects (off by default)'
on :b, :trello_board=, 'Target trello board (will be created if necessary)'
on :l, :trello_list=, 'Trello list name (will be created if necessary, default is "To Do")'
end
# Merge cmd-line opts with yaml config
cmd_line = { 'rally' => {}, 'trello' => {} }
@opts.to_hash.each do |k,v|
if v
(app, key) = k.to_s.split('_')
@config[app][key] = v
end
end
@config['trello']['list'] ||= "To Do"
validate_config
Trello.configure do |trello|
trello.developer_public_key = @config['trello']['developer_key']
trello.member_token = @config['trello']['user_token']
end
end
def validate_config
errors = []
errors << "Rally iteration must be specified on command line (-i)" if !@config['rally']['iteration']
errors << "Rally workspace must be specified in either #{CONFIG_FILE} or on command line" if !@config['rally']["workspace"]
errors << "Rally project must be specified in either #{CONFIG_FILE} or on command line" if !@config['rally']["project"]
errors << "Rally API key must be specified in #{CONFIG_FILE}" if !@config['rally']["api_key"]
errors << "Trello API key must be specified in #{CONFIG_FILE}" if !@config['trello']['developer_key']
errors << "Trello user token must be specified in #{CONFIG_FILE}" if !@config['trello']['user_token']
errors << "Trello board be specified in on command line (-b)" if !@config['trello']['board']
errors << "Trello list be specified in on command line (-l)" if !@config['trello']['list']
if errors.length > 0
errors.each { |e| puts e }
puts @opts
abort
end
end
# Get the Rally API client
def rally
@rally ||= begin
headers = RallyAPI::CustomHttpHeader.new({:vendor => "Trello", :name => "Trello Import", :version => "1.0"})
config = {:base_url => RALLY_URL}
config[:api_key] = @config['rally']['api_key']
config[:workspace] = @config['rally']['workspace']
config[:project] = @config['rally']['project']
config[:headers] = headers #from RallyAPI::CustomHttpHeader.new()
RallyAPI::RallyRestJson.new(config)
end
end
def rally_stories_for_iteration(iteration)
query = RallyAPI::RallyQuery.new()
query.type = "hierarchicalrequirement"
query.fetch = "Name,FormattedID,Project,ObjectID"
query.page_size = 1000
query.limit = 1000
query.order = "FormattedID Desc"
query.query_string = "(Iteration.Name = #{iteration})"
rally.find(query)
end
def rally_defects_for_iteration(iteration)
query = RallyAPI::RallyQuery.new()
query.type = "defect"
query.fetch = "Name,FormattedID,Project,ObjectID"
query.page_size = 1000
query.limit = 1000
query.order = "FormattedID Desc"
query.query_string = "(Iteration.Name = #{iteration})"
rally.find(query)
end
def trello_board(name)
board = Trello::Board.all().find { |b| b.name == name }
if !board && name
puts "Creating board '#{name}'"
board = Trello::Board.create(name: name)
end
board
end
def trello_list(name, board)
list = board.lists.find { |l| l.name == name }
if !list && name
puts "Creating list '#{name}'"
list = Trello::List.create(name: name, board_id: board.id)
end
list
end
def cards(list)
@cards ||= list.cards
end
def import_card(card_name, attachment_name, attachment_url, list)
if cards(list).any? {|c| c.name == card_name }
puts "Card '#{card_name}' already exists"
else
puts "Creating card: #{card_name}"
card = Trello::Card.create(name: card_name, list_id: list.id)
card.add_attachment(attachment_url, attachment_name)
end
end
def import_rally_entities_as_cards(rally_entities, entity_type, attachment_name, trello_list)
projectId = rally_entities.first.Project.read.ObjectID
rally_entities.each do |entity|
card_name = "#{entity.FormattedID}: #{entity.name}"
story_url = "https://rally1.rallydev.com/#/#{projectId}d/detail/#{entity_type}/#{entity.ObjectID}"
import_card(card_name, attachment_name, story_url, trello_list)
end
end
read_config()
iteration = @config['rally']['iteration']
stories = rally_stories_for_iteration(iteration)
if stories.length < 1
puts "No user stories found for iteration '#{iteration}'"
end
defects = rally_defects_for_iteration(iteration)
if defects.length < 1
puts "No defects found for iteration '#{iteration}'"
end
board = trello_board(@config['trello']['board'])
list = trello_list(@config['trello']['list'], board)
puts "Importing to board '#{board.name}'"
puts "Importing to list '#{list.name}'"
import_rally_entities_as_cards(defects, 'defect', "Rally Defect", list) if @config['rally']['defects']
import_rally_entities_as_cards(stories, 'userstory', "Rally User Story", list)