Class: BundleUpdateInteractive::ChangelogLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/bundle_update_interactive/changelog_locator.rb

Instance Method Summary collapse

Instance Method Details

#find_changelog_uri(name:, version: nil) ⇒ Object

TODO: refactor



14
15
16
17
18
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
# File 'lib/bundle_update_interactive/changelog_locator.rb', line 14

def find_changelog_uri(name:, version: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  if version
    response = Faraday.get("https://rubygems.org/api/v2/rubygems/#{name}/versions/#{version}.json")
    version = nil unless response.success?
  end

  response = Faraday.get("https://rubygems.org/api/v1/gems/#{name}.json") if version.nil?

  return nil unless response.success?

  data = JSON.parse(response.body)

  version ||= data["version"]
  changelog_uri = data["changelog_uri"]
  github_repo = guess_github_repo(data)

  if changelog_uri.nil? && github_repo
    file_list = Faraday.get("https://github.com/#{github_repo}")
    if file_list.status == 301
      github_repo = file_list.headers["Location"][GITHUB_PATTERN, 1]
      file_list = Faraday.get(file_list.headers["Location"])
    end
    match = file_list.body.match(%r{/(#{github_repo}/blob/[^/]+/#{FILE_PATTERN}(?:\.#{EXT_PATTERN})?)"}i)
    changelog_uri = "https://github.com/#{match[1]}" if match
  end

  if changelog_uri.nil? && github_repo
    releases_uri = "https://github.com/#{github_repo}/releases"
    changelog_uri = releases_uri if Faraday.head("#{releases_uri}/tag/v#{version}").success?
  end

  changelog_uri
end