Class: MakersToolbelt::FetchPullRequests

Inherits:
Object
  • Object
show all
Defined in:
lib/makers_toolbelt/fetch_pull_requests.rb

Constant Summary collapse

OPEN_SWITCHES =
['-a', '--all', '-o', '--open']
CLOSED_SWITCHES =
['-a', '--all', '-c', '--closed']
GITHUB_REMOTE_REGEX =
/github\.com(?::|\/)?(.+\/[\w\-]+)(?:\.git|\/)?\n/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FetchPullRequests

Returns a new instance of FetchPullRequests.


12
13
14
15
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 12

def initialize(options = {})
  @options = options
  @filter = options[:filter] || '--open'
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.


10
11
12
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 10

def filter
  @filter
end

#optionsObject (readonly)

Returns the value of attribute options.


10
11
12
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 10

def options
  @options
end

Instance Method Details

#include_closed?Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 47

def include_closed?
  CLOSED_SWITCHES.include? filter
end

#include_open?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 43

def include_open?
  OPEN_SWITCHES.include? filter
end

#repoObject


32
33
34
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 32

def repo
  @repo ||= repo_from_remote_path(origin_path)
end

#repo_from_remote_path(path) ⇒ Object


36
37
38
39
40
41
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 36

def repo_from_remote_path(path)
  @regex ||= GITHUB_REMOTE_REGEX
  @regex.match(path).captures.first
rescue
  fail NotFoundError.new('Could not find remote origin')
end

#runObject


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/makers_toolbelt/fetch_pull_requests.rb', line 17

def run()
  pulls = []
  pulls << open_pull_requests if include_open?
  pulls << closed_pull_requests if include_closed?

  pulls.flatten!.select! {|p| p.head.repo }

  pulls.map { |p| [p.head.repo.owner., p.head.repo.html_url] }.each do |name, url|
    puts "adding #{name}"
    `git remote add #{name} #{url}`
    puts "fetching #{url}"
    `git fetch #{name}`
  end
end