|
34 | 34 | from cwe2.database import Database |
35 | 35 | from cwe2.database import InvalidCWEError |
36 | 36 | from packageurl import PackageURL |
| 37 | +from packageurl.contrib import purl2url |
37 | 38 | from packageurl.contrib.django.utils import without_empty_values |
| 39 | +from packageurl.contrib.url2purl import url2purl |
38 | 40 | from univers.version_range import RANGE_CLASS_BY_SCHEMES |
39 | 41 | from univers.version_range import AlpineLinuxVersionRange |
40 | 42 | from univers.version_range import NginxVersionRange |
@@ -873,17 +875,16 @@ def generate_patch_url(vcs_url, commit_hash): |
873 | 875 | """ |
874 | 876 | Generate patch URL from VCS URL and commit hash. |
875 | 877 | """ |
| 878 | + |
876 | 879 | if not vcs_url or not commit_hash: |
877 | | - return None |
| 880 | + return |
| 881 | + |
| 882 | + purl = url2purl(vcs_url) |
| 883 | + if not purl: |
| 884 | + return |
| 885 | + |
| 886 | + new_purl = PackageURL( |
| 887 | + type=purl.type, namespace=purl.namespace, name=purl.name, version=commit_hash |
| 888 | + ) |
878 | 889 |
|
879 | | - vcs_url = vcs_url.rstrip("/") |
880 | | - |
881 | | - if vcs_url.startswith("https://github.com"): |
882 | | - return f"{vcs_url}/commit/{commit_hash}.patch" |
883 | | - elif vcs_url.startswith("https://gitlab.com"): |
884 | | - return f"{vcs_url}/-/commit/{commit_hash}.patch" |
885 | | - elif vcs_url.startswith("https://bitbucket.org"): |
886 | | - return f"{vcs_url}/-/commit/{commit_hash}/raw" |
887 | | - elif vcs_url.startswith("https://git.kernel.org"): |
888 | | - return f"{vcs_url}.git/patch/?id={commit_hash}" |
889 | | - return |
| 890 | + return purl2url.get_patch_url(str(new_purl)) |
0 commit comments