PageRenderTime 21ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/git2bit

https://bitbucket.org/joeworkman/git2bit
Ruby | 133 lines | 87 code | 31 blank | 15 comment | 10 complexity | d29fe17266f79480783b0b502744a031 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'date'
  3. require 'optparse'
  4. require 'methadone'
  5. require 'git2bit'
  6. class App
  7. include Methadone::Main
  8. include Methadone::CLILogging
  9. main do
  10. help_now!("'guser' is required") unless options[:guser]
  11. help_now!("'gpass' is required") unless options[:gpass]
  12. help_now!("'grepo' is required") unless options[:grepo]
  13. help_now!("'buser' is required") unless options[:buser]
  14. help_now!("'bpass' is required") unless options[:bpass]
  15. help_now!("'brepo' is required") unless options[:brepo]
  16. debug "Configuration Used: " + options.inspect
  17. # Connect to Bitbucket and Github
  18. bitbucket = Git2bit::BitbucketProxy.new({:user => options[:buser], :pass => options[:bpass], :org => options[:borg], :repo => options[:brepo]})
  19. github = Git2bit::GithubProxy.new({:user => options[:guser], :pass => options[:gpass], :org => options[:gorg], :repo => options[:grepo]})
  20. if options[:issue]
  21. info "Will only process Github issue ##{options[:issue]}"
  22. else
  23. if options[:closed]
  24. info "Starting to process through ALL Github issues"
  25. else
  26. info "Starting to process through only the OPENED Github issues"
  27. end
  28. end
  29. # Start processing each issue in Github
  30. issues = Hash.new
  31. github.each_issue(options[:closed]) { |issue|
  32. issues.store(issue.number, issue)
  33. }
  34. issues.keys.sort.each { |key|
  35. issue = issues[key]
  36. # Go to the next iteration if the issue number does not match option provided
  37. next if options[:issue] and options[:issue].to_i != issue.number
  38. info "Processing Github issue ##{issue.number}"
  39. if issue.milestone and !bitbucket.milestones.find_index(issue.milestone.title)
  40. # If there is a milestone and it does not already exist, then create it
  41. bitbucket.create_milestone(issue.milestone.title)
  42. end
  43. # Analyze the Github labels in an attempt to find the BitBucket data we need
  44. tags = bitbucket.analyze_labels issue.labels.map{|x| x.name}
  45. if tags[:status].nil? or issue.state == 'closed'
  46. # If a status was not defined in the github labels, then set to open/resolved
  47. # If the Github state is closed, we want to ignore the labels and set the ticket to resolved
  48. tags[:status] = issue.state == 'open' ? 'open' : 'resolved'
  49. end
  50. issue_date = DateTime.parse(issue.created_at)
  51. issue_body = "_Github issue **##{issue.number}** created by **#{issue.user.login}** on #{issue_date.strftime('%F %T')}_\n\n#{issue[:body]}"
  52. # Create the Bitbucket issue
  53. new_issue = bitbucket.create_issue({
  54. :title => issue.title,
  55. :content => issue_body,
  56. :responsible => issue.assignee,
  57. :milestone => issue.milestone ? issue.milestone.title : nil,
  58. :component => tags[:component],
  59. :priority => tags[:priority],
  60. :status => tags[:status],
  61. :kind => tags[:kind]
  62. })
  63. # Add the Github labels as a comment so the data is not lost
  64. bitbucket.create_comment new_issue, "**Github labels**: " + tags[:labels].join(', ')
  65. # Process Comments if the Github issue has any
  66. if issue.comments >= 1
  67. # Get the Github comments
  68. comments = github.get_comments issue.number
  69. comments.each do |comment|
  70. comment_date = DateTime.parse(comment.created_at)
  71. # Add the comment into the newly created bitbucket issue
  72. bitbucket.create_comment new_issue, "_Github comment by **#{comment.user.login}** on #{comment_date.strftime('%F %T')}_\n\n#{comment[:body]}"
  73. end
  74. end
  75. }
  76. info "Processing Complete."
  77. end
  78. # Declare command-line interface here
  79. version Git2bit::VERSION
  80. description 'git2bit migrates issues with their comments and milestones from a github repo to a bitbucket repo'
  81. on("--[no-]closed","[Do not] migrate closed issues")
  82. on("--issue ISSUE_NUMBER","Process only a single Github issue number")
  83. on("--guser USER","Github username")
  84. on("--gpass PASSWORD","Github password")
  85. on("--grepo REPO","Github repo name")
  86. on("--gorg ORG", "Github Organization name for repos owned by Organizations")
  87. on("--buser USER","BitBucket username")
  88. on("--bpass PASSWORD","BitBucket password")
  89. on("--brepo REPO","BitBucket repo name")
  90. use_log_level_option
  91. defaults_from_config_file '.git2bit.rc'
  92. go!
  93. end
  94. __END__
  95. # Sample .git2bit.rc file
  96. closed: true
  97. guser: joeworkman
  98. gpass: p@ssw0rd
  99. grepo: myrepo
  100. buser: joeworkman
  101. bpass: p@ssw0rd
  102. brepo: newrepo