2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
module Gitlab
|
|
|
|
module DependencyLinker
|
2017-05-16 16:25:57 -04:00
|
|
|
class GemfileLinker < MethodLinker
|
2019-02-15 14:11:16 -05:00
|
|
|
class_attribute :package_keyword
|
|
|
|
|
|
|
|
self.package_keyword = :gem
|
2017-05-12 15:31:05 -04:00
|
|
|
self.file_type = :gemfile
|
2016-07-10 17:13:06 -04:00
|
|
|
|
2019-02-15 14:11:16 -05:00
|
|
|
GITHUB_REGEX = /(github:|:github\s*=>)\s*['"](?<name>[^'"]+)['"]/.freeze
|
|
|
|
GIT_REGEX = /(git:|:git\s*=>)\s*['"](?<name>#{URL_REGEX})['"]/.freeze
|
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def link_dependencies
|
2017-05-16 16:25:57 -04:00
|
|
|
link_urls
|
|
|
|
link_packages
|
|
|
|
end
|
2016-07-10 17:13:06 -04:00
|
|
|
|
2017-05-16 16:25:57 -04:00
|
|
|
def link_urls
|
2016-07-10 17:13:06 -04:00
|
|
|
# Link `github: "user/repo"` to https://github.com/user/repo
|
2019-02-15 14:11:16 -05:00
|
|
|
link_regex(GITHUB_REGEX, &method(:github_url))
|
2016-07-10 17:13:06 -04:00
|
|
|
|
2017-05-12 11:29:25 -04:00
|
|
|
# Link `git: "https://gitlab.example.com/user/repo"` to https://gitlab.example.com/user/repo
|
2019-02-15 14:11:16 -05:00
|
|
|
link_regex(GIT_REGEX, &:itself)
|
2017-05-12 11:29:25 -04:00
|
|
|
|
2016-07-10 17:13:06 -04:00
|
|
|
# Link `source "https://rubygems.org"` to https://rubygems.org
|
2017-05-16 16:25:57 -04:00
|
|
|
link_method_call('source', URL_REGEX, &:itself)
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
|
|
|
|
2017-05-16 16:25:57 -04:00
|
|
|
def link_packages
|
2019-02-15 14:11:16 -05:00
|
|
|
packages = parse_packages
|
|
|
|
|
|
|
|
return if packages.blank?
|
|
|
|
|
|
|
|
packages.each do |package|
|
|
|
|
link_method_call('gem', package.name) do
|
|
|
|
external_url(package.name, package.external_ref)
|
|
|
|
end
|
2017-05-16 16:25:57 -04:00
|
|
|
end
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
2019-02-15 14:11:16 -05:00
|
|
|
|
|
|
|
def package_url(name)
|
|
|
|
"https://rubygems.org/gems/#{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_packages
|
|
|
|
parser = Gitlab::DependencyLinker::Parser::Gemfile.new(plain_text)
|
|
|
|
parser.parse(keyword: self.class.package_keyword)
|
|
|
|
end
|
2016-07-10 17:13:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|