gitlab-org--gitlab-foss/lib/gitlab/dependency_linker/gemfile_linker.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.4 KiB
Ruby
Raw Normal View History

# 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
class_attribute :package_keyword
self.package_keyword = :gem
self.file_type = :gemfile
2016-07-10 17:13:06 -04: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
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
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
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
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