PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/git2bit.rb

https://bitbucket.org/ckirby/git2bit
Ruby | 133 lines | 87 code | 24 blank | 22 comment | 10 complexity | 8aeeb82169f4fe6bcf014ac30ae686fd MD5 | raw file
  1. require 'date'
  2. require 'github_api'
  3. require 'bitbucket_rest_api'
  4. bb_owner = 'joeworkman'
  5. bb_repo = 'testinggit2bit'
  6. bb_user = 'joeworkman'
  7. bb_pass = 'dopey09'
  8. # Possible values from BitBucket
  9. BB_STATUS = ['new','open','resolved','on hold','invalid','duplicate','wontfix']
  10. BB_KINDS = ['bug','enhancement','proposal','task']
  11. BB_PRIORITIES = ['trivial','minor','major','critical','blocker']
  12. gh_repo = 'stacks'
  13. gh_user = 'joeworkman'
  14. gh_pass = 'dopey09'
  15. def analyze_labels(labels,components)
  16. # The tag values that we are going to try to discover
  17. tags = { :component => nil, :priority => nil, :kind => nil, :status => nil, :labels => Array.new }
  18. labels.each do |label|
  19. # Store the label name
  20. tags[:labels].push label.name
  21. name = label.name.downcase
  22. # Look for a priority in available fields in BB_PRIORITIES
  23. if tags[:priority].nil? and BB_PRIORITIES.find_index(name)
  24. tags[:priority] = name
  25. next
  26. end
  27. # Look for a kind in available fields in BB_KINDS
  28. if tags[:kind].nil? and BB_KINDS.find_index(name)
  29. tags[:kind] = name
  30. next
  31. end
  32. # Look for a status in available fields in BB_STATUS
  33. if tags[:status].nil? and BB_STATUS.find_index(name)
  34. tags[:status] = name
  35. next
  36. end
  37. # Look for a component. Need to do case insensitive match on each known component.
  38. if tags[:component].nil?
  39. components.each do |c|
  40. if c.match(/#{name}/i)
  41. tags[:component] = c
  42. break
  43. end
  44. end
  45. end
  46. end
  47. tags
  48. end
  49. # Connect to BitBucket
  50. bitbucket = BitBucket.new :basic_auth => "#{bb_user}:#{bb_pass}", :user => bb_user, :repo => bb_repo
  51. # Store Milestones to search upon later on
  52. bb_milestones = Array.new
  53. bitbucket.issues.milestones.list(bb_owner, bb_repo).each {|m| bb_milestones.push m.name}
  54. # Store Components to search upon later on
  55. bb_components = Array.new
  56. bitbucket.issues.components.list(bb_owner, bb_repo).each {|c| bb_components.push c.name}
  57. # Setup Github connection
  58. github = Github.new :basic_auth => "#{gh_user}:#{gh_pass}"
  59. # Get all open and closed issues
  60. github_data = Array.new
  61. github_data.push github.issues.list :filter => 'all', :state => 'open', :user => gh_user, :repo => gh_repo
  62. github_data.push github.issues.list :filter => 'all', :state => 'closed', :user => gh_user, :repo => gh_repo
  63. # Process through each page of github issues and then through each issue per page
  64. github_data.each do |result_set|
  65. result_set.each_page do |page|
  66. page.each do |issue|
  67. if issue.milestone and !bb_milestones.find_index(issue.milestone.title)
  68. # If there is a milestone and it does not already exist, then create it
  69. milestone = bitbucket.issues.milestones.create bb_owner, bb_repo, { :name => issue.milestone.title }
  70. # Add new milestone to array so its not created again
  71. bb_milestones.push milestone.name
  72. end
  73. # Analyze the Github labels in an attempt to find the BitBucket data we need
  74. tags = analyze_labels issue.labels, bb_components
  75. if tags[:status].nil? or issue.state == 'closed'
  76. # If a status was not defined in the github labels, then set to open/resolved
  77. # If the Github state is closed, we want to ignore the labels and set the ticket to resolved
  78. tags[:status] = issue.state == 'open' ? 'open' : 'resolved'
  79. end
  80. issue_date = DateTime.parse(issue.created_at)
  81. issue_body = "_Github issue **##{issue.number}** created by **#{issue.user.login}** on #{issue_date.strftime('%F %T')}_\n\n#{issue[:body]}"
  82. # Create the Bitbucket issue
  83. bb_issue = bitbucket.issues.create bb_owner, bb_repo, {
  84. :title => issue.title,
  85. :content => issue_body,
  86. :responsible => issue.assignee,
  87. :milestone => issue.milestone ? issue.milestone.title : nil,
  88. :component => tags[:component],
  89. :priority => tags[:priority],
  90. :status => tags[:status],
  91. :kind => tags[:kind]
  92. }
  93. labels = "**Github labels**: " + tags[:labels].join(', ')
  94. bitbucket.issues.comments.create bb_owner, bb_repo, bb_issue.local_id, {:content => labels}
  95. # Process Comments if the Github issue has any
  96. if issue.comments > 1
  97. # Get the Github comments
  98. comments = github.issues.comments.all gh_user, gh_repo, issue_id: issue.number
  99. comments.each do |comment|
  100. comment_date = DateTime.parse(comment.created_at)
  101. content = "_Github comment by **#{comment.user.login}** on #{comment_date.strftime('%F %T')}_\n\n#{comment[:body]}"
  102. # Add the comment into the newly created bitbucket issue
  103. bitbucket.issues.comments.create bb_owner, bb_repo, bb_issue.local_id, {:content => content}
  104. end
  105. end
  106. end
  107. exit
  108. end
  109. end