Skip to content

Commit 4929fc0

Browse files
committed
Fix invalid sub-gitlab domain
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent fbff2d3 commit 4929fc0

4 files changed

Lines changed: 47 additions & 33 deletions

File tree

src/packageurl/contrib/purl2url.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def build_gitlab_repo_url(purl):
206206
clean_url = re.sub(r"^https?://", "", repository_url)
207207
for pattern in SUB_GITLAB_DOMAINS:
208208
if re.match(pattern, clean_url):
209-
return f"https://{namespace}/{name}"
209+
return f"{repository_url}/{namespace}/{name}"
210210

211211
return f"https://gitlab.com/{namespace}/{name}"
212212

@@ -342,17 +342,20 @@ def build_main_commit_url(purl):
342342
name = purl_data.name
343343
namespace = purl_data.namespace
344344
version = purl_data.version
345+
qualifiers = purl_data.qualifiers
346+
repository_url = qualifiers.get("repository_url")
347+
345348
if not (namespace and name and version):
346349
return
347350

348351
commit_url_template = {
349352
"github": f"https://github.com/{namespace}/{name}/commit/{version}",
350353
"gitlab": f"https://gitlab.com/{namespace}/{name}/-/commit/{version}",
351-
"sub-gitlab": f"https://{namespace}/{name}/-/commit/{version}",
354+
"sub-gitlab": f"{repository_url}/{namespace}/{name}/-/commit/{version}",
352355
"bitbucket": f"https://bitbucket.org/{namespace}/{name}/commits/{version}",
353356
}
354357

355-
if purl_type == "gitlab" and purl_data.qualifiers.get("repository_url"):
358+
if purl_type == "gitlab" and repository_url:
356359
purl_type = "sub-gitlab"
357360

358361
return commit_url_template[purl_type].format(namespace=namespace, name=name, version=version)
@@ -368,18 +371,19 @@ def build_main_patch_url(purl):
368371
name = purl_data.name
369372
namespace = purl_data.namespace
370373
version = purl_data.version
371-
374+
qualifiers = purl_data.qualifiers
375+
repository_url = qualifiers.get("repository_url")
372376
if not (namespace and name and version):
373377
return
374378

375379
patch_url_templates = {
376380
"github": f"https://github.com/{namespace}/{name}/commit/{version}.patch",
377381
"gitlab": f"https://gitlab.com/{namespace}/{name}/-/commit/{version}.patch",
378-
"sub-gitlab": f"https://{namespace}/{name}/-/commit/{version}.patch",
382+
"sub-gitlab": f"{repository_url}/{namespace}/{name}/-/commit/{version}.patch",
379383
"bitbucket": f"https://bitbucket.org/{namespace}/{name}/commits/{version}/raw",
380384
}
381385

382-
if purl_type == "gitlab" and purl_data.qualifiers.get("repository_url"):
386+
if purl_type == "gitlab" and repository_url:
383387
purl_type = "sub-gitlab"
384388

385389
return patch_url_templates[purl_type].format(namespace=namespace, name=name, version=version)

