2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
module Gitlab
|
|
|
|
module DependencyLinker
|
|
|
|
class GemfileLinker < MethodLinker
|
2019-03-13 22:55:13 +05:30
|
|
|
class_attribute :package_keyword
|
|
|
|
|
|
|
|
self.package_keyword = :gem
|
2017-09-10 17:25:29 +05:30
|
|
|
self.file_type = :gemfile
|
|
|
|
|
2019-03-13 22:55:13 +05:30
|
|
|
GITHUB_REGEX = /(github:|:github\s*=>)\s*['"](?<name>[^'"]+)['"]/.freeze
|
|
|
|
GIT_REGEX = /(git:|:git\s*=>)\s*['"](?<name>#{URL_REGEX})['"]/.freeze
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def link_dependencies
|
|
|
|
link_urls
|
|
|
|
link_packages
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_urls
|
|
|
|
# Link `github: "user/repo"` to https://github.com/user/repo
|
2019-03-13 22:55:13 +05:30
|
|
|
link_regex(GITHUB_REGEX, &method(:github_url))
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
# Link `git: "https://gitlab.example.com/user/repo"` to https://gitlab.example.com/user/repo
|
2019-03-13 22:55:13 +05:30
|
|
|
link_regex(GIT_REGEX, &:itself)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
# Link `source "https://rubygems.org"` to https://rubygems.org
|
|
|
|
link_method_call('source', URL_REGEX, &:itself)
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_packages
|
2019-03-13 22:55:13 +05:30
|
|
|
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-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
2019-03-13 22:55:13 +05:30
|
|
|
|
|
|
|
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
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|