Class: Danger::Changelog::PRMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/changelog/pr_metadata.rb

Constant Summary collapse

EXAMPLE_PR_JSON =
{
  'number' => 123,
  'html_url' => 'https://github.com/org/repo/pull/123'
}.freeze
EXAMPLE_TITLE =
'Your contribution'.freeze
EXAMPLE_AUTHOR =
'username'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pr_json:, pr_title:, pr_author:) ⇒ PRMetadata

Returns a new instance of PRMetadata.



13
14
15
16
17
# File 'lib/changelog/pr_metadata.rb', line 13

def initialize(pr_json:, pr_title:, pr_author:)
  @pr_json = pr_json
  @pr_title = pr_title
  @pr_author = pr_author
end

Instance Attribute Details

#pr_authorObject (readonly)

Returns the value of attribute pr_author.



11
12
13
# File 'lib/changelog/pr_metadata.rb', line 11

def pr_author
  @pr_author
end

#pr_jsonObject (readonly)

Returns the value of attribute pr_json.



11
12
13
# File 'lib/changelog/pr_metadata.rb', line 11

def pr_json
  @pr_json
end

#pr_titleObject (readonly)

Returns the value of attribute pr_title.



11
12
13
# File 'lib/changelog/pr_metadata.rb', line 11

def pr_title
  @pr_title
end

Class Method Details

.fallbackObject



55
56
57
58
59
60
61
# File 'lib/changelog/pr_metadata.rb', line 55

def self.fallback
  new(
    pr_json: EXAMPLE_PR_JSON,
    pr_title: EXAMPLE_TITLE,
    pr_author: EXAMPLE_AUTHOR
  )
end

.from_event_file(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/changelog/pr_metadata.rb', line 40

def self.from_event_file(path)
  return nil unless path
  return nil unless File.exist?(path)

  event = JSON.parse(File.read(path))
  pr = event['pull_request']
  return nil unless pr

  new(
    pr_json: pr,
    pr_title: pr['title'],
    pr_author: pr.dig('user', 'login')
  )
end

.from_github_plugin(github = nil) ⇒ Object

Attempt to fetch PR metadata from the GitHub plugin. Returns nil if GitHub API access fails for any reason (authentication, rate limiting, network errors, etc). This allows the fallback chain to proceed to other sources like GITHUB_EVENT_PATH.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/changelog/pr_metadata.rb', line 22

def self.from_github_plugin(github = nil)
  return nil unless github

  begin
    pr_json = github.pr_json
    return nil unless pr_json

    new(
      pr_json: pr_json,
      pr_title: github.pr_title,
      pr_author: github.pr_author
    )
  rescue Octokit::Error => e
    warn "[Changelog::PRMetadata] GitHub API request failed (#{e.class}: #{e.message}). Falling back to other PR metadata sources."
    nil
  end
end