PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/git2bit/bitbucket.rb

https://bitbucket.org/joeworkman/git2bit
Ruby | 146 lines | 106 code | 26 blank | 14 comment | 8 complexity | ed93206bd3c18be81490f4ae65998931 MD5 | raw file
  1. require 'date'
  2. require 'bitbucket_rest_api'
  3. require 'methadone'
  4. module Git2bit
  5. class BitbucketProxy
  6. include Methadone::Main
  7. include Methadone::CLILogging
  8. attr_reader :components, :milestones
  9. # Possible values from BitBucket
  10. STATUS = ['new','open','resolved','on hold','invalid','duplicate','wontfix']
  11. KINDS = ['bug','enhancement','proposal','task']
  12. PRIORITIES = ['trivial','minor','major','critical','blocker']
  13. def initialize(args)
  14. @repo = args[:repo]
  15. @user = args[:user]
  16. @org = args[:org]
  17. if @org
  18. @owner = @org
  19. else
  20. @owner = @user
  21. end
  22. # Connect to BitBucket
  23. begin
  24. @conn = BitBucket.new :basic_auth => "#{@user}:#{args[:pass]}", :user => @user, :repo => @repo
  25. # Store Milestones to search upon later on
  26. @milestones = Array.new
  27. @conn.issues.milestones.list(@owner, @repo).each {|m| @milestones.push m.name}
  28. debug "BitBucket Milestones:\n - " + @milestones.join("\n - ")
  29. # Store Components to search upon later on
  30. @components = Array.new
  31. @conn.issues.components.list(@owner, @repo).each {|c| @components.push c.name}
  32. debug "BitBucket Components:\n - " + @components.join("\n - ")
  33. info "Successfully connected to BitBucket #{@user}/#{@repo}"
  34. rescue Exception => e
  35. error = ["Error: Unable to connect to BitBucket #{@user}/#{@repo}", e.message].push e.backtrace
  36. exit_now!(error.join("\n"))
  37. end
  38. end
  39. def create_milestone(title)
  40. begin
  41. # Create the milestone in BitBucket
  42. milestone = @conn.issues.milestones.create @owner, @repo, { :name => title }
  43. # Add it to known milestones
  44. @milestones.push title
  45. info "BitBucket: created milestone '#{title}'"
  46. rescue Exception => e
  47. error "Error: Unable to create to BitBucket milestone '#{title}' - " + e.message
  48. debug e.backtrace.join("\n")
  49. end
  50. milestone
  51. end
  52. def create_issue(args)
  53. begin
  54. # Create the Bitbucket issue
  55. issue = @conn.issues.create @owner, @repo, {
  56. :title => args[:title],
  57. :content => args[:content],
  58. :responsible => args[:responsible],
  59. :milestone => args[:milestone],
  60. :component => args[:component],
  61. :priority => args[:priority],
  62. :status => args[:status],
  63. :kind => args[:kind]
  64. }
  65. info "BitBucket: created issue ##{issue.local_id} '#{args[:title]}'"
  66. rescue Exception => e
  67. error "Error: Unable to create to BitBucket issue '#{args[:title]}' - " + e.message
  68. debug e.backtrace.join("\n")
  69. end
  70. issue.local_id
  71. end
  72. def create_comment(id,content)
  73. begin
  74. # Create the Bitbucket comment
  75. comment = @conn.issues.comments.create @owner, @repo, id, {:content => content}
  76. info "BitBucket: created comment for issue ##{id}"
  77. rescue Exception => e
  78. error "Error: Unable to create to BitBucket comment for issue ##{id} - " + e.message
  79. debug e.backtrace.join("\n")
  80. end
  81. comment
  82. end
  83. def analyze_labels(labels)
  84. debug "Analyzing labels: " + labels.inspect
  85. # The tag values that we are going to try to discover
  86. tags = { :component => nil, :priority => nil, :kind => nil, :status => nil, :labels => Array.new }
  87. labels.each do |label|
  88. # Store the label name
  89. tags[:labels].push label
  90. name = label.downcase
  91. # Look for a priority in available fields in BB_PRIORITIES
  92. if tags[:priority].nil? and PRIORITIES.find_index(name)
  93. tags[:priority] = name
  94. next
  95. end
  96. # Look for a kind in available fields in BB_KINDS
  97. if tags[:kind].nil? and KINDS.find_index(name)
  98. tags[:kind] = name
  99. next
  100. end
  101. # Look for a status in available fields in BB_STATUS
  102. if tags[:status].nil? and STATUS.find_index(name)
  103. tags[:status] = name
  104. next
  105. end
  106. # Look for a component. Need to do case insensitive match on each known component.
  107. if tags[:component].nil?
  108. components.each do |c|
  109. if c.match(/#{name}/i)
  110. tags[:component] = c
  111. break
  112. end
  113. end
  114. end
  115. end
  116. debug "Built Tags: " + tags.inspect
  117. tags
  118. end
  119. end
  120. end