Commit fbec6dbd authored by Douwe Maan's avatar Douwe Maan

Autolink package names in composer.json

parent 83747783
...@@ -4,6 +4,7 @@ module Gitlab ...@@ -4,6 +4,7 @@ module Gitlab
GemfileLinker, GemfileLinker,
GemspecLinker, GemspecLinker,
PackageJsonLinker, PackageJsonLinker,
ComposerJsonLinker,
].freeze ].freeze
def self.linker(blob_name) def self.linker(blob_name)
......
module Gitlab
module DependencyLinker
class ComposerJsonLinker < PackageJsonLinker
self.file_type = :composer_json
private
def link_packages
link_packages_at_key("require", &method(:package_url))
link_packages_at_key("require-dev", &method(:package_url))
end
def package_url(name)
"https://packagist.org/packages/#{name}" if name =~ %r{\A#{REPO_REGEX}\z}
end
end
end
end
require 'rails_helper'
describe Gitlab::DependencyLinker::ComposerJsonLinker, lib: true do
describe '.support?' do
it 'supports composer.json' do
expect(described_class.support?('composer.json')).to be_truthy
end
it 'does not support other files' do
expect(described_class.support?('composer.json.example')).to be_falsey
end
end
describe '#link' do
let(:file_name) { "composer.json" }
let(:file_content) do
<<-CONTENT.strip_heredoc
{
"name": "laravel/laravel",
"homepage": "https://laravel.com/",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"repositories": [
{
"type": "git",
"url": "https://github.com/laravel/laravel.git"
}
],
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~4.0",
"symfony/css-selector": "2.8.*|3.0.*",
"symfony/dom-crawler": "2.8.*|3.0.*"
}
}
CONTENT
end
subject { Gitlab::Highlight.highlight(file_name, file_content) }
def link(name, url)
%{<a href="#{url}" rel="nofollow noreferrer noopener" target="_blank">#{name}</a>}
end
it 'links the module name' do
expect(subject).to include(link('laravel/laravel', 'https://packagist.org/packages/laravel/laravel'))
end
it 'links the homepage' do
expect(subject).to include(link('https://laravel.com/', 'https://laravel.com/'))
end
it 'links the repository URL' do
expect(subject).to include(link('https://github.com/laravel/laravel.git', 'https://github.com/laravel/laravel.git'))
end
it 'links the license' do
expect(subject).to include(link('MIT', 'http://choosealicense.com/licenses/mit/'))
end
it 'links dependencies' do
expect(subject).to include(link('laravel/framework', 'https://packagist.org/packages/laravel/framework'))
expect(subject).to include(link('fzaninotto/faker', 'https://packagist.org/packages/fzaninotto/faker'))
expect(subject).to include(link('mockery/mockery', 'https://packagist.org/packages/mockery/mockery'))
expect(subject).to include(link('phpunit/phpunit', 'https://packagist.org/packages/phpunit/phpunit'))
expect(subject).to include(link('symfony/css-selector', 'https://packagist.org/packages/symfony/css-selector'))
expect(subject).to include(link('symfony/dom-crawler', 'https://packagist.org/packages/symfony/dom-crawler'))
end
it 'does not link core dependencies' do
expect(subject).not_to include(link('php', 'https://packagist.org/packages/php'))
end
end
end
...@@ -25,5 +25,13 @@ describe Gitlab::DependencyLinker, lib: true do ...@@ -25,5 +25,13 @@ describe Gitlab::DependencyLinker, lib: true do
described_class.link(blob_name, nil, nil) described_class.link(blob_name, nil, nil)
end end
it 'links using ComposerJsonLinker' do
blob_name = 'composer.json'
expect(described_class::ComposerJsonLinker).to receive(:link)
described_class.link(blob_name, nil, nil)
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment