This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
trello
executable file
·986 lines (867 loc) · 35.8 KB
/
trello
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
#!/usr/bin/env ruby
# coding: utf-8
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib') unless $LOAD_PATH.include?(File.dirname(__FILE__) + '/lib')
require 'config'
require 'trello_helper'
require 'trello_json_loader'
require 'output_helper'
require 'overviews_helper'
require 'reports'
require 'report'
require 'erb'
require 'pp'
require 'yaml'
require 'i18n'
require 'commander/import'
require 'bugzilla_helper'
require 'csv'
Encoding.default_external = "UTF-8"
I18n.backend.store_translations(:en,
i18n: {
transliterate: {
rule: {
'ö' => 'oe',
'ü' => 'ue',
'Ö' => 'Oe',
'Ü' => 'Ue',
'ß' => 'ss',
}
}
})
I18n.config.enforce_available_locales = false
name = "#{__FILE__}"
program :name, "Trello Utilities"
program :version, "1.0.0"
program :description, "An assortment of Trello utilities"
# This loads the conf files and creates new objects based on the specified classes
def load_conf(klass, args, single = false)
if single
klass.new(args)
else
Hash[*args.map do |key, val|
[key, klass.new(val)]
end.flatten]
end
end
$tag_to_label_color = {
'documentation' => 'green',
'tc-approved' => 'yellow',
'no-qe' => 'orange',
'security' => 'red',
'devcut' => 'purple'
}
LINKS = { 'roadmap_overview' => 'Roadmap',
'releases_overview' => 'Releases',
'teams_overview' => 'Teams',
'developers_overview' => 'Developers',
'sprints_overview' => 'Sprints',
'labels_overview' => 'Labels',
'sprint_schedule' => 'Sprint Schedule' }
ONE_DAY = 60 * 60 * 24
trello = load_conf(TrelloHelper, CONFIG.trello, true)
json_loader = TrelloJsonLoader.new
# Generate a formatted string representing the sortable card for
# dry-run output
def card_data_string(card)
labeldata = if card.release
label_text = "%-30s" % "#{card.state}-#{card.product}-#{card.release}"
else
" " * 30
end
"#{labeldata} %-4d pos: %-20s name: [%-30s]" % [card.card.short_id, "#{card.new_pos}", card.card.name[0..29]]
end
def try_loading_bugzilla
bz = nil
begin
if CONFIG.bugzilla && CONFIG.bugzilla[:username] != "<bugzilla_username>"
bz = load_conf(BugzillaHelper, CONFIG.bugzilla, true)
end
rescue NoMethodError
end
if bz.nil?
$stderr.puts "*** WARNING: No bugzilla configuration could be loaded; RFE and bug info will be excluded from processing"
end
bz
end
global_option "--load-dir LOADDIR", "Load sprint *board* data from *.json backups in LOADDIR" do |loaddir|
json_files = Dir.glob(File.join(loaddir, "*.json")).map { |f| File.expand_path(f) }
# Load org and org members first, since they're special
org_prefix = File.expand_path("#{loaddir}/#{TrelloHelper::ORG_BACKUP_PREFIX}")
org_members_prefix = File.expand_path("#{loaddir}/#{TrelloHelper::ORG_MEMBERS_BACKUP_PREFIX}")
if json_files.any? { |fname| fname.match(/^#{org_prefix}/) }
json_filespec = json_files.delete_at(json_files.index { |fname| fname.match(/^#{org_prefix}/) })
$stderr.puts "*** Loading org data from #{json_filespec}"
json_loader.load_from_file(json_filespec, "organizations")
end
if json_files.any? { |fname| fname.match(/^#{org_members_prefix}/) }
json_filespec = json_files.delete_at(json_files.index { |fname| fname.match(/^#{org_members_prefix}/) })
$stderr.puts "*** Loading org members data from #{json_filespec}"
json_loader.load_org_members_from_file(json_filespec)
end
# Load board data
json_files.sort.each do |json_filespec|
$stderr.puts "*** Loading board data from #{json_filespec}"
json_loader.load_from_file(json_filespec, "boards")
end
trello.add_json_loader_content(json_loader)
end
# Sort only the release cards by their release label, release number
# Sort only the cards in TrelloHelper::NEXT_STATES and TrelloHelper::BACKLOG_STATES
command :organize_release_cards do |c|
c.option "--dry-run", "Show what changes would be made without actually making them"
c.syntax = "#{name} organize_release_cards"
c.description = "Rearrange board list cards sorted by release"
c.action do |args, options|
release_states = TrelloHelper::NEXT_STATES.keys + TrelloHelper::BACKLOG_STATES.keys
trello.boards.values.each do |board|
trello.board_lists(board).each do |list|
next unless release_states.include? list.name
puts "\n*** Processing list #{list.name} in board #{trello.board(list.board_id).name}"
# *** Set up for sorting list
# list_cards contains the cards for this board, sorted by
# position (i.e. as they are displayed in the UI)
sortable_cards = trello.sortable_cards(list)
next unless sortable_cards
puts "\nCards in list, pre-sort"
sortable_cards.each do |card|
labeldata = card_data_string(card)
puts " #{labeldata}"
end
puts "Sorting.\n"
sortcount = 0
needs_sorting = true
# *** Start sort logic
while needs_sorting do
needs_sorting = false
sortcount += 1
puts "Still sorting..." if (sortcount % 10) == 0
last_card = nil
ooo = nil
sortable_cards.each do |card|
next unless card.release
if last_card
if ooo
if !trello.cards_in_order(ooo, card) && !trello.cards_equal(ooo, card)
ooo = card
needs_sorting = true
end
else
if !trello.cards_in_order(last_card, card) && !trello.cards_equal(last_card, card)
ooo = card
needs_sorting = true
end
end
end
last_card = card
end
if needs_sorting
insert_before = nil
sortable_cards[0..(sortable_cards.index(ooo) - 1)].reverse_each do |card|
next unless card.release
if trello.cards_in_order(ooo, card)
if insert_before
if trello.cards_in_order(card, insert_before)
insert_before = card
end
else
insert_before = card
end
end
end
if insert_before
delete_index = sortable_cards.index { |card| card == ooo }
sortable_cards.delete_at(delete_index)
insert_index = sortable_cards.index { |card| card == insert_before }
sortable_cards.insert(insert_index, ooo)
end
end
end
bounding_card_ids_by_id = trello.bounding_card_ids_by_id(sortable_cards)
puts "Cards in list, post-sort"
sortable_cards.each do |card|
labeldata = card_data_string(card)
if bounding_card_ids_by_id.include? card.card.id
marker = "*"
else
marker = " "
end
puts "#{marker} #{labeldata}"
end
puts "\nUpdating cards."
# For each card that needs renumbering, use the bounding card
# IDs to find the positions of the previous (adjacent,
# in-order) and following in-order cards to calculate a new
# position that is guaranteed to be strictly increasing.
bounding_card_ids_by_id.each do |card_id, bounding_card_ids|
list_cards = trello.list_cards(list)
card = list_cards.find { |c| c.id == card_id }
before_card = nil
after_card = nil
# "top" and "bottom" have special meaning for Trello card
# positions - i.e. "before the start of list" and "after the
# end of list" - we use them so we don't honk up the
# renumbering algorithms on the server side.
if bounding_card_ids[:before].nil?
card.pos = 'top'
elsif bounding_card_ids[:after].nil?
card.pos = 'bottom'
else
# If we're not headed to either end of the list, put us
# between the appropriate pair of in-order cards.
before_card = list_cards.find { |c| c.id == bounding_card_ids[:before] }
after_card = list_cards.find { |c| c.id == bounding_card_ids[:after] }
card.pos = Float(before_card.pos + after_card.pos) / 2
end
if options.dry_run
# Make sure we have the cards we need now, even if we didn't before.
before_card = list_cards.find { |c| c.id == bounding_card_ids[:before] } if before_card.nil?
after_card = list_cards.find { |c| c.id == bounding_card_ids[:after] } if after_card.nil?
if card.pos == 'top' && after_card
# Fake the top card position for future faking (since we
# will do math with it)
card.pos = Float(after_card.pos) / 2
elsif card.pos == 'bottom' && before_card
# Fake the bottom card position for future faking (since
# we will do math with it)
card.pos = before_card.pos + TrelloHelper::TRELLO_CARD_INCREMENT
end
puts " Would update: #{labeldata = card_data_string(trello.sortable_card(card))}"
else
puts " Updating: #{labeldata = card_data_string(trello.sortable_card(card))}"
trello.update_card(card)
# We want to get updated card positions for the next run,
# so we force reloading the cards for this list.
trello.cards_by_list.delete(list.id)
end
end
end
end
end
end
command :backup_org_boards do |c|
c.option "--out-dir DIRECTORY", "Directory to dump backup json to"
c.syntax = "#{name} backup_org_boards"
c.description = "dump a JSON backup of all organization boards to the specified directory"
c.action do |args, options|
output_dir = (options.out_dir.nil? || options.out_dir.empty?) ? Pathname.pwd : Pathname.new(options.out_dir)
if !output_dir.writable?
$stderr.puts "Directory #{output_dir} doesn't specify a writable directory."
exit 1
end
trello.org_boards.each do |board|
unless board.nil?
out_filename = "#{board.url.split('/').last}.json"
out_path = File.join(output_dir, out_filename)
puts "Backing up #{board.name} to #{out_path}"
begin
# cache the response so we don't create a 0-byte file if the request times out
output = trello.dump_board_json(board)
File.open(out_path, 'w') { |f| f.write(output) }
rescue Exception => e
$stderr.puts "Error while writing to #{out_path}: #{e.message}"
raise
end
end
end
out_filename = "#{TrelloHelper::ORG_BACKUP_PREFIX}#{trello.org.name}.json"
out_path = File.join(output_dir, out_filename)
begin
output = trello.dump_org_json()
File.open(out_path, 'w') { |f| f.write(output) }
rescue Exception => e
$stderr.puts "Error while writing to #{out_path}: #{e.message}"
raise
end
out_filename = "#{TrelloHelper::ORG_MEMBERS_BACKUP_PREFIX}#{trello.org.name}.json"
out_path = File.join(output_dir, out_filename)
begin
output = trello.dump_org_members_json()
File.open(out_path, 'w') { |f| f.write(output) }
rescue Exception => e
$stderr.puts "Error while writing to #{out_path}: #{e.message}"
raise
end
end
end
command :list do |c|
c.syntax = "#{name} list"
c.option "--list LIST_NAME", "Restrict to a particular list"
c.option "--team TEAM_NAME (#{trello.teams.keys.join('|')})", "Restrict to a team"
c.option "--card-ref SCOPE_TEAM_ID", "Get a single card Ex: #{trello.teams.values.first.keys.first}_1"
c.option "--card-url URL", "Get a single card Ex: https://trello.com/c/6EhPEbM4"
c.description = "An assortment of Trello queries"
c.action do |args, options|
puts "Organization: #{trello.org.name}"
if options.card_ref
card = trello.card_by_ref(options.card_ref)
if card
trello.print_card(card)
else
$stderr.puts "#{options.card_ref} is an invalid format!"
exit 1
end
elsif options.card_url
card = trello.card_by_url(options.card_url)
if card
trello.print_card(card)
else
$stderr.puts "#{options.card_url} is an invalid format!"
exit 1
end
else
if options.team
boards = trello.team_boards(options.team)
else
boards = trello.org_boards
end
boards.each do |board|
trello.board_members(board) # seed the cache with board-specific members
puts "\nBoard Name: #{board.name}"
lists = trello.board_lists(board)
if options.list
lists = []
lists.each do |list|
if list.name == options.list
lists = [list]
break
end
end
end
lists.each do |list|
trello.print_list(list)
end
end
end
end
end
command :list_roadmap_labels do |c|
c.syntax = "#{name} list_roadmap_labels"
c.description = "List the labels belonging to a board, defaulting to the Roadmap board designated in the configuration."
c.option "--board BOARD_NAME", "List labels on a particular board"
c.action do |args, options|
if options.board_name
board = trello.boards.find { |b| b.name == options.board_name }
if board
trello.print_labels(board)
else
puts "Couldn't find board named \"#{options.board_name}\""
end
else
trello.print_labels
end
end
end
command :sprint_identifier do |c|
c.syntax = "#{name} sprint_identifier"
c.description = "Print the sprint identifier"
c.action do |args, options|
if trello.sprint_card.name =~ TrelloHelper::SPRINT_REGEX
print "#{$1}"
else
print "unknown"
end
end
end
command :release_identifier do |c|
c.syntax = "#{name} release_identifier"
c.description = "Print the release identifier"
c.action do |args, options|
if trello.current_release_labels && !trello.current_release_labels.empty?
TrelloHelper::RELEASE_LABEL_REGEX.match(trello.current_release_labels.first) do |fields|
print "#{fields[3]}"
end
end
end
end
command :days_left_in_sprint do |c|
c.syntax = "#{name} days_left_in_sprint"
c.description = "Print the number of days left in the sprint"
c.action do |args, options|
# Take the modulo by trello.sprint_length_in_days (21), since the
# last Wednesday of each sprint is when the sprint card usually is
# updated. From that point until Saturday, the formula below would
# otherwise give an answer that doesn't make sense (e.g. 23 days
# left on Wednesday afternoon)
print (((trello.sprint_card.due.to_time - Time.new) / ONE_DAY).ceil % trello.sprint_length_in_days)
end
end
command :days_until_code_freeze do |c|
c.syntax = "#{name} days_until_code_freeze"
c.description = "Print the number of days left until the next code freeze"
c.action do |args, options|
sprint = Sprint.new(trello: trello, uninitialized: true)
print ((sprint.code_freeze.to_time - Time.new) / ONE_DAY).ceil
end
end
command :days_until_feature_complete do |c|
c.syntax = "#{name} days_until_feature_complete"
c.description = "Print the number of days left until feature_complete"
c.action do |args, options|
sprint = Sprint.new(trello: trello, uninitialized: true)
print ((sprint.feature_complete.to_time - Time.new) / ONE_DAY).ceil
end
end
command :days_until_stage_one_dep_complete do |c|
c.syntax = "#{name} days_until_stage_one_dep_complete"
c.description = "Print the number of days left until stage one dependencies are feature complete"
c.action do |args, options|
sprint = Sprint.new(trello: trello, uninitialized: true)
print ((sprint.stage_one_dep_complete.to_time - Time.new) / ONE_DAY).ceil
end
end
command :card_ref_from_url do |c|
c.syntax = "#{name} card_ref_from_url"
c.option "--card-url URL", "Card url Ex: https://trello.com/c/6EhPEbM4"
c.description = "Print the card ref based on a given url"
c.action do |args, options|
card = trello.card_by_url(options.card_url)
if card
print trello.card_ref(card)
else
$stderr.puts "#{options.card_url} not found!"
exit 1
end
end
end
command :list_invalid_users do |c|
c.syntax = "#{name} list_invalid_users"
c.description = "List the potentially invalid users"
c.action do |args, options|
require 'ldap_helper'
ldap = load_conf(LdapHelper, CONFIG.ldap, true)
puts "Potential Invalid Organization Users:"
valid_user_names = {}
invalid_user_names = ldap.print_invalid_members(trello.org_members, valid_user_names, invalid_user_names)
puts "\n\nPotential Invalid Board Users:"
trello.org_boards.each do |board|
unless board.closed?
puts "\n#{board.name}:"
ldap.print_invalid_members(trello.board_members(board), valid_user_names, invalid_user_names.clone)
end
end
puts "\n\nValid Logins:"
puts valid_user_names.map {|k, v| "%-35s %s" % [k, v]}
end
end
command :generate_release_json do |c|
c.syntax = "#{name} generate_release_json"
c.option "--out-dir DIRECTORY", "The dir to output to Ex: /tmp"
c.option "--release RELEASE", "Release to build json for Ex: 3.3"
c.option "--product PRODUCT", "Product to build json for Ex: myproduct"
c.description = "Generate json for the cards in a release"
c.action do |args, options|
release_cards = trello.release_cards(options.product, options.release)
if options.out_dir
File.open(File.join(options.out_dir, "#{options.product ? options.product + '-' : '' }#{options.release}.json"), 'w') { |f| f.write(JSON.pretty_generate(release_cards)) }
else
pp JSON.pretty_generate(release_cards)
end
end
end
command :generate_trello_login_to_email_json do |c|
c.syntax = "#{name} generate_trello_login_to_email_json"
c.option "--out OUT_FILE", "File to output the resulting json to Ex: /tmp/trello_login_to_email.json"
c.description = "Generate a json file with trello login -> email"
c.action do |args, options|
require 'ldap_helper'
ldap = load_conf(LdapHelper, CONFIG.ldap, true)
valid_users = ldap.valid_users(trello.org_members)
if options.out
File.open(options.out, 'w') { |f| f.write(JSON.pretty_generate(valid_users)) }
else
pp JSON.pretty_generate(valid_users)
end
end
end
command :report do |c|
c.syntax = "#{name} report"
c.option "--report-type NAME" , "Available report types: %s" % CONFIG.reports.keys.join(', ')
c.option "--send-email" , "Send email?"
c.description = "An assortment of Trello reporting utilities"
c.action do |args, options|
options.report_type ||= choose("Report to run?", *CONFIG.reports.keys)
options.report_type = options.report_type.to_sym
if options.date
$date = Date.parse(options.date)
end
heading "Generating Status Report" do
# Read Rally configuration file
_progress "Logging into Trello" do
$sprint = Sprint.new(trello: trello)
end
# Generate queries
_progress "Generating queries" do
$report_types = load_conf(UserStoryReport, CONFIG.queries)
end
# Generate reports
_progress "Building available reports" do
$reports = load_conf(Report, CONFIG.reports)
end
end
report = $reports[options.report_type]
report.options = options.__hash__
_table(
"Running Report With Options",
report.options.marshal_dump,
capitalize: true,
sort: 0,
separator: ':'
)
report.send_email
end
end
command :generate_roadmap_overview do |c|
c.syntax = "#{name} generate_roadmap_overview"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/roadmap_overview.html"
c.description = "Generate the overview of the roadmap board"
c.action do |args, options|
options.out ||= '/tmp/roadmap_overview.html'
overviews_helper = OverviewsHelper.new(trello: trello)
overviews_helper.create_roadmap_overview(options.out)
end
end
command :generate_sprints_overview do |c|
c.syntax = "#{name} generate_sprints_overview"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/sprints_overview.html"
c.option "--sprints NUM", "The number of sprints to show"
c.option "--offset NUM", "The number of sprints to offset from the latest"
c.description = "Generate the sprints overview"
c.action do |args, options|
options.out ||= '/tmp/sprints_overview.html'
options.sprints = options.sprints ? options.sprints.to_i : 8
options.offset = options.offset ? options.offset.to_i : 0
erb = ERB.new(File.open('templates/sprints_overview.erb', "rb").read)
File.open(options.out, 'w') { |f| f.write(erb.result(binding)) }
end
end
command :generate_sprint_schedule do |c|
c.syntax = "#{name} generate_sprint_schedule"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/sprint_schedule.html"
c.option "--sprints NUM", "The number of sprints to show"
c.description = "Generate the sprint schedule"
c.action do |args, options|
options.out ||= '/tmp/sprint_schedule.html'
options.sprints = options.sprints ? options.sprints.to_i : 8
erb = ERB.new(File.open('templates/sprint_schedule.erb', "rb").read)
File.open(options.out, 'w') { |f| f.write(erb.result(binding)) }
end
end
command :generate_labels_overview do |c|
c.syntax = "#{name} generate_labels_overview"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/labels_overview.html"
c.description = "Generate the labels overview"
c.action do |args, options|
options.out ||= '/tmp/labels_overview.html'
overviews_helper = OverviewsHelper.new(trello: trello)
overviews_helper.create_labels_overview(options.out)
end
end
command :generate_releases_overview do |c|
c.syntax = "#{name} generate_releases_overview"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/releases_overview.html"
c.description = "Generate the releases overview"
c.action do |args, options|
bugzilla = try_loading_bugzilla
options.out ||= '/tmp/releases_overview.html'
overviews_helper = OverviewsHelper.new(trello: trello, bugzilla: bugzilla)
overviews_helper.create_releases_overview(options.out)
end
end
command :generate_teams_overview do |c|
c.syntax = "#{name} generate_teams_overview"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/teams_overview.html"
c.description = "Generate the teams overview"
c.action do |args, options|
bugzilla = try_loading_bugzilla
options.out ||= '/tmp/teams_overview.html'
overviews_helper = OverviewsHelper.new(trello: trello, bugzilla: bugzilla)
overviews_helper.create_teams_overview(options.out)
end
end
command :generate_developers_overview do |c|
c.syntax = "#{name} generate_developers_overview"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/developers_overview.html"
c.description = "Generate the developers overview"
c.action do |args, options|
options.out ||= '/tmp/developers_overview.html'
overviews_helper = OverviewsHelper.new(trello: trello)
overviews_helper.create_developers_overview(options.out)
end
end
command :generate_raw_overview do |c|
c.syntax = "#{name} generate_raw_overview"
c.option "--out OUT_FILE", "The file to output Ex: /tmp/raw_overview.csv"
c.description = "Generate a CSV-formatted file containing the card data used in the overview pages, suitable to importing into a spreadsheet."
c.action do |args, options|
options.out ||= '/tmp/raw_overview.csv'
overviews_helper = OverviewsHelper.new(trello: trello)
overviews_helper.create_raw_overview_data(options.out)
end
end
command :generate_default_overviews do |c|
c.syntax = "#{name} generate_default_overviews"
c.option "--out-dir DIRECTORY", "The dir to output to Ex: /tmp"
c.option "--sprints NUM", "The number of sprints to show"
c.option "--offset NUM", "The number of sprints to offset from the latest"
c.description = "Generate the default overviews"
c.action do |args, options|
bugzilla = try_loading_bugzilla
options.out_dir ||= '/tmp'
overviews_helper = OverviewsHelper.new(trello: trello, bugzilla: bugzilla)
overviews_helper.create_roadmap_overview(File.join(options.out_dir, 'roadmap_overview.html'))
overviews_helper.create_releases_overview(File.join(options.out_dir, 'releases_overview.html'))
overviews_helper.create_labels_overview(File.join(options.out_dir, 'labels_overview.html'))
overviews_helper.create_teams_overview(File.join(options.out_dir, 'teams_overview.html'))
overviews_helper.create_developers_overview(File.join(options.out_dir, 'developers_overview.html'))
overviews_helper.create_raw_overview_data(File.join(options.out_dir, 'raw_overview.csv'))
options.sprints = options.sprints ? options.sprints.to_i : 14
options.offset = options.offset ? options.offset.to_i : 0
erb = ERB.new(File.open('templates/sprints_overview.erb', "rb").read)
File.open(File.join(options.out_dir, 'sprints_overview.html'), 'w') { |f| f.write(erb.result(binding)) }
end
end
command :comment do |c|
c.syntax = "#{name} comment"
c.option "--card-ref SCOPE_TEAM_ID", "Card to comment on by ref Ex: #{trello.teams.values.first.keys.first}_1"
c.option "--card-url URL", "Card to comment on by url Ex: https://trello.com/c/6EhPEbM4"
c.description = "Adds a comment to a trello card"
c.action do |args, options|
comment = args[0]
card = nil
if options.card_ref
card = trello.card_by_ref(options.card_ref)
elsif options.card_url
card = trello.card_by_url(options.card_url)
end
if card
card.add_comment(comment)
else
$stderr.puts "#{options.card_ref || options.card_url} is an invalid format!"
exit 1
end
end
end
command :sync_labels do |c|
c.syntax = "#{name} sync_labels"
c.description = "Sync the labels from the roadmap board to all the rest"
c.action do |args, options|
roadmap_label_colors_by_name = trello.roadmap_label_colors_by_name
unless roadmap_label_colors_by_name.empty?
boards = trello.boards.values
boards.insert(0, trello.public_roadmap_board) if trello.public_roadmap_board
boards.each do |board|
puts "\nBoard Name: #{board.name}"
board_labels = trello.target(trello.board_labels(board))
board_labels_by_name = {}
empty_labels_by_color = {}
board_labels.each do |board_label|
if board_label.name.nil? || board_label.name.empty?
empty_labels_by_color[board_label.color] = board_label if board_label.color
else
board_labels_by_name[board_label.name] = board_label
end
end
raise "Duplicate labels found: #{board.name}" unless board_labels_by_name.length + empty_labels_by_color.length == board_labels.length
roadmap_label_colors_by_name.each do |label_name, label_color|
board_label = board_labels_by_name[label_name] || empty_labels_by_color[label_color]
if !board_label || (label_color != board_label.color) || empty_labels_by_color[label_color]
if board_label
puts "Updating label #{label_name} to #{label_color}"
board_label.color = (label_color.nil? || label_color.empty?) ? '' : label_color
board_label.name = label_name
trello.update_label(board_label)
empty_labels_by_color.delete(label_color) if empty_labels_by_color[label_color]
else
puts "Setting label #{label_name} to #{label_color}"
created = false
create_retry_count = 0
while !created
begin
trello.create_label(label_name, label_color, board.id)
created = true
rescue => e
board_labels = trello.target(trello.board_labels(board))
created = board_labels.map { |l| l.name }.include?(label_name)
unless created
$stderr.puts "Error creating label: #{e.message}"
raise if create_retry_count >= TrelloHelper::DEFAULT_RETRIES
end
end
create_retry_count += 1
end
end
end
end
board_labels = trello.target(trello.board_labels(board))
board_labels.each do |board_label|
if !roadmap_label_colors_by_name.has_key?(board_label.name)
puts "Deleting label #{board_label.name}"
begin
trello.delete_label(board_label)
rescue => e
$stderr.puts "Error deleting label: #{e.message}"
end
end
end
end
end
end
end
command :rename_label do |c|
c.syntax = "#{name} rename_label"
c.option "--from FROM", "The label to rename"
c.option "--to TO", "What to rename it to"
c.description = "Renames a label on all configured boards"
c.action do |args, options|
raise "--from not specified" if options.from.nil? || options.from.strip.empty?
raise "--to not specified" if options.to.nil? || options.to.strip.empty?
from = options.from.strip
to = options.to.strip
boards = trello.roadmap_boards + trello.boards.values
boards.each do |board|
puts "\nBoard Name: #{board.name}"
board_labels = trello.target(trello.board_labels(board))
board_label = nil
board_labels.each do |bl|
if from == bl.name
board_label = bl
break
end
end
if board_label.nil?
puts " #{from} not found on #{board.name}"
else
puts " Updating label #{from} to #{to}"
board_label.name = options.to
trello.update_label(board_label)
end
end
end
end
command :create_roadmap_labels do |c|
c.syntax = "#{name} create_roadmap_labels LABEL1 LABEL2 ..."
c.description = "creates each of LABEL1 LABEL2 ... as labels on the configured roadmap board"
c.action do |args, options|
puts "args: #{args} options: #{options}"
roadmap_label_names = trello.board_labels(trello.roadmap_board).map { |l| l.name }
args.each do |new_label_name|
if roadmap_label_names.include? new_label_name
puts "Not creating duplicate label: #{new_label_name}"
else
puts "Creating label: #{new_label_name}"
begin
trello.create_label(new_label_name, nil, trello.roadmap_id)
rescue Exception => e
$stderr.puts "Error while creating label named #{new_label_name} on board #{trello.roadmap_board.name}: #{e.message}"
end
end
end
end
end
command :convert_markers_to_labels do |c|
c.syntax = "#{name} sync_labels"
c.description = "Convert [] markers on cards to epic- and future labels that exist"
c.action do |args, options|
boards = trello.boards.values
boards.each do |board|
puts "\nBoard Name: #{board.name}"
board_labels = trello.target(trello.board_labels(board))
board_labels_by_name = {}
board_labels.each do |board_label|
board_labels_by_name[board_label.name] = board_label
end
lists = trello.board_lists(board)
lists.each do |list|
cards = trello.list_cards(list)
unless cards.empty?
puts "\n List: #{list.name} (#cards: #{cards.length})"
cards.each_with_index do |card, index|
card_name = card.name
card_tags = card_name.scan(/\[[^\]]+\]/)
card_tags.each do |card_tag|
card_tag.downcase!
if card_tag == TrelloHelper::FUTURE_TAG
epic_label_name = TrelloHelper::FUTURE_LABEL
else
epic_label_name = "epic-#{card_tag[1..-2]}"
end
if board_labels_by_name.has_key? epic_label_name
unless trello.card_labels(card).map { |c| c.name }.include?(epic_label_name)
epic_label = board_labels_by_name[epic_label_name]
puts "Adding #{epic_label.name} to #{card.name} (#{card.url})"
trello.add_label_to_card(card, epic_label)
end
card_name = card_name.sub "#{card_tag} ", ''
card_name = card_name.sub " #{card_tag}", ''
card_name = card_name.sub card_tag, ''
card.name = card_name
puts "New card name #{card.name}"
trello.update_card(card)
end
end
end
end
end
end
end
end
command :update do |c|
c.syntax = "#{name} update"
c.option "--add-task-checklists", "Add task checklists to stories"
c.option "--add-bug-checklists", "Add checklists to stories"
c.option "--add-dependent-tasks", "Add dependent work tasks (e.g. documentation tasks) to corresponding labeled stories"
c.option "--add-dependent-cards", "Add dependent work cards (e.g. documentation cards) for corresponding labeled dev cards"
c.option "--update-bug-tasks", "Update closed/verified bug tasks"
c.option "--update-roadmap", "Update the roadmap board with progress from teams. Note: Existing checklist items will be removed."
c.description = "An assortment of Trello modification utilities"
c.action do |args, options|
if options.update_roadmap
trello.update_roadmap
end
bugzilla = try_loading_bugzilla if options.update_bug_tasks
if options.add_task_checklists || options.add_bug_checklists || options.update_bug_tasks || options.add_dependent_tasks || options.add_dependent_cards
# Build a labels reference for each dependent work board
trello.boards.each do |board_id, board|
next if trello.dependent_work_board_ids.include? board_id
team_map = trello.board_id_to_team_map[board_id]
# Use the team dependent work boards if set, default to global
# dependent work boards
dependent_work_boards = team_map[:dependent_work_boards] || trello.dependent_work_boards || {} # can't call .each on nil
dependent_work_boards.each do |dep_work_board_id, dep_work_board_params|
dep_work_board = trello.boards[dep_work_board_id]
puts "\nBoard Name: #{board.name}"
puts "Dependent Work Board Name: #{dep_work_board.name}"
# Don't leak info from private boards to public boards
dependent_work_valid = !(board.prefs['permissionLevel'] == 'org' && dep_work_board.prefs['permissionLevel'] != 'org')
lists = trello.board_lists(board)
lists.each do |list|
if trello.list_for_in_progress_work?(list.name)
cards = trello.list_cards(list)
if !cards.empty?
puts "\n List: #{list.name} (#cards #{cards.length})"
cards.each_with_index do |card, index|
if options.add_task_checklists || options.add_bug_checklists || options.add_dependent_cards
if !(card.name =~ TrelloHelper::SPRINT_REGEX && !card.due.nil?)
labels = trello.card_labels(card)
label_names = labels.map { |l| l.name }
if options.add_dependent_cards && dependent_work_valid
trello.add_dependent_cards(card, dep_work_board_id, dep_work_board_params, label_names, team_map)
end
trello.update_card_checklists(card, label_names, options.add_task_checklists, options.add_bug_checklists)
end
end
if options.update_bug_tasks
trello.update_bug_tasks(card, bugzilla) if !bugzilla.nil?
end
if options.add_dependent_tasks && dep_work_board_params.include?(:task_reminder_text) && dependent_work_valid
trello.add_dependent_tasks_reminder(card, dep_work_board_params[:task_reminder_text], dep_work_board_params[:label])
end
end
end
end
end
end
end
end
end
end