Skip to content

Commit 7e87e09

Browse files
committed
Add purl2url support to get a vcs_url
Add more tests Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent db9d6bc commit 7e87e09

4 files changed

Lines changed: 56 additions & 14 deletions

File tree

src/packageurl/contrib/purl2url.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,49 @@ def build_gitlab_repo_url(purl):
179179
(
180180
r"git\.kernel\.org",
181181
r"gitweb\.gentoo\.org",
182-
): "https://{namespace}/{name}.git/commit/?id={version}",
182+
): {
183+
"commit_url": "https://{namespace}/{name}.git/commit/?id={version}",
184+
"repo_url": "https://{namespace}/{name}.git",
185+
},
183186
# gitiles
184187
(
185188
r"android\.googlesource\.com",
186189
r"aomedia\.googlesource\.com",
187190
r"chromium\.googlesource\.com",
188-
): "https://{namespace}/{name}/+/{version}",
191+
): {
192+
"commit_url": "https://{namespace}/{name}/+/{version}",
193+
"repo_url": "https://{namespace}/{name}",
194+
},
189195
# allura
190-
(r"sourceforge\.net", r"forge-allura\.apache\.org"): "https://{namespace}/{name}/ci/{version}",
196+
(r"sourceforge\.net", r"forge-allura\.apache\.org"): {
197+
"commit_url": "https://{namespace}/{name}/ci/{version}",
198+
"repo_url": "https://{namespace}/{name}",
199+
},
191200
# gitweb
192201
(
193202
r"gcc\.gnu\.org",
194203
r"git\.postgresql\.org",
195-
): "https://{namespace}/?p={name}.git;a=commit;h={version}",
204+
r"sourceware\.org/git",
205+
): {
206+
"commit_url": "https://{namespace}/?p={name}.git;a=commit;h={version}",
207+
"repo_url": "https://{namespace}/?p={name}.git",
208+
},
196209
# gitea / forgejo
197210
(
198211
r"codeberg\.org",
199212
r"gitea\.com",
200-
): "https://{namespace}/{name}/commit/{version}",
213+
): {
214+
"commit_url": "https://{namespace}/{name}/commit/{version}",
215+
"repo_url": "https://{namespace}/{name}",
216+
},
201217
# sub gitlab ( excludes gitlab.com )
202218
(
203-
r"git\.codelinaro\.org",
219+
r"git\.codelinaro\.org.*",
204220
r"gitlab\.(?!com\b)[^/]+",
205-
): "https://{namespace}/{name}/-/commit/{version}",
221+
): {
222+
"commit_url": "https://{namespace}/{name}/-/commit/{version}",
223+
"repo_url": "https://{namespace}/{name}",
224+
},
206225
}
207226

208227

@@ -216,17 +235,20 @@ def build_generic_repo_url(purl):
216235
namespace = purl_data.namespace
217236
version = purl_data.version
218237

219-
if not (namespace and name and version):
238+
if not (namespace and name):
220239
return
221240

222241
for patterns, template_url in GIT_REPO_GENERIC.items():
223242
for pattern in patterns:
224243
if not re.match(pattern, namespace):
225244
continue
226245

227-
return template_url.format(namespace=namespace, name=name, version=version)
228-
229-
return None
246+
if version:
247+
return template_url["commit_url"].format(
248+
namespace=namespace, name=name, version=version
249+
)
250+
return template_url["repo_url"].format(namespace=namespace, name=name)
251+
return
230252

231253

232254
@repo_router.route("pkg:(gem|rubygems)/.*")

src/packageurl/contrib/url2purl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def build_allura_purl(url):
859859
)
860860

861861

862-
GITWEB_DOMAINS = [r"gcc\.gnu\.org/git", r"git\.postgresql\.org/gitweb"]
862+
GITWEB_DOMAINS = [r"gcc\.gnu\.org/git", r"git\.postgresql\.org/gitweb", "sourceware\.org/git"]
863863
GITWEB_ROUTE_REGEX = build_route_regex(GITWEB_DOMAINS)
864864

865865

