Commit 67d0d86f authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'feat/rust-cargo-toml-blob-view' into 'master'

Add support for Rust Cargo.toml dependency vizualisation to file blob view

See merge request gitlab-org/gitlab!21374
parents f1881951 4426572f
...@@ -458,7 +458,7 @@ gem 'grpc', '~> 1.24.0' ...@@ -458,7 +458,7 @@ gem 'grpc', '~> 1.24.0'
gem 'google-protobuf', '~> 3.8.0' gem 'google-protobuf', '~> 3.8.0'
gem 'toml-rb', '~> 1.0.0', require: false gem 'toml-rb', '~> 1.0.0'
# Feature toggles # Feature toggles
gem 'flipper', '~> 0.17.1' gem 'flipper', '~> 0.17.1'
......
...@@ -51,6 +51,7 @@ class Blob < SimpleDelegator ...@@ -51,6 +51,7 @@ class Blob < SimpleDelegator
BlobViewer::Contributing, BlobViewer::Contributing,
BlobViewer::Changelog, BlobViewer::Changelog,
BlobViewer::CargoToml,
BlobViewer::Cartfile, BlobViewer::Cartfile,
BlobViewer::ComposerJson, BlobViewer::ComposerJson,
BlobViewer::Gemfile, BlobViewer::Gemfile,
......
# frozen_string_literal: true
module BlobViewer
class CargoToml < DependencyManager
include Static
self.file_types = %i(cargo_toml)
def manager_name
'Cargo'
end
def manager_url
'https://crates.io/'
end
end
end
---
title: Add support for Rust Cargo.toml dependency vizualisation and linking
merge_request: 21374
author: Fabio Huser
type: added
...@@ -12,7 +12,8 @@ module Gitlab ...@@ -12,7 +12,8 @@ module Gitlab
PodspecJsonLinker, PodspecJsonLinker,
CartfileLinker, CartfileLinker,
GodepsJsonLinker, GodepsJsonLinker,
RequirementsTxtLinker RequirementsTxtLinker,
CargoTomlLinker
].freeze ].freeze
def self.linker(blob_name) def self.linker(blob_name)
......
# frozen_string_literal: true
module Gitlab
module DependencyLinker
class CargoTomlLinker < BaseLinker
self.file_type = :cargo_toml
def link
return highlighted_text unless toml
super
end
private
def link_dependencies
link_dependencies_at("dependencies")
link_dependencies_at("dev-dependencies")
link_dependencies_at("build-dependencies")
end
def link_dependencies_at(type)
dependencies = toml[type]
return unless dependencies
dependencies.each do |name, value|
link_toml(name, value, type) do |name|
"https://crates.io/crates/#{name}"
end
end
end
def link_toml(key, value, type, &url_proc)
if value.is_a? String
link_regex(/^(?<name>#{key})\s*=\s*"#{value}"/, &url_proc)
else
link_regex(/^\[#{type}\.(?<name>#{key})]/, &url_proc)
end
end
def toml
@toml ||= TomlRB.parse(plain_text) rescue nil
end
end
end
end
...@@ -25,6 +25,7 @@ module Gitlab ...@@ -25,6 +25,7 @@ module Gitlab
route_map: '.gitlab/route-map.yml', route_map: '.gitlab/route-map.yml',
# Dependency files # Dependency files
cargo_toml: 'Cargo.toml',
cartfile: %r{\ACartfile[^/]*\z}, cartfile: %r{\ACartfile[^/]*\z},
composer_json: 'composer.json', composer_json: 'composer.json',
gemfile: /\A(Gemfile|gems\.rb)\z/, gemfile: /\A(Gemfile|gems\.rb)\z/,
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::DependencyLinker::CargoTomlLinker do
describe '.support?' do
it 'supports Cargo.toml' do
expect(described_class.support?('Cargo.toml')).to be_truthy
end
it 'does not support other files' do
expect(described_class.support?('cargo.yaml')).to be_falsey
end
end
describe '#link' do
let(:file_name) { "Cargo.toml" }
let(:file_content) do
<<-CONTENT.strip_heredoc
# See https://doc.rust-lang.org/cargo/reference/manifest.html
[package]
# Package shouldn't be matched
name = "gitlab-test"
version = "0.0.1"
authors = ["Some User <some.user@example.org>"]
description = "A GitLab test Cargo.toml."
keywords = ["gitlab", "test", "rust", "crago"]
readme = "README.md"
[dependencies]
# Default dependencies format with fixed version and version range
chrono = "0.4.7"
xml-rs = ">=0.8.0"
[dependencies.memchr]
# Specific dependency with optional info
version = "2.2.1"
optional = true
[dev-dependencies]
# Dev dependency with version modifier
commandspec = "~0.12.2"
[build-dependencies]
# Build dependency with version wildcard
thread_local = "0.3.*"
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 dependencies' do
expect(subject).to include(link('chrono', 'https://crates.io/crates/chrono'))
expect(subject).to include(link('xml-rs', 'https://crates.io/crates/xml-rs'))
expect(subject).to include(link('memchr', 'https://crates.io/crates/memchr'))
expect(subject).to include(link('commandspec', 'https://crates.io/crates/commandspec'))
expect(subject).to include(link('thread_local', 'https://crates.io/crates/thread_local'))
end
it 'does not contain metadata identified as package' do
expect(subject).not_to include(link('version', 'https://crates.io/crates/version'))
end
end
end
...@@ -83,5 +83,13 @@ describe Gitlab::DependencyLinker do ...@@ -83,5 +83,13 @@ describe Gitlab::DependencyLinker do
described_class.link(blob_name, nil, nil) described_class.link(blob_name, nil, nil)
end end
it 'links using CargoTomlLinker' do
blob_name = 'Cargo.toml'
expect(described_class::CargoTomlLinker).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