PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/git2bit/bitbucket.rb

https://bitbucket.org/ckirby/git2bit
Ruby | 140 lines | 101 code | 25 blank | 14 comment | 7 complexity | 6413c3ee6e0ae3b279ec96b5d7552a38 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. @owner = args[:user]
  17. # Connect to BitBucket
  18. begin
  19. @conn = BitBucket.new :basic_auth => "#{@user}:#{args[:pass]}", :user => @user, :repo => @repo
  20. # Store Milestones to search upon later on
  21. @milestones = Array.new
  22. @conn.issues.milestones.list(@owner, @repo).each {|m| @milestones.push m.name}
  23. debug "BitBucket Milestones:\n - " + @milestones.join("\n - ")
  24. # Store Components to search upon later on
  25. @components = Array.new
  26. @conn.issues.components.list(@owner, @repo).each {|c| @components.push c.name}
  27. debug "BitBucket Components:\n - " + @components.join("\n - ")
  28. info "Successfully connected to BitBucket #{@user}/#{@repo}"
  29. rescue Exception => e
  30. error = ["Error: Unable to connect to BitBucket #{@user}/#{@repo}", e.message].push e.backtrace
  31. exit_now!(error.join("\n"))
  32. end
  33. end
  34. def create_milestone(title)
  35. begin
  36. # Create the milestone in BitBucket
  37. milestone = @conn.issues.milestones.create @owner, @repo, { :name => title }
  38. # Add it to known milestones
  39. @milestones.push title
  40. info "BitBucket: created milestone '#{title}'"
  41. rescue Exception => e
  42. error "Error: Unable to create to BitBucket milestone '#{title}' - " + e.message
  43. debug e.backtrace.join("\n")
  44. end
  45. milestone
  46. end
  47. def create_issue(args)
  48. begin
  49. # Create the Bitbucket issue
  50. issue = @conn.issues.create @owner, @repo, {
  51. :title => args[:title],
  52. :content => args[:content],
  53. :responsible => args[:responsible],
  54. :milestone => args[:milestone],
  55. :component => args[:component],
  56. :priority => args[:priority],
  57. :status => args[:status],
  58. :kind => args[:kind]
  59. }
  60. info "BitBucket: created issue ##{issue.local_id} '#{args[:title]}'"
  61. rescue Exception => e
  62. error "Error: Unable to create to BitBucket issue '#{args[:title]}' - " + e.message
  63. debug e.backtrace.join("\n")
  64. end
  65. issue.local_id
  66. end
  67. def create_comment(id,content)
  68. begin
  69. # Create the Bitbucket comment
  70. comment = @conn.issues.comments.create @owner, @repo, id, {:content => content}
  71. info "BitBucket: created comment for issue ##{id}"
  72. rescue Exception => e
  73. error "Error: Unable to create to BitBucket comment for issue ##{id} - " + e.message
  74. debug e.backtrace.join("\n")
  75. end
  76. comment
  77. end
  78. def analyze_labels(labels)
  79. debug "Analyzing labels: " + labels.inspect
  80. # The tag values that we are going to try to discover
  81. tags = { :component => nil, :priority => nil, :kind => nil, :status => nil, :labels => Array.new }
  82. labels.each do |label|
  83. # Store the label name
  84. tags[:labels].push label
  85. name = label.downcase
  86. # Look for a priority in available fields in BB_PRIORITIES
  87. if tags[:priority].nil? and PRIORITIES.find_index(name)
  88. tags[:priority] = name
  89. next
  90. end
  91. # Look for a kind in available fields in BB_KINDS
  92. if tags[:kind].nil? and KINDS.find_index(name)
  93. tags[:kind] = name
  94. next
  95. end
  96. # Look for a status in available fields in BB_STATUS
  97. if tags[:status].nil? and STATUS.find_index(name)
  98. tags[:status] = name
  99. next
  100. end
  101. # Look for a component. Need to do case insensitive match on each known component.
  102. if tags[:component].nil?
  103. components.each do |c|
  104. if c.match(/#{name}/i)
  105. tags[:component] = c
  106. break
  107. end
  108. end
  109. end
  110. end
  111. debug "Built Tags: " + tags.inspect
  112. tags
  113. end
  114. end
  115. end