Class: Gub::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/gub/cli.rb

Instance Method Summary collapse

Instance Method Details

#add_upstreamObject



184
185
186
187
188
189
190
191
# File 'lib/gub/cli.rb', line 184

def add_upstream
  repository = Gub::Repository.new(origin_name)
  repository.add_upstream
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#browseObject



38
39
40
41
# File 'lib/gub/cli.rb', line 38

def browse
  repository = Gub::Repository.new(origin_name)
  repository.browse
end

#clone(repo) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/gub/cli.rb', line 166

def clone repo
  if options.https
    url = "https://github.com/#{repo}"
  else
    url = "[email protected]:#{repo}"
  end
  Gub.log.info "Cloning from #{url}..."
  Gub.git.clone(url)
  `cd #{repo.split('/').last}`
  repository = Gub::Repository.new(origin_name)
  repository.add_upstream
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#finish(id = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/gub/cli.rb', line 145

def finish id = nil
  id ||= `git rev-parse --abbrev-ref HEAD`.split('-').last.to_s.chop
  if id.nil?
    panic "Unable to guess issue ID from branch name. You might want to specify it explicitly."
  else
    repository = Repository.new(origin_name)
    issue = repository.issue(id)
    say 'Pushing branch...'
    Gub.git.push('origin', issue.branch)
    say "Creating pull-request for issue ##{id}..."
    issue.request_pull
    Gub.git.checkout('master')
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#infoObject



204
205
206
207
208
209
210
211
212
# File 'lib/gub/cli.rb', line 204

def info
  repo = Gub::Repository.new(origin_name)
  say "Github repository: #{repo.full_name}"
  say "Forked from: #{repo.parent}" if repo.parent
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#issue(id) ⇒ Object



48
49
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
# File 'lib/gub/cli.rb', line 48

def issue(id)
  repository = Gub::Repository.new(origin_name)
  if options.comment
    repository.issue(id).comment(options.comment)
  elsif options.close
    repository.issue(id).close
  elsif options.assign
    repository.issue(id).assign
  elsif options.reopen
    repository.issue(id).reopen
  else
    issue = repository.issue(id)
    rows = []
    rows << ['Status:', issue.state]
    rows << ['Milestone:', issue.milestone.title]
    rows << ['Author:', issue.user.]
    rows << ['Assignee:', (issue.assignee.nil? ? '-' : issue.assignee.)]
    rows << ['Description:', word_wrap(issue.body, line_width: 70)]
    comments = []
    issue.comments.each do |comment|
      comments << [comment.user., "On #{comment.updated_at}: #{comment.body}"]
    end
    Gub.log.info "Hint: use 'gub start #{id}' to start working on this issue."
    puts table rows, ["Issue ##{id}:", issue.title]
    puts table comments, ['', 'Comments']
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#issuesObject



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
# File 'lib/gub/cli.rb', line 83

def issues
  args = {}
  repository = Gub::Repository.new(origin_name)
  if options.all || repository.full_name.nil?
    say "Listing all issues:"
    issues = Gub.github.user_issues
  else
    if options.mine
      args[:assignee] = Gub.github.user.
    end
    if repository.has_issues?
      say "Listing issues for #{repository.full_name}:"
    else
      say "Issues disabled #{repository.full_name}.", :yellow
      say "Listing issues for #{repository.parent}:"
    end
    issues = repository.issues(args)
  end
  unless issues.nil?
    rows = []
    issues.each do |issue|
      row = []
      row << issue.number
      row << issue.title
      row << issue.user.
      row << (issue.assignee.nil? ? '' : issue.assignee.)
      row << issue.state
      rows << row
    end
    say table rows, ['ID', 'Title', 'Author', 'Assignee', 'Status']
    say "Found #{issues.count} issue(s)."
    say 'Hint: use "gub start" to start working on an issue.', :green
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#reposObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gub/cli.rb', line 14

def repos
  rows = []
  id = 0
  table = Terminal::Table.new(headings: ['#', 'Repository']) do |t|
    Gub.github.repos.each do |repo|
      id = id.next
      t.add_row [id, repo.full_name]
    end
    Gub.github.orgs.each do |org|
      t.add_separator
      Gub.github.organization_repositories(org.).each do |repo|
        id = id.next
        t.add_row [id, repo.full_name]
      end
    end
  end
  puts table
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#setupObject



215
216
217
218
219
220
221
222
223
224
# File 'lib/gub/cli.rb', line 215

def setup
  unless Gub.config.data && Gub.config.data.has_key?('token')
    hl = HighLine.new
    username = hl.ask 'Github username: '
    password = hl.ask('Github password (we will not store this): ') { |q| q.echo = "*" }
    gh = Gub::Github.new(login: username, password: password)
    token = gh.create_authorization(scopes: [:user, :repo, :gist], note: 'Gub').token
    Gub.config.add('token', token)
  end
end

#start(id) ⇒ Object



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

def start id
  if id.nil?
    panic 'Issue ID required.'
  else
    repository = Repository.new(origin_name)
    issue = repository.issue(id)
    if repository.branches.include?(issue.branch)
      Gub.git.checkout(issue.branch)
    else
      repository = Repository.new(origin_name)
      repository.sync
      issue.assign
      Gub.git.checkout('-b', issue.branch)
    end
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#syncObject



194
195
196
197
198
199
200
201
# File 'lib/gub/cli.rb', line 194

def sync
  Gub.log.info 'Synchroizing with upstream...'
  Gub::Repository.new(origin_name).sync
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end

#versionObject



227
228
229
# File 'lib/gub/cli.rb', line 227

def version
  say Gub::VERSION
end