-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR Use changelog uri from rubygems metadata (#171)
- Loading branch information
Showing
29 changed files
with
1,467 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'nokogiri' | ||
require 'open-uri' | ||
require 'gem_updater/changelog_parser/github_parser' | ||
|
||
module GemUpdater | ||
# ChangelogParser is responsible for parsing a source page where | ||
# the gem code is hosted. | ||
class ChangelogParser | ||
MARKUP_FILES = %w[.md .rdoc .textile].freeze | ||
|
||
attr_reader :uri, :version | ||
|
||
# @param uri [String] uri of changelog | ||
# @param version [String] version of gem | ||
def initialize(uri:, version:) | ||
@uri = uri | ||
@version = version | ||
end | ||
|
||
# Get the changelog in an uri. | ||
# | ||
# @return [String, nil] URL of changelog | ||
def changelog | ||
return uri unless changelog_may_contain_anchor? | ||
|
||
parse_changelog | ||
rescue OpenURI::HTTPError # Uri points to nothing | ||
log_error_and_return_uri("Cannot find #{uri}") | ||
rescue Errno::ETIMEDOUT # timeout | ||
log_error_and_return_uri("#{URI.parse(uri).host} is down") | ||
rescue ArgumentError => e # x-oauth-basic raises userinfo not supported. [RFC3986] | ||
log_error_and_return_uri(e) | ||
end | ||
|
||
private | ||
|
||
# Try to find where changelog might be. | ||
# | ||
# @param doc [Nokogiri::XML::Element] document of source page | ||
def parse_changelog | ||
case URI.parse(uri).host | ||
when 'github.com' | ||
GithubParser.new(uri: uri, version: version).changelog | ||
else | ||
uri | ||
end | ||
end | ||
|
||
# Some documents like the one written in markdown may contain | ||
# a direct anchor to specific version. | ||
# | ||
# @return [Boolean] true if file may contain an anchor | ||
def changelog_may_contain_anchor? | ||
MARKUP_FILES.include?(File.extname(uri.to_s)) | ||
end | ||
|
||
def log_error_and_return_uri(error_message) | ||
Bundler.ui.error error_message | ||
uri | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'nokogiri' | ||
require 'open-uri' | ||
|
||
module GemUpdater | ||
class ChangelogParser | ||
# ChangelogParser is responsible for parsing a changelog hosted on github. | ||
class GithubParser | ||
attr_reader :uri, :version | ||
|
||
# @param uri [String] changelog uri | ||
# @param version [String] version of gem | ||
def initialize(uri:, version:) | ||
@uri = uri | ||
@version = version | ||
end | ||
|
||
# Finds anchor in changelog, otherwise return the base uri. | ||
# | ||
# @return [String] the URL of changelog | ||
def changelog | ||
uri + find_anchor(document).to_s | ||
end | ||
|
||
private | ||
|
||
# Opens changelog url and parses it. | ||
# | ||
# @return [Nokogiri::HTML4::Document] the changelog | ||
def document | ||
Nokogiri::HTML(URI.parse(uri).open, nil, Encoding::UTF_8.to_s) | ||
end | ||
|
||
# Looks into document to find it there is an anchor to new gem version. | ||
# | ||
# @param doc [Nokogiri::HTML4::Document] document | ||
# @return [String, nil] anchor's href | ||
def find_anchor(doc) | ||
anchor = doc.xpath('//a[contains(@class, "anchor")]').find do |element| | ||
element.attr('href').match(version.delete('.')) | ||
end | ||
return unless anchor | ||
|
||
anchor.attr('href').gsub(%(\\"), '') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.