Class: Git2bit::BitbucketProxy

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::Main
Defined in:
lib/git2bit/bitbucket.rb

Constant Summary collapse

STATUS =

Possible values from BitBucket

['new','open','resolved','on hold','invalid','duplicate','wontfix']
KINDS =
['bug','enhancement','proposal','task']
PRIORITIES =
['trivial','minor','major','critical','blocker']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ BitbucketProxy

Returns a new instance of BitbucketProxy.



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
# File 'lib/git2bit/bitbucket.rb', line 17

def initialize(args)
	@repo = args[:repo]
	@user = args[:user]
	@owner = args[:user]

	# Connect to BitBucket
	begin
		@conn = BitBucket.new :basic_auth => "#{@user}:#{args[:pass]}", :user => @user, :repo => @repo

		# Store Milestones to search upon later on
		@milestones = Array.new
		@conn.issues.milestones.list(@owner, @repo).each {|m| @milestones.push m.name}
		debug "BitBucket Milestones:\n - " + @milestones.join("\n - ")

		# Store Components to search upon later on
		@components = Array.new
		@conn.issues.components.list(@owner, @repo).each {|c| @components.push c.name}
		debug "BitBucket Components:\n - " + @components.join("\n - ")

		info "Successfully connected to BitBucket #{@user}/#{@repo}"
   rescue Exception => e 
     error = ["Error: Unable to connect to BitBucket #{@user}/#{@repo}", e.message].push e.backtrace
     exit_now!(error.join("\n"))
   end
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



10
11
12
# File 'lib/git2bit/bitbucket.rb', line 10

def components
  @components
end

#milestonesObject (readonly)

Returns the value of attribute milestones.



10
11
12
# File 'lib/git2bit/bitbucket.rb', line 10

def milestones
  @milestones
end

Instance Method Details

#analyze_labels(labels) ⇒ Object



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
# File 'lib/git2bit/bitbucket.rb', line 92

def analyze_labels(labels)
	debug "Analyzing labels: " + labels.inspect

	# The tag values that we are going to try to discover
	tags = { :component => nil, :priority => nil, :kind => nil, :status => nil, :labels => Array.new }
	
	labels.each do |label|
		# Store the label name
		tags[:labels].push label

		name = label.downcase
		
		# Look for a priority in available fields in BB_PRIORITIES
		if tags[:priority].nil? and PRIORITIES.find_index(name)
			tags[:priority] = name
			next
		end

		# Look for a kind in available fields in BB_KINDS
		if tags[:kind].nil? and KINDS.find_index(name)
			tags[:kind] = name
			next
		end

		# Look for a status in available fields in BB_STATUS
		if tags[:status].nil? and STATUS.find_index(name)
			tags[:status] = name
			next
		end

		# Look for a component. Need to do case insensitive match on each known component.
		if tags[:component].nil?
			components.each do |c|
				if c.match(/#{name}/i)
					tags[:component] = c
					break
				end
			end
		end

	end 
	debug "Built Tags: " + tags.inspect

	tags
end

#create_comment(id, content) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/git2bit/bitbucket.rb', line 80

def create_comment(id,content)
	begin
     # Create the Bitbucket comment
    	comment = @conn.issues.comments.create @owner, @repo, id, {:content => content}
		info "BitBucket: created comment for issue ##{id}"
   rescue Exception => e 
     error "Error: Unable to create to BitBucket comment for issue ##{id} - " + e.message
     debug e.backtrace.join("\n")
   end
   comment
end

#create_issue(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/git2bit/bitbucket.rb', line 59

def create_issue(args)
	begin
     # Create the Bitbucket issue
     issue = @conn.issues.create @owner, @repo, {
       :title => args[:title], 
       :content => args[:content], 
       :responsible => args[:responsible], 
       :milestone => args[:milestone], 
       :component => args[:component],
       :priority => args[:priority],
       :status => args[:status],
       :kind => args[:kind]
     }
		info "BitBucket: created issue ##{issue.local_id} '#{args[:title]}'"
   rescue Exception => e 
     error "Error: Unable to create to BitBucket issue '#{args[:title]}' - " + e.message
     debug e.backtrace.join("\n")
   end
   issue.local_id
end

#create_milestone(title) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/git2bit/bitbucket.rb', line 43

def create_milestone(title)
	begin
		# Create the milestone in BitBucket
      milestone = @conn.issues.milestones.create @owner, @repo, { :name => title }
     
      # Add it to known milestones
		@milestones.push title

		info "BitBucket: created milestone '#{title}'"
   rescue Exception => e 
     error "Error: Unable to create to BitBucket milestone '#{title}' - " + e.message
     debug e.backtrace.join("\n")
   end
   milestone
end