Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
83747783
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
83747783
编写于
5月 16, 2017
作者:
D
Douwe Maan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Autolink package names in package.json
上级
02ad8c0c
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
205 addition
and
0 deletion
+205
-0
lib/gitlab/dependency_linker.rb
lib/gitlab/dependency_linker.rb
+1
-0
lib/gitlab/dependency_linker/base_linker.rb
lib/gitlab/dependency_linker/base_linker.rb
+4
-0
lib/gitlab/dependency_linker/json_linker.rb
lib/gitlab/dependency_linker/json_linker.rb
+63
-0
lib/gitlab/dependency_linker/package_json_linker.rb
lib/gitlab/dependency_linker/package_json_linker.rb
+44
-0
spec/lib/gitlab/dependency_linker/package_json_linker_spec.rb
.../lib/gitlab/dependency_linker/package_json_linker_spec.rb
+85
-0
spec/lib/gitlab/dependency_linker_spec.rb
spec/lib/gitlab/dependency_linker_spec.rb
+8
-0
未找到文件。
lib/gitlab/dependency_linker.rb
浏览文件 @
83747783
...
...
@@ -3,6 +3,7 @@ module Gitlab
LINKERS
=
[
GemfileLinker
,
GemspecLinker
,
PackageJsonLinker
,
].
freeze
def
self
.
linker
(
blob_name
)
...
...
lib/gitlab/dependency_linker/base_linker.rb
浏览文件 @
83747783
...
...
@@ -2,6 +2,7 @@ module Gitlab
module
DependencyLinker
class
BaseLinker
URL_REGEX
=
%r{https?://[^'"]+}
.
freeze
REPO_REGEX
=
%r{[^/'"]+/[^/'"]+}
.
freeze
class_attribute
:file_type
...
...
@@ -36,6 +37,9 @@ module Gitlab
Licensee
::
License
.
find
(
name
)
&
.
url
end
def
github_url
(
name
)
"https://github.com/
#{
name
}
"
end
def
link_tag
(
name
,
url
)
%{<a href="#{ERB::Util.html_escape_once(url)}" rel="nofollow noreferrer noopener" target="_blank">#{ERB::Util.html_escape_once(name)}</a>}
...
...
lib/gitlab/dependency_linker/json_linker.rb
0 → 100644
浏览文件 @
83747783
module
Gitlab
module
DependencyLinker
class
JsonLinker
<
BaseLinker
def
link
return
highlighted_text
unless
json
super
end
private
# Links package names in a JSON key or values.
#
# Example:
# link_json('name')
# # Will link `package` in `"name": "package"`
#
# link_json('name', 'specific_package')
# # Will link `specific_package` in `"name": "specific_package"`
#
# link_json('name', /[^\/]+\/[^\/]+/)
# # Will link `user/repo` in `"name": "user/repo"`, but not `"name": "package"`
#
# link_json('specific_package', '1.0.1', link: :key)
# # Will link `specific_package` in `"specific_package": "1.0.1"`
def
link_json
(
key
,
value
=
nil
,
link: :value
,
&
url_proc
)
key
=
case
key
when
Array
Regexp
.
union
(
key
.
map
{
|
name
|
Regexp
.
escape
(
name
)
})
when
String
Regexp
.
escape
(
key
)
when
nil
'[^"]+'
else
key
end
value
=
case
value
when
String
Regexp
.
escape
(
value
)
when
nil
'[^"]+'
else
value
end
if
link
==
:value
value
=
"(?<name>
#{
value
}
)"
else
key
=
"(?<name>
#{
key
}
)"
end
link_regex
(
/"
#{
key
}
":\s*"
#{
value
}
"/
,
&
url_proc
)
end
def
json
@json
||=
JSON
.
parse
(
plain_text
)
rescue
nil
end
end
end
end
lib/gitlab/dependency_linker/package_json_linker.rb
0 → 100644
浏览文件 @
83747783
module
Gitlab
module
DependencyLinker
class
PackageJsonLinker
<
JsonLinker
self
.
file_type
=
:package_json
private
def
link_dependencies
link_json
(
'name'
,
json
[
"name"
],
&
method
(
:package_url
))
link_json
(
'license'
,
&
method
(
:license_url
))
link_json
(
%w[homepage url]
,
URL_REGEX
,
&
:itself
)
link_packages
end
def
link_packages
link_packages_at_key
(
"dependencies"
,
&
method
(
:package_url
))
link_packages_at_key
(
"devDependencies"
,
&
method
(
:package_url
))
end
def
link_packages_at_key
(
key
,
&
url_proc
)
dependencies
=
json
[
key
]
return
unless
dependencies
dependencies
.
each
do
|
name
,
version
|
link_json
(
name
,
version
,
link: :key
,
&
url_proc
)
link_json
(
name
)
do
|
value
|
case
value
when
/\A
#{
URL_REGEX
}
\z/
value
when
/\A
#{
REPO_REGEX
}
\z/
github_url
(
value
)
end
end
end
end
def
package_url
(
name
)
"https://npmjs.com/package/
#{
name
}
"
end
end
end
end
spec/lib/gitlab/dependency_linker/package_json_linker_spec.rb
0 → 100644
浏览文件 @
83747783
require
'rails_helper'
describe
Gitlab
::
DependencyLinker
::
PackageJsonLinker
,
lib:
true
do
describe
'.support?'
do
it
'supports package.json'
do
expect
(
described_class
.
support?
(
'package.json'
)).
to
be_truthy
end
it
'does not support other files'
do
expect
(
described_class
.
support?
(
'package.json.example'
)).
to
be_falsey
end
end
describe
'#link'
do
let
(
:file_name
)
{
"package.json"
}
let
(
:file_content
)
do
<<-
CONTENT
.
strip_heredoc
{
"name": "module-name",
"version": "10.3.1",
"repository": {
"type": "git",
"url": "https://github.com/vuejs/vue.git"
},
"homepage": "https://github.com/vuejs/vue#readme",
"dependencies": {
"primus": "*",
"async": "~0.8.0",
"express": "4.2.x",
"bigpipe": "bigpipe/pagelet",
"plates": "https://github.com/flatiron/plates/tarball/master"
},
"devDependencies": {
"vows": "^0.7.0",
"assume": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0",
"pre-commit": "*"
},
"license": "MIT"
}
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
(
'module-name'
,
'https://npmjs.com/package/module-name'
))
end
it
'links the homepage'
do
expect
(
subject
).
to
include
(
link
(
'https://github.com/vuejs/vue#readme'
,
'https://github.com/vuejs/vue#readme'
))
end
it
'links the repository URL'
do
expect
(
subject
).
to
include
(
link
(
'https://github.com/vuejs/vue.git'
,
'https://github.com/vuejs/vue.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
(
'primus'
,
'https://npmjs.com/package/primus'
))
expect
(
subject
).
to
include
(
link
(
'async'
,
'https://npmjs.com/package/async'
))
expect
(
subject
).
to
include
(
link
(
'express'
,
'https://npmjs.com/package/express'
))
expect
(
subject
).
to
include
(
link
(
'bigpipe'
,
'https://npmjs.com/package/bigpipe'
))
expect
(
subject
).
to
include
(
link
(
'plates'
,
'https://npmjs.com/package/plates'
))
expect
(
subject
).
to
include
(
link
(
'vows'
,
'https://npmjs.com/package/vows'
))
expect
(
subject
).
to
include
(
link
(
'assume'
,
'https://npmjs.com/package/assume'
))
expect
(
subject
).
to
include
(
link
(
'pre-commit'
,
'https://npmjs.com/package/pre-commit'
))
end
it
'links GitHub repos'
do
expect
(
subject
).
to
include
(
link
(
'bigpipe/pagelet'
,
'https://github.com/bigpipe/pagelet'
))
end
it
'links Git repos'
do
expect
(
subject
).
to
include
(
link
(
'https://github.com/flatiron/plates/tarball/master'
,
'https://github.com/flatiron/plates/tarball/master'
))
end
end
end
spec/lib/gitlab/dependency_linker_spec.rb
浏览文件 @
83747783
...
...
@@ -17,5 +17,13 @@ describe Gitlab::DependencyLinker, lib: true do
described_class
.
link
(
blob_name
,
nil
,
nil
)
end
it
'links using PackageJsonLinker'
do
blob_name
=
'package.json'
expect
(
described_class
::
PackageJsonLinker
).
to
receive
(
:link
)
described_class
.
link
(
blob_name
,
nil
,
nil
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录