Class: GitIssue::Bitbucket

Inherits:
Base
  • Object
show all
Defined in:
lib/git_issue/bitbucket.rb

Constant Summary

Constants inherited from Base

GitIssue::Base::BRANCH_NAME_FORMAT

Constants included from Helper

Helper::CONFIGURE_MESSAGE

Instance Attribute Summary

Attributes inherited from Base

#apikey, #command, #options, #syserr, #sysout, #tickets

Instance Method Summary collapse

Methods inherited from Base

#apply_colors, #cherry, #connection, #current_branch, #default_cmd, #err, #execute, #exit_with_message, #find_command, #guess_ticket, #help, #mktmpdir, #mlength, #mljust, #parse_command_and_tickets, #parse_options, #prompt, #publish, #puts, #rebase, #response_success?, #ticket_and_branch, #ticket_branch, #time_ago_in_words, #to_date, #usage

Methods included from Helper

configure_error, configured_value, get_body_from_editor, get_title_and_body_from_editor, #git_editor, global_configured_value, its_klass_of, #open_editor, #read_body, #split_head_and_body, #work_dir

Constructor Details

#initialize(args, options = {}) ⇒ Bitbucket

Returns a new instance of Bitbucket.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/git_issue/bitbucket.rb', line 7

def initialize(args, options = {})
  super(args, options)

  @repo = configured_value('issue.repo')
  if @repo.blank?
    url = `git config remote.origin.url`.strip
    @repo = url.match(/bitbucket.org[:\/](.+)\.git/)[1]
  end

  @user = options[:user] || configured_value('issue.user')
  @user = global_configured_value('bitbucket.user') if @user.blank?
  @user = Pit.get("bitbucket", :require => {
      "user" => "Your user name in Bitbucket",
  })["user"] if @user.blank?

  configure_error('user', "git config issue.user yuroyoro")  if @user.blank?
  @ssl_options = {}
  if @options.key?(:sslNoVerify) && RUBY_VERSION < "1.9.0"
    @ssl_options[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
  elsif configured_value('http.sslVerify') == "false"
    @ssl_options[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
  end
  if (ssl_cert = configured_value('http.sslCert'))
    @ssl_options[:ssl_ca_cert] = ssl_cert
  end
end

Instance Method Details

#add(options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/git_issue/bitbucket.rb', line 101

def add(options = {})
  property_names = [:title, :content, :assignee, :milestone, :labels]

  message = <<-MSG
### Write title here ###

### descriptions here ###
  MSG

  unless options[:title]
    options[:title], options[:content] = get_title_and_body_from_editor(message)
  end

  url = to_url("repositories", @repo, 'issues')

  issue = post_json(url, nil, options)
  puts "created issue #{oneline_issue(issue)}"
end

#branch(options = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/git_issue/bitbucket.rb', line 158

def branch(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  branch_name = ticket_branch(ticket)

  if options[:force]
    system "git branch -D #{branch_name}" if options[:force]
    system "git checkout -b #{branch_name}"
  else
    if %x(git branch -l | grep "#{branch_name}").strip.empty?
      system "git checkout -b #{branch_name}"
    else
      system "git checkout #{branch_name}"
    end
  end

  show(options)
end

#close(options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/git_issue/bitbucket.rb', line 178

def close(options = {})

  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  unless options[:content]
    options[:content] = get_body_from_editor("### comment here ###")
  end

  options[:status] = "resolved" unless options[:status]

  url = to_url("repositories", @repo, 'issues', ticket)

  issue = put_json(url, nil, options)

  comment_url = to_url("repositories", @repo, 'issues', ticket, 'comments')
  post_json(comment_url, nil, options)

  puts "closed issue #{oneline_issue(issue)}"
end

#commandsObject



34
35
36
37
38
# File 'lib/git_issue/bitbucket.rb', line 34

def commands
  cl = super
  cl << GitIssue::Command.new(:mention, :men, 'create a comment to given issue')
  cl << GitIssue::Command.new(:close , :cl, 'close an issue with comment. comment is optional.')
end

#list(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/git_issue/bitbucket.rb', line 58

def list(options = {})
  state = options[:status] || "open"

  query_names = [:status, :milestone, :assignee, :mentioned, :labels, :sort, :direction]
  params = query_names.inject({}){|h,k| h[k] = options[k] if options[k];h}
  params[:status] ||= "open"
  params[:per_page] = options[:max_count] || 30

  url = to_url("repositories", @repo, 'issues')

  issues = fetch_json(url, options, params)
  issues = issues['issues']
  issues = issues.sort_by{|i| i['local_id']} unless params[:sort] || params[:direction]

  t_max = issues.map{|i| mlength(i['title'])}.max
  l_max = issues.map{|i| mlength(i['metadata']['kind'])}.max
  u_max = issues.map{|i| mlength(i['reported_by']['username'])}.max

  or_zero = lambda{|v| v.blank? ? "0" : v }

  issues.each do |i|
    puts sprintf("%s  %s  %s  %s  %s c:%s v:%s p:%s %s %s",
                 apply_fmt_colors(:id, sprintf('#%-4d', i['local_id'])),
                 apply_fmt_colors(:state, i['status']),
                 mljust(i['title'], t_max),
                 apply_fmt_colors(:login, mljust(i['reported_by']['username'], u_max)),
                 apply_fmt_colors(:labels, mljust(i['metadata']['kind'], l_max)),
                 or_zero.call(i['comment_count']),
                 or_zero.call(i['votes']),
                 or_zero.call(i['position']),
                 to_date(i['created_on']),
                 to_date(i['utc_last_updated'])
                 )
  end

end

#mention(options = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/git_issue/bitbucket.rb', line 140

def mention(options = {})

  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  unless options[:content]
    options[:content] = get_body_from_editor("### comment here ###")
  end
  raise 'comment content is required.' unless options[:content]

  url = to_url("repositories", @repo, 'issues', ticket, 'comments')

  issue = post_json(url, nil, options)

  issue = fetch_issue(ticket)
  puts "commented issue #{oneline_issue(issue)}"
end

#mine(options = {}) ⇒ Object



95
96
97
98
99
# File 'lib/git_issue/bitbucket.rb', line 95

def mine(options = {})
  raise "Not implemented yet."

  list(options.merge(:assignee => @user))
end

#show(options = {}) ⇒ Object



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

def show(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket
  issue = fetch_issue(ticket, options)

  if options[:oneline]
    puts oneline_issue(issue, options)
  else
    comments = []

    if issue['comment_count'] > 0
      comments = fetch_comments(ticket) unless options[:supperss_comments]
    end
    puts ""
    puts format_issue(issue, comments, options)
  end
end

#update(options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/git_issue/bitbucket.rb', line 120

def update(options = {})

  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  property_names = [:title, :content, :assignee, :milestone, :labels, :status]

  if options.slice(*property_names).empty?
    issue = fetch_issue(ticket)
    message = "#{issue['title']}\n\n#{issue['content']}"
    options[:title], options[:content] = get_title_and_body_from_editor(message)
  end

  url = to_url("repositories", @repo, 'issues', ticket)

  issue = put_json(url, nil, options) # use POST instead of PATCH.
  puts "updated issue #{oneline_issue(issue)}"
end