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

#current_branch, #default_cmd, #err, #execute, #exit_with_message, #find_command, #guess_ticket, #help, #mktmpdir, #mlength, #mljust, #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, global_configured_value, its_klass_of

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
# File 'lib/git_issue/github.rb', line 7

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

  @apikey = options[:apikey] || configured_value('apikey')
  @apikey = global_configured_value('github.token') if @apikey.blank?
  configure_error('apikey', "git config issue.apikey some_api_key") if @apikey.blank?

  url = `git config remote.origin.url`.strip
  @repo = url.match(/github.com[:\/](.+)\.git/)[1]

  @user = options[:user] || configured_value('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?
  @sslNoVerify = @options[:sslNoVerify] &&  RUBY_VERSION < '1.9.0'
end

Instance Method Details

#add(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/git_issue/github.rb', line 89

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

  required_properties = [:title]
  required_properties.each do |name|
    options[name] = prompt(name) unless options[name]
  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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/git_issue/github.rb', line 134

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

#commandsObject



27
28
29
30
# File 'lib/git_issue/github.rb', line 27

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

#list(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
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
# File 'lib/git_issue/github.rb', line 50

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"

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

  issues = fetch_json(url, 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("#%-4d  %s  %s  %s  %s c:%s v:%s p:%s %s %s",
                 i['number'].to_i,
                 i['state'],
                 mljust(i['title'], t_max),
                 mljust(i['user']['login'], u_max),
                 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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/git_issue/github.rb', line 118

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

  body = options[:body]
  raise 'commnet body is required.' unless body

  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



85
86
87
# File 'lib/git_issue/github.rb', line 85

def mine(options = {})
  list(options.merge(:assignee => @user))
end

#show(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_issue/github.rb', line 32

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



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/git_issue/github.rb', line 104

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

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

  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