src/packageurl/contrib/url2purl.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,16 @@ def build_gitlab_sub_purl(url):
691691
https://git.codelinaro.org/linaro/qcom/project/-/commit/a40a9732c840e5a324fba78b0ff7980b497c3831
692692
"""
693693
archive_pattern = (
694-
r"^https?://" r"(?P<namespace>[^/]+)/" r".+?/-/archive/[^/]+/" r"(?P<name>[^/]+)$"
694+
r"^https?://"
695+
r"(?P<domain>[^/]+)/"
696+
r"(?P<namespace>[^/]+)/"
697+
r".+?/-/archive/[^/]+/"
698+
r"(?P<name>[^/]+)$"
695699
)
696700

697701
commit_pattern = (
698702
r"^https?://"
703+
r"(?P<domain>[^/]+)/"
699704
r"(?P<namespace>.+?)/"
700705
r"(?P<name>[^/]+)"
701706
r"(?:/-/commit/(?P<version>[0-9a-fA-F]{7,64}))?"
@@ -704,14 +709,19 @@ def build_gitlab_sub_purl(url):
704709

705710
archive_match = re.search(archive_pattern, url)
706711
if archive_match:
712+
domain = archive_match.group("domain")
707713
namespace = archive_match.group("namespace")
708714
name = archive_match.group("name")
709715
return PackageURL(
710-
type="gitlab", namespace=namespace, name=name, qualifiers={"download_url": url}
716+
type="gitlab",
717+
namespace=namespace,
718+
name=name,
719+
qualifiers={"download_url": url, "repository_url": f"https://{domain}"},
711720
)
712721

713722
commit_match = re.search(commit_pattern, url)
714723
if commit_match:
724+
domain = commit_match.group("domain")
715725
namespace = commit_match.group("namespace")
716726
name = commit_match.group("name")
717727
version = commit_match.group("version")
@@ -721,7 +731,7 @@ def build_gitlab_sub_purl(url):
721731
namespace=namespace,
722732
name=name,
723733
version=version,
724-
qualifiers={"repository_url": f"https://{namespace}/{name}"},
734+
qualifiers={"repository_url": f"https://{domain}"},
725735
)
726736

727737

tests/contrib/data/url2purl.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,18 @@
269269
"https://hackage.haskell.org/package/3d-graphics-examples-0.0.0.2/3d-graphics-examples-0.0.0.2.tar.gz": "pkg:hackage/3d-graphics-examples@0.0.0.2",
270270
"https://hackage.haskell.org/package/cli-extras-0.2.0.0": "pkg:hackage/cli-extras@0.2.0.0",
271271
"https://hackage.haskell.org/package/cli-extras-0.2.0.0/": "pkg:hackage/cli-extras@0.2.0.0",
272-
"https://salsa.debian.org/lxc-team/lxc/-/archive/master/lxc-master.tar.gz": "pkg:gitlab/salsa.debian.org/lxc-master.tar.gz?download_url=https://salsa.debian.org/lxc-team/lxc/-/archive/master/lxc-master.tar.gz",
272+
"https://salsa.debian.org/rust-team/debcargo-conf/-/archive/master/debcargo-conf.tar.gz": "pkg:gitlab/rust-team/debcargo-conf.tar.gz?download_url=https://salsa.debian.org/rust-team/debcargo-conf/-/archive/master/debcargo-conf.tar.gz&repository_url=https://salsa.debian.org",
273273
"http://apt-rpm.org/": null,
274274
"": null,
275275
"https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-notifier/android-notifier-desktop-0.5.1-1.i386.rpm": "pkg:generic/code.google.com/android-notifier?download_url=https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/android-notifier/android-notifier-desktop-0.5.1-1.i386.rpm",
276276
"https://cran.r-project.org/src/contrib/jsonlite_1.8.8.tar.gz": "pkg:cran/jsonlite@1.8.8",
277277
"https://packagemanager.rstudio.com/cran/2022-06-23/src/contrib/curl_4.3.2.tar.gz": "pkg:cran/curl@4.3.2?download_url=https://packagemanager.rstudio.com/cran/2022-06-23/src/contrib/curl_4.3.2.tar.gz",
278278
"https://github.com/TG1999/first_repo/commit/98e516011d6e096e25247b82fc5f196bbeecff10": "pkg:github/tg1999/first_repo@98e516011d6e096e25247b82fc5f196bbeecff10",
279279
"https://gitlab.com/TG1999/first_repo/-/commit/bf04e5f289885cf2f20a92b387bcc6df33e30809": "pkg:gitlab/tg1999/first_repo@bf04e5f289885cf2f20a92b387bcc6df33e30809",
280-
"https://git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm/-/commit/a5f07894058c4198f61e533d727b343c5be879b0": "pkg:gitlab/git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm@a5f07894058c4198f61e533d727b343c5be879b0?repository_url=https://git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm",
281-
"https://gitlab.gnome.org/GNOME/gimp/-/commit/112a5e038f0646eae5ae314988ec074433d2b365": "pkg:gitlab/gitlab.gnome.org/gnome/gimp@112a5e038f0646eae5ae314988ec074433d2b365?repository_url=https://gitlab.gnome.org/GNOME/gimp",
282-
"https://gitlab.freedesktop.org/poppler/poppler/-/commit/8677500399fc2548fa816b619580c2c07915a98c": "pkg:gitlab/gitlab.freedesktop.org/poppler/poppler@8677500399fc2548fa816b619580c2c07915a98c?repository_url=https://gitlab.freedesktop.org/poppler/poppler",
283-
"https://salsa.debian.org/apt-team/apt/-/commit/dceb1e49e4b8e4dadaf056be34088b415939cda6": "pkg:gitlab/salsa.debian.org/apt-team/apt@dceb1e49e4b8e4dadaf056be34088b415939cda6?repository_url=https://salsa.debian.org/apt-team/apt",
280+
"https://git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm/-/commit/a5f07894058c4198f61e533d727b343c5be879b0": "pkg:gitlab/clo/qsdk/oss/kernel/linux-msm@a5f07894058c4198f61e533d727b343c5be879b0?repository_url=https://git.codelinaro.org",
281+
"https://gitlab.gnome.org/GNOME/gimp/-/commit/112a5e038f0646eae5ae314988ec074433d2b365": "pkg:gitlab/gnome/gimp@112a5e038f0646eae5ae314988ec074433d2b365?repository_url=https://gitlab.gnome.org",
282+
"https://gitlab.freedesktop.org/poppler/poppler/-/commit/8677500399fc2548fa816b619580c2c07915a98c": "pkg:gitlab/poppler/poppler@8677500399fc2548fa816b619580c2c07915a98c?repository_url=https://gitlab.freedesktop.org",
283+
"https://salsa.debian.org/apt-team/apt/-/commit/dceb1e49e4b8e4dadaf056be34088b415939cda6": "pkg:gitlab/apt-team/apt@dceb1e49e4b8e4dadaf056be34088b415939cda6?repository_url=https://salsa.debian.org",
284284
"https://gitea.com/htc47/entur/commit/271b852cfb761a1fe257aa0f0a12ff38bd8bfd1c": "pkg:generic/gitea.com/htc47/entur@271b852cfb761a1fe257aa0f0a12ff38bd8bfd1c",
285285
"https://codeberg.org/alpinelinux/aports/commit/a40a9732c840e5a324fba78b0ff7980b497c3831": "pkg:generic/codeberg.org/alpinelinux/aports@a40a9732c840e5a324fba78b0ff7980b497c3831",
286286
"https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=477734000555ffc24bf873952e40367deee26f17": "pkg:generic/git.kernel.org/pub/scm/utils/b4/b4@477734000555ffc24bf873952e40367deee26f17",
@@ -304,9 +304,9 @@
304304
"https://git.openssl.org/gitweb/?p=openssl.git;a=commitdiff;h=1ad73b4d27bd8c1b369a3cd453681d3a4f1bb9b2": "pkg:generic/git.openssl.org/gitweb/openssl@1ad73b4d27bd8c1b369a3cd453681d3a4f1bb9b2",
305305
"https://sourceware.org/git/?p=bunsen.git;a=commit;h=6c55933f37099517e050c923527b0b2267e1deed": "pkg:generic/sourceware.org/git/bunsen@6c55933f37099517e050c923527b0b2267e1deed",
306306
"https://gitbox.apache.org/repos/asf?p=xalan-java.git;a=commit;h=da3e0d06b467247643ce04e88d3346739d119f21": "pkg:generic/gitbox.apache.org/repos/asf/xalan-java@da3e0d06b467247643ce04e88d3346739d119f21",
307-
"https://git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm": "pkg:gitlab/git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm?repository_url=https://git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm",
308-
"https://gitlab.gnome.org/GNOME/gimp": "pkg:gitlab/gitlab.gnome.org/gnome/gimp?repository_url=https://gitlab.gnome.org/GNOME/gimp",
309-
"https://gitlab.freedesktop.org/poppler/poppler": "pkg:gitlab/gitlab.freedesktop.org/poppler/poppler?repository_url=https://gitlab.freedesktop.org/poppler/poppler",
307+
"https://git.codelinaro.org/clo/qsdk/oss/kernel/linux-msm": "pkg:gitlab/clo/qsdk/oss/kernel/linux-msm?repository_url=https://git.codelinaro.org",
308+
"https://gitlab.gnome.org/GNOME/gimp": "pkg:gitlab/gnome/gimp?repository_url=https://gitlab.gnome.org",
309+
"https://gitlab.freedesktop.org/poppler/poppler": "pkg:gitlab/poppler/poppler?repository_url=https://gitlab.freedesktop.org",
310310
"https://gitea.com/htc47/entur": "pkg:generic/gitea.com/htc47/entur",
311311
"https://codeberg.org/alpinelinux/aports": "pkg:generic/codeberg.org/alpinelinux/aports",
312312
"https://git.kernel.org/pub/scm/utils/b4/b4.git": "pkg:generic/git.kernel.org/pub/scm/utils/b4/b4",

0 commit comments

Comments
 (0)