Class: PullRequestAi::Repo::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/pull_request_ai/repo/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt: Prompt.new) ⇒ Reader

Returns a new instance of Reader.



10
11
12
# File 'lib/pull_request_ai/repo/reader.rb', line 10

def initialize(prompt: Prompt.new)
  @prompt = prompt
end

Instance Attribute Details

#promptObject

Returns the value of attribute prompt.



8
9
10
# File 'lib/pull_request_ai/repo/reader.rb', line 8

def prompt
  @prompt
end

Instance Method Details

#current_branchObject



14
15
16
# File 'lib/pull_request_ai/repo/reader.rb', line 14

def current_branch
  prompt.configured? ? Success(prompt.current_branch) : Error.failure(:project_not_configured)
end

#current_changes(to_base) ⇒ Object



68
69
70
71
72
# File 'lib/pull_request_ai/repo/reader.rb', line 68

def current_changes(to_base)
  current_branch.bind do |current|
    changes_between(to_base, current)
  end
end

#destination_branchesObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pull_request_ai/repo/reader.rb', line 56

def destination_branches
  current_branch.bind do |current|
    remote_branches.bind do |branches|
      if branches.include?(current)
        Success(branches.reject { _1 == current || _1.start_with?('HEAD') })
      else
        Error.failure(:current_branch_not_pushed)
      end
    end
  end
end

#flatten_current_changes(to_base) ⇒ Object



74
75
76
77
78
# File 'lib/pull_request_ai/repo/reader.rb', line 74

def flatten_current_changes(to_base)
  current_changes(to_base).bind do |changes|
    Success(changes.inject(''.dup) { |result, file| result << file.trimmed_modified_lines })
  end
end

#remote_branchesObject



46
47
48
49
50
51
52
53
54
# File 'lib/pull_request_ai/repo/reader.rb', line 46

def remote_branches
  remote_name.bind do |name|
    branches = prompt.remote_branches
      .reject { !_1.strip.start_with?(name) }
      .map { _1.strip.sub(%r{\A#{name}/}, '') }
      .reject(&:empty?)
    Success(branches)
  end
end

#remote_nameObject



18
19
20
# File 'lib/pull_request_ai/repo/reader.rb', line 18

def remote_name
  prompt.configured? ? Success(prompt.remote_name) : Error.failure(:project_not_configured)
end

#remote_uriObject



36
37
38
39
40
41
42
43
44
# File 'lib/pull_request_ai/repo/reader.rb', line 36

def remote_uri
  remote_name.bind do |name|
    url = prompt.remote_url(name)
    uri = GitCloneUrl.parse(url)
    Success(uri)
  end
rescue URI::InvalidComponentError
  Error.failure(:invalid_repository_url)
end

#repository_hostObject



30
31
32
33
34
# File 'lib/pull_request_ai/repo/reader.rb', line 30

def repository_host
  remote_uri.bind do |uri|
    Success(uri.host)
  end
end

#repository_slugObject



22
23
24
25
26
27
28
# File 'lib/pull_request_ai/repo/reader.rb', line 22

def repository_slug
  remote_uri.bind do |uri|
    regex = %r{\A/?(?<slug>.*?)(?:\.git)?\Z}
    match = regex.match(uri.path)
    match ? Success(match[:slug]) : Error.failure(:invalid_repository_url)
  end
end