Class: GitIssue::Github

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

Constant Summary

Constants inherited from Base

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 = {}) ⇒ Github

Returns a new instance of Github.



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/github.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(/github.com[:\/](.+)\.git/)[1]
  end

  @user = options[:user] || configured_value('issue.user')
  @user = global_configured_value('github.user') if @user.blank?
  @user = Pit.get("github", :require => {
      "user" => "Your user name in GitHub",
  })["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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/git_issue/github.rb', line 105

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

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

### descriptions here ###
  MSG

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

  json = build_issue_json(options, property_names)
  url = to_url("repos", @repo, 'issues')

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

#branch(options = {}) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/git_issue/github.rb', line 161

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



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

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

  body = options[:body] || get_body_from_editor("### comment here ###")

  json = {:state => 'closed' }
  url = to_url("repos", @repo, 'issues', ticket)

  issue = post_json(url, json, options)

  comment_json = { :body => body }
  comment_url = to_url("repos", @repo, 'issues', ticket, 'comments')
  post_json(comment_url, comment_json, options)

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

#commandsObject



34
35
36
37
38
# File 'lib/git_issue/github.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



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
94
95
96
97
98
99
# File 'lib/git_issue/github.rb', line 65

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

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

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

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

  t_max = issues.map{|i| mlength(i['title'])}.max
  l_max = issues.map{|i| mlength(i['labels'].map{|l| l['name']}.join(","))}.max
  u_max = issues.map{|i| mlength(i['user']['login'])}.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['number'].to_i)),
                 apply_fmt_colors(:state, i['state']),
                 mljust(i['title'], t_max),
                 apply_fmt_colors(:login, mljust(i['user']['login'], u_max)),
                 apply_fmt_colors(:labels, mljust(i['labels'].map{|l| l['name']}.join(','), l_max)),
                 or_zero.call(i['comments']),
                 or_zero.call(i['votes']),
                 or_zero.call(i['position']),
                 to_date(i['created_at']),
                 to_date(i['updated_at'])
                 )
  end

end

#mention(options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/git_issue/github.rb', line 145

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

  body = options[:body] || get_body_from_editor("### comment here ###")
  raise 'comment body is required.' if body.empty?

  json = { :body => body }
  url = to_url("repos", @repo, 'issues', ticket, 'comments')

  issue = post_json(url, json, options)

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

#mine(options = {}) ⇒ Object



101
102
103
# File 'lib/git_issue/github.rb', line 101

def mine(options = {})
  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/github.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['comments'].to_i > 0
      comments = fetch_comments(ticket) unless options[:supperss_comments]
    end
    puts ""
    puts format_issue(issue, comments, options)
  end
end

#update(options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/git_issue/github.rb', line 125

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

  property_names = [:title, :body, :assignee, :milestone, :labels, :state]

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

  json = build_issue_json(options, property_names)
  url = to_url("repos", @repo, 'issues', ticket)

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

#view(options = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/git_issue/github.rb', line 58

def view(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket
  url = URI.join('https://github.com/', [@user, @repo, 'issues', ticket].join("/"))
  system "git web--browse #{url}"
end