Class: GHI::Commands::Edit

Inherits:
Command
  • Object
show all
Defined in:
lib/ghi/commands/edit.rb

Constant Summary

Constants included from Formatting

Formatting::CURSOR, Formatting::THROBBERS

Constants included from Formatting::Colors

Formatting::Colors::ANSI, Formatting::Colors::WEB

Instance Attribute Summary collapse

Attributes inherited from Command

#action, #args, #verbose

Attributes included from Formatting

#paging

Instance Method Summary collapse

Methods inherited from Command

#api, #assigns, execute, #initialize, #repo, #repo=

Methods included from Formatting

#columns, #dimensions, #format_comment, #format_comment_editor, #format_comments_and_events, #format_date, #format_editor, #format_event, #format_event_type, #format_issue, #format_issues, #format_issues_header, #format_labels, #format_markdown, #format_milestone, #format_milestone_editor, #format_milestones, #format_number, #format_state, #format_tag, #format_username, #indent, #page, #paginate?, #paging?, #past_due?, #percent, #puts, #throb, #truncate

Methods included from Formatting::Colors

#bg, #blink, #bright, #colorize?, colorize?, #fg, #highlight, #inverse, #no_color, #to_hex, #underline

Constructor Details

This class inherits a constructor from GHI::Commands::Command

Instance Attribute Details

#editorObject

Returns the value of attribute editor.



4
5
6
# File 'lib/ghi/commands/edit.rb', line 4

def editor
  @editor
end

Instance Method Details

#executeObject



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
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/ghi/commands/edit.rb', line 63

def execute
  self.action = 'edit'
  require_repo
  require_issue
  options.parse! args
  case action
  when 'edit'
    begin
      if editor || assigns.empty?
        i = throb { api.get "/repos/#{repo}/issues/#{issue}" }.body
        e = Editor.new "GHI_ISSUE_#{issue}"
        message = e.gets format_editor(i)
        e.unlink "There's no issue." if message.nil? || message.empty?
        assigns[:title], assigns[:body] = message.split(/\n+/, 2)
      end
      if i && assigns.keys.map { |k| k.to_s }.sort == %w[body title]
        titles_match = assigns[:title].strip == i['title'].strip
        if assigns[:body]
          bodies_match = assigns[:body].to_s.strip == i['body'].to_s.strip
        end
        if titles_match && bodies_match
          e.unlink if e
          abort 'No change.' if assigns.dup.delete_if { |k, v|
            [:title, :body].include? k
          }
        end
      end
      unless assigns.empty?
        i = throb {
          api.patch "/repos/#{repo}/issues/#{issue}", assigns
        }.body
        puts format_issue(i)
        puts 'Updated.'
      end
      e.unlink if e
    rescue Client::Error => e
      raise unless error = e.errors.first
      abort "%s %s %s %s." % [
        error['resource'],
        error['field'],
        [*error['value']].join(', '),
        error['code']
      ]
    end
  when 'pull'
    begin
      assigns[:issue] = issue
      assigns[:base] ||= 'master'
      head = begin
        if ref = %x{
          git rev-parse --abbrev-ref HEAD@{upstream} 2>/dev/null
        }.chomp!
          ref.split('/', 2).last if $? == 0
        end
      end
      assigns[:head] ||= head
      if assigns[:head]
        assigns[:head].sub!(/:$/, ":#{head}")
      else
        abort <<EOF.chomp
fatal: HEAD can't be null. (Is your current branch being tracked upstream?)
EOF
      end
      throb { api.post "/repos/#{repo}/pulls", assigns }
      base = [repo.split('/').first, assigns[:base]].join ':'
      puts 'Issue #%d set up to track remote branch %s against %s.' % [
        issue, assigns[:head], base
      ]
    rescue Client::Error => e
      raise unless error = e.errors.last
      abort error['message'].sub(/^base /, '')
    end
  end
end

#optionsObject



6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ghi/commands/edit.rb', line 6

def options
  OptionParser.new do |opts|
    opts.banner = <<EOF
usage: ghi edit <issueno> [options]
EOF
    opts.separator ''
    opts.on(
      '-m', '--message [<text>]', 'change issue description'
    ) do |text|
      next self.editor = true if text.nil?
      assigns[:title], assigns[:body] = text.split(/\n+/, 2)
    end
    opts.on(
      '-u', '--[no-]assign [<user>]', 'assign to specified user'
    ) do |assignee|
      assigns[:assignee] = assignee || nil
    end
    opts.on '--claim', 'assign to yourself' do
      assigns[:assignee] = Authorization.username
    end
    opts.on(
      '-s', '--state <in>', %w(open closed),
      {'o'=>'open', 'c'=>'closed'}, "'open' or 'closed'"
    ) do |state|
      assigns[:state] = state
    end
    opts.on(
      '-M', '--[no-]milestone [<n>]', Integer, 'associate with milestone'
    ) do |milestone|
      assigns[:milestone] = milestone
    end
    opts.on(
      '-L', '--label <labelname>...', Array, 'associate with label(s)'
    ) do |labels|
      (assigns[:labels] ||= []).concat labels
    end
    opts.separator ''
    opts.separator 'Pull request options'
    opts.on(
      '-H', '--head [[<user>:]<branch>]',
      'branch where your changes are implemented',
      '(defaults to current branch)'
    ) do |head|
      self.action = 'pull'
      assigns[:head] = head
    end
    opts.on(
      '-b', '--base [<branch>]',
      'branch you want your changes pulled into', '(defaults to master)'
    ) do |base|
      self.action = 'pull'
      assigns[:base] = base
    end
    opts.separator ''
  end
end