@@ -870,6 +870,7 @@ def build_gitweb_purl(url):
870870
For example:
871871
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=82cc94e5fb69d1c45a386f83798251de5bff9339
872872
https://git.postgresql.org/gitweb/?p=hamn.git;a=commit;h=a796b71a5b3fe7f751f1086a08cb114b9877dea2
873+
https://sourceware.org/git/?p=glibc.git;a=commit;h=dedebed24f77762eea7d3c5ed2739a90a4d60461
873874
"""
874875

875876
gitweb_pattern = (

tests/contrib/data/url2purl.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,6 @@
299299
"https://sourceforge.net/p/expat/code_git/ci/f0bec73b018caa07d3e75ec8dd967f3785d71bde": "pkg:generic/sourceforge.net/p/expat/code_git@f0bec73b018caa07d3e75ec8dd967f3785d71bde",
300300
"https://forge-allura.apache.org/p/allura/git/ci/674e070e5ca7db7c75cf61d8efd2a3e3e49bd946": "pkg:generic/forge-allura.apache.org/p/allura/git@674e070e5ca7db7c75cf61d8efd2a3e3e49bd946",
301301
"https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=82cc94e5fb69d1c45a386f83798251de5bff9339": "pkg:generic/gcc.gnu.org/git/gcc@82cc94e5fb69d1c45a386f83798251de5bff9339",
302-
"https://git.postgresql.org/gitweb/?p=hamn.git;a=commit;h=a796b71a5b3fe7f751f1086a08cb114b9877dea2": "pkg:generic/git.postgresql.org/gitweb/hamn@a796b71a5b3fe7f751f1086a08cb114b9877dea2"
302+
"https://git.postgresql.org/gitweb/?p=hamn.git;a=commit;h=a796b71a5b3fe7f751f1086a08cb114b9877dea2": "pkg:generic/git.postgresql.org/gitweb/hamn@a796b71a5b3fe7f751f1086a08cb114b9877dea2",
303+
"https://sourceware.org/git/?p=bunsen.git;a=commit;h=6c55933f37099517e050c923527b0b2267e1deed": "pkg:generic/sourceware.org/git/bunsen@6c55933f37099517e050c923527b0b2267e1deed"
303304
}

tests/contrib/test_purl2url.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,25 @@ def test_purl2url_get_repo_url():
8585
"pkg:generic/sourceforge.net/p/expat/code_git@f0bec73b018caa07d3e75ec8dd967f3785d71bde": "https://sourceforge.net/p/expat/code_git/ci/f0bec73b018caa07d3e75ec8dd967f3785d71bde",
8686
"pkg:generic/forge-allura.apache.org/p/allura/git@674e070e5ca7db7c75cf61d8efd2a3e3e49bd946": "https://forge-allura.apache.org/p/allura/git/ci/674e070e5ca7db7c75cf61d8efd2a3e3e49bd946",
8787
"pkg:generic/gcc.gnu.org/git/gcc@82cc94e5fb69d1c45a386f83798251de5bff9339": "https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=82cc94e5fb69d1c45a386f83798251de5bff9339",
88-
"pkg:generic/git.postgresql.org/gitweb/p/infrarecorder/hamn@4f4fed18770ff15da3c7ab1e81854b75181ab5d0": "https://git.postgresql.org/gitweb/p/infrarecorder/?p=hamn.git;a=commit;h=4f4fed18770ff15da3c7ab1e81854b75181ab5d0"
88+
"pkg:generic/git.postgresql.org/gitweb/p/infrarecorder/hamn@4f4fed18770ff15da3c7ab1e81854b75181ab5d0": "https://git.postgresql.org/gitweb/p/infrarecorder/?p=hamn.git;a=commit;h=4f4fed18770ff15da3c7ab1e81854b75181ab5d0",
89+
"pkg:generic/git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm": "https://git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm",
90+
"pkg:generic/gitlab.gnome.org/GNOME/gimp": "https://gitlab.gnome.org/GNOME/gimp",
91+
"pkg:generic/gitlab.freedesktop.org/poppler/poppler": "https://gitlab.freedesktop.org/poppler/poppler",
92+
"pkg:generic/gitea.com/htc47/entur": "https://gitea.com/htc47/entur",
93+
"pkg:generic/codeberg.org/alpinelinux/aports": "https://codeberg.org/alpinelinux/aports",
94+
"pkg:generic/git.kernel.org/pub/scm/utils/b4/b4": "https://git.kernel.org/pub/scm/utils/b4/b4.git",
95+
"pkg:generic/git.kernel.org/pub/scm/virt/kvm/mst/qemu": "https://git.kernel.org/pub/scm/virt/kvm/mst/qemu.git",
96+
"pkg:generic/gitweb.gentoo.org/dev/darkside": "https://gitweb.gentoo.org/dev/darkside.git",
97+
"pkg:generic/gitweb.gentoo.org/repo/gentoo": "https://gitweb.gentoo.org/repo/gentoo.git",
98+
"pkg:generic/android.googlesource.com/platform/frameworks/base": "https://android.googlesource.com/platform/frameworks/base",
99+
"pkg:generic/android.googlesource.com/device/generic/vulkan-cereal": "https://android.googlesource.com/device/generic/vulkan-cereal",
100+
"pkg:generic/chromium.googlesource.com/aosp/platform/external/dbus-binding-generator": "https://chromium.googlesource.com/aosp/platform/external/dbus-binding-generator",
101+
"pkg:generic/aomedia.googlesource.com/libavifinfo": "https://aomedia.googlesource.com/libavifinfo",
102+
"pkg:generic/sourceforge.net/p/djvu/djvulibre-git": "https://sourceforge.net/p/djvu/djvulibre-git",
103+
"pkg:generic/sourceforge.net/p/expat/code_git": "https://sourceforge.net/p/expat/code_git",
104+
"pkg:generic/forge-allura.apache.org/p/allura/git": "https://forge-allura.apache.org/p/allura/git",
105+
"pkg:generic/gcc.gnu.org/git/gcc": "https://gcc.gnu.org/git/?p=gcc.git",
106+
"pkg:generic/git.postgresql.org/gitweb/hamn": "https://git.postgresql.org/gitweb/?p=hamn.git"
89107
}
90108

91109
for purl, url in purls_url.items():

0 commit comments

Comments
 (0)