Skip to content

Commit bc4f5f6

Browse files
authored
Enhance url2purl to support commit hashes parsing ( GitHub, GitLab, and Bitbucket ) (#211)
1 parent ef0a47a commit bc4f5f6

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

‎src/packageurl/contrib/url2purl.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,14 @@ def build_github_purl(url):
543543
# https://github.com/pombredanne/schematics.git
544544
git_pattern = r"https?://github.com/(?P<namespace>.+)/(?P<name>.+).(git)"
545545

546+
# https://github.com/<namespace>/<name>/commit/<sha>
547+
commit_pattern = (
548+
r"https?://github.com/"
549+
r"(?P<namespace>[^/]+)/(?P<name>[^/]+)/commit/(?P<version>[0-9a-fA-F]{7,40})/?$"
550+
)
551+
546552
patterns = (
553+
commit_pattern,
547554
archive_tags_pattern,
548555
archive_pattern,
549556
raw_pattern,
@@ -591,6 +598,13 @@ def build_github_purl(url):
591598
)
592599

593600

601+
# https://bitbucket.org/<namespace>/<name>/commits/<sha>
602+
bitbucket_commit_pattern = (
603+
r"https?://bitbucket.org/"
604+
r"(?P<namespace>[^/]+)/(?P<name>[^/]+)/commits/(?P<version>[0-9a-fA-F]{7,64})/?$"
605+
)
606+
607+
594608
@purl_router.route("https?://bitbucket\\.org/.*")
595609
def build_bitbucket_purl(url):
596610
"""
@@ -599,7 +613,18 @@ def build_bitbucket_purl(url):
599613
https://bitbucket.org/TG1999/first_repo/src/master or
600614
https://bitbucket.org/TG1999/first_repo/src or
601615
https://bitbucket.org/TG1999/first_repo/src/master/new_folder
616+
https://bitbucket.org/TG1999/first_repo/commits/16a60c4a74ef477cd8c16ca82442eaab2fbe8c86
602617
"""
618+
commit_matche = re.search(bitbucket_commit_pattern, url)
619+
if commit_matche:
620+
return PackageURL(
621+
type="bitbucket",
622+
namespace=commit_matche.group("namespace"),
623+
name=commit_matche.group("name"),
624+
version=commit_matche.group("version"),
625+
qualifiers={},
626+
subpath="",
627+
)
603628

604629
segments = get_path_segments(url)
605630

@@ -651,7 +676,26 @@ def build_gitlab_purl(url):
651676
https://gitlab.com/TG1999/firebase/-/tree
652677
https://gitlab.com/TG1999/firebase/-/master
653678
https://gitlab.com/tg1999/Firebase/-/tree/master
679+
https://gitlab.com/tg1999/Firebase/-/commit/bf04e5f289885cf2f20a92b387bcc6df33e30809
654680
"""
681+
# https://gitlab.com/<ns>/<name>/-/commit/<sha>
682+
commit_pattern = (
683+
r"https?://gitlab.com/"
684+
r"(?P<namespace>[^/]+)/(?P<name>[^/]+)/-/commit/"
685+
r"(?P<version>[0-9a-fA-F]{7,64})/?$"
686+
)
687+
688+
commit_matche = re.search(commit_pattern, url)
689+
if commit_matche:
690+
return PackageURL(
691+
type="gitlab",
692+
namespace=commit_matche.group("namespace"),
693+
name=commit_matche.group("name"),
694+
version=commit_matche.group("version"),
695+
qualifiers={},
696+
subpath="",
697+
)
698+
655699
segments = get_path_segments(url)
656700

657701
if not len(segments) >= 2:

‎tests/contrib/data/url2purl.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,5 +274,8 @@
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",
277-
"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"
277+
"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",
278+
"https://github.com/TG1999/first_repo/commit/98e516011d6e096e25247b82fc5f196bbeecff10": "pkg:github/tg1999/first_repo@98e516011d6e096e25247b82fc5f196bbeecff10",
279+
"https://gitlab.com/TG1999/first_repo/-/commit/bf04e5f289885cf2f20a92b387bcc6df33e30809": "pkg:gitlab/tg1999/first_repo@bf04e5f289885cf2f20a92b387bcc6df33e30809",
280+
"https://bitbucket.org/TG1999/first_repo/commits/16a60c4a74ef477cd8c16ca82442eaab2fbe8c86": "pkg:bitbucket/tg1999/first_repo@16a60c4a74ef477cd8c16ca82442eaab2fbe8c86"
278281
}

0 commit comments

Comments
 (0)