Class: Ace::Review::Molecules::GhPrFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/review/molecules/gh_pr_fetcher.rb

Overview

Fetch PR diff and metadata via gh CLI

Class Method Summary collapse

Class Method Details

.fetch_diff(pr_identifier, options = {}) ⇒ Hash

Fetch PR diff content

Parameters:

  • pr_identifier (String)

    PR identifier (number, URL, or owner/repo#number)

  • options (Hash) (defaults to: {})

    Fetch options

Options Hash (options):

  • :max_retries (Integer)

    Maximum retry attempts (default: 3)

  • :initial_backoff (Integer)

    Initial backoff in seconds (default: 1)

  • :timeout (Integer)

    Timeout in seconds for gh CLI (default: 30)

Returns:

  • (Hash)

    Result with :success, :diff, :error



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
# File 'lib/ace/review/molecules/gh_pr_fetcher.rb', line 19

def self.fetch_diff(pr_identifier, options = {})
  # Parse identifier to get gh CLI format using ace-git
  parsed = Ace::Git::Atoms::PrIdentifierParser.parse(pr_identifier)
  gh_format = parsed.gh_format

  # Default timeout for PR diff operations
  timeout = options[:timeout] || 30

  # Fetch diff with retry logic
  result = Ace::Review::Atoms::RetryWithBackoff.execute(options) do
    Ace::Git::Molecules::GhCliExecutor.execute("pr", ["diff", gh_format], timeout: timeout)
  end

  if result[:success]
    {
      success: true,
      diff: result[:stdout],
      identifier: gh_format,
      parsed: parsed.to_h
    }
  else
    handle_fetch_error(result, pr_identifier)
  end
rescue Ace::Review::Errors::DiffTooLargeError
  # Fall back to local git diff when GitHub API rejects large diffs
  fetch_local_diff_fallback(pr_identifier, options)
rescue Ace::Review::Errors::GhCliNotInstalledError, Ace::Review::Errors::GhAuthenticationError,
  Ace::Git::GhNotInstalledError, Ace::Git::GhAuthenticationError
  # Re-raise authentication and installation errors
  raise
rescue => e
  {
    success: false,
    error: "Failed to fetch PR diff: #{e.message}"
  }
end

.fetch_metadata(pr_identifier, options = {}) ⇒ Hash

Fetch PR metadata (state, draft status, title, etc.)

Parameters:

  • pr_identifier (String)

    PR identifier

  • options (Hash) (defaults to: {})

    Fetch options

Options Hash (options):

  • :timeout (Integer)

    Timeout in seconds for gh CLI (default: 30)

Returns:

  • (Hash)

    Result with :success, :metadata, :error



62
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
# File 'lib/ace/review/molecules/gh_pr_fetcher.rb', line 62

def self.(pr_identifier, options = {})
  # Parse identifier using ace-git
  parsed = Ace::Git::Atoms::PrIdentifierParser.parse(pr_identifier)
  gh_format = parsed.gh_format

  # Default timeout for PR operations
  timeout = options[:timeout] || 30

  # Fetch metadata as JSON
  fields = "number,state,isDraft,title,body,author,headRefName,baseRefName,url"

  result = Ace::Review::Atoms::RetryWithBackoff.execute(options) do
    Ace::Git::Molecules::GhCliExecutor.execute("pr", ["view", gh_format, "--json", fields], timeout: timeout)
  end

  if result[:success]
     = JSON.parse(result[:stdout])
    {
      success: true,
      metadata: ,
      identifier: gh_format,
      parsed: parsed.to_h
    }
  else
    handle_fetch_error(result, pr_identifier)
  end
rescue JSON::ParserError => e
  {
    success: false,
    error: "Failed to parse PR metadata: #{e.message}"
  }
rescue Ace::Review::Errors::GhCliNotInstalledError, Ace::Review::Errors::GhAuthenticationError,
  Ace::Git::GhNotInstalledError, Ace::Git::GhAuthenticationError
  raise
rescue => e
  {
    success: false,
    error: "Failed to fetch PR metadata: #{e.message}"
  }
end

.fetch_pr(pr_identifier, options = {}) ⇒ Hash

Fetch both diff and metadata in one call

Parameters:

  • pr_identifier (String)

    PR identifier

  • options (Hash) (defaults to: {})

    Fetch options

Returns:

  • (Hash)

    Result with :success, :diff, :metadata, :error



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ace/review/molecules/gh_pr_fetcher.rb', line 108

def self.fetch_pr(pr_identifier, options = {})
  # Fetch diff and metadata
  diff_result = fetch_diff(pr_identifier, options)
  return diff_result unless diff_result[:success]

   = (pr_identifier, options)
  return  unless [:success]

  {
    success: true,
    diff: diff_result[:diff],
    metadata: [:metadata],
    identifier: diff_result[:identifier],
    parsed: diff_result[:parsed]
  }
end