diff --git a/README.md b/README.md index ad2d494..58a1fea 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ $ pip install -e . ### Main concepts: -The definition for each components is: +The definition for each component is: #### scheme ``` @@ -54,11 +54,12 @@ extra subpath within a package, relative to the package root. ``` ## Current `type` Support: -* GitHub, +* GitHub +* Gitlab * BitBucket -## How to Use: +## How to Use: ``` # install the package $ cd pypurl/ @@ -71,45 +72,45 @@ $ python # import main module >>> from pypurl.purl import Purl ->>> po = Purl() +>>> p = Purl() # parse the Download URL to dictionary ->>> po.durl_to_dict('https://github.com/vBarbaros/pypurl/archive/refs/heads/main.zip') +>>> p.durl_to_dict('https://github.com/vBarbaros/pypurl/archive/refs/heads/main.zip') {'scheme': 'pkg', 'type': 'github', 'namespace': 'vBarbaros', 'name': 'pypurl'} # obtain the PURL from the Download URL only >>> durl = 'https://github.com/vBarbaros/pypurl/archive/refs/heads/main.zip' ->>> po.durl_to_purl(durl) +>>> p.durl_to_purl(durl) 'pkg:github/vBarbaros/pypurl' # obtain the PURL from the Download URL + version number/hash-commit >>> durl = 'https://github.com/vBarbaros/pypurl/archive/refs/heads/main.zip' ->>> po.durl_to_purl(durl, '244fd47e07d1004f0aed9c') +>>> p.durl_to_purl(durl, '244fd47e07d1004f0aed9c') 'pkg:github/vBarbaros/pypurl@244fd47e07d1004f0aed9c' # obtain the PURL from the Download URL + version number/hash-commit + qualifiers ->>> po.durl_to_purl(durl, '244fd47e07d1004f0aed9c', 'param1=one¶m2=two¶m3=three') +>>> p.durl_to_purl(durl, '244fd47e07d1004f0aed9c', 'param1=one¶m2=two¶m3=three') 'pkg:github/vBarbaros/pypurl@244fd47e07d1004f0aed9c?param1=one¶m2=two¶m3=three' # obtain the PURL from the Download URL + version number/hash-commit + qualifiers + subpath ->>> po.durl_to_purl(durl, '244fd47e07d1004f0aed9c', 'param1=one¶m2=two¶m3=three', 'some/dummy/path') +>>> p.durl_to_purl(durl, '244fd47e07d1004f0aed9c', 'param1=one¶m2=two¶m3=three', 'some/dummy/path') 'pkg:github/vBarbaros/pypurl@244fd47e07d1004f0aed9c?param1=one¶m2=two¶m3=three#some/dummy/path' # parse the PURL containing main fields (+ version) to dictionary >>> purl = 'pkg:github/vBarbaros/pypurl@244fd47e07d1004f0aed9c' ->>> po.purl_to_dict(purl) +>>> p.purl_to_dict(purl) {'scheme': 'pkg', 'type': 'github', 'namespace': 'vBarbaros', 'name': 'pypurl', 'version': '244fd47e07d1004f0aed9c'} # parse the PURL containing all possible fields, to dictionary >>> purl = 'pkg:github/vBarbaros/pypurl@244fd47e07d1004f0aed9c?param1=one¶m2=two¶m3=three#some/dummy/path' ->>> po.purl_to_dict(purl) +>>> p.purl_to_dict(purl) {'scheme': 'pkg', 'type': 'github', 'namespace': 'vBarbaros', 'name': 'pypurl', 'version': '244fd47e07d1004f0aed9c', 'qualifiers': 'param1=one¶m2=two¶m3=three', 'subpath': 'some/dummy/path'} ``` @@ -120,9 +121,9 @@ While this module represents a simple exercise of mine based on some struggles I ## To-do -- add support for other repositories, besides the currently implemented (GitHub, BitBucket) +- add support for other repositories, besides the currently implemented (GitHub, Gitlab, BitBucket) ## [License](https://github.com/vBarbaros/pypurl/blob/main/LICENSE) -MIT © [Victor Barbaros](https://github.com/vBarbaros) +MIT © [Victor Barbaros, 2021](https://github.com/vBarbaros) diff --git a/tests/test_gitlab_purl.py b/tests/test_gitlab_purl.py new file mode 100644 index 0000000..5850741 --- /dev/null +++ b/tests/test_gitlab_purl.py @@ -0,0 +1,183 @@ +import unittest +from pypurl.purl import Purl + + +class GitlabPurlTestCase(unittest.TestCase): + def setUp(self): + self.purl_obj = Purl() + + def test_gitlab_purl_to_dict_version_success(self): + purl_with_version = 'pkg:gitlab/gitlab-org/gitlab-foss@v14.1.8' + r = self.purl_obj.purl_to_dict(purl_with_version) + self.assertEqual(r['scheme'], 'pkg') + self.assertEqual(r['type'], 'gitlab') + self.assertEqual(r['namespace'], 'gitlab-org') + self.assertEqual(r['name'], 'gitlab-foss') + self.assertEqual(r['version'], 'v14.1.8') + + def test_gitlab_purl_to_dict_version_qualifier_success(self): + purl_with_version_qualifiers = 'pkg:gitlab/gitlab-org/gitlab-foss@v14.1.8?param1=one,param2=two' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers) + self.assertEqual(r['scheme'], 'pkg') + self.assertEqual(r['type'], 'gitlab') + self.assertEqual(r['namespace'], 'gitlab-org') + self.assertEqual(r['name'], 'gitlab-foss') + self.assertEqual(r['version'], 'v14.1.8') + self.assertEqual(r['qualifiers'], 'param1=one,param2=two') + + def test_gitlab_purl_to_dict_version_qualifier_path_success(self): + purl_with_version_qualifiers_subpath = 'pkg:gitlab/gitlab-org/gitlab-foss@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r['scheme'], 'pkg') + self.assertEqual(r['type'], 'gitlab') + self.assertEqual(r['namespace'], 'gitlab-org') + self.assertEqual(r['name'], 'gitlab-foss') + self.assertEqual(r['version'], 'v14.1.8') + self.assertEqual(r['qualifiers'], 'param1=one,param2=two') + self.assertEqual(r['subpath'], '/src/main/sub-path') + + def test_gitlab_purl_to_dict_no_pkg_fail(self): + purl_with_version_qualifiers_subpath = 'gitlab/gitlab-org/gitlab-foss@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r, {}) + + def test_gitlab_purl_to_dict_no_type_fail(self): + purl_with_version_qualifiers_subpath = 'pkg:gitlab-org/gitlab-foss@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r, {}) + + def test_gitlab_purl_to_dict_no_namespace_fail(self): + purl_with_version_qualifiers_subpath = 'pkg:gitlab/gitlab-foss@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r, {}) + + def test_gitlab_purl_to_dict_no_name_fail(self): + purl_with_version_qualifiers_subpath = 'pkg:gitlab/gitlab-org@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r, {}) + + def test_gitlab_purl_to_dict_no_type_wrong_url_fail(self): + purl_with_version_qualifiers_subpath = 'pkg:/gitlab-org/gitlab-foss@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r, {}) + + def test_gitlab_purl_to_dict_no_namespace_wrong_url_fail(self): + purl_with_version_qualifiers_subpath = 'pkg:gitlab//gitlab-foss@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r, {}) + + def test_gitlab_purl_to_dict_no_name_wrong_url_fail(self): + purl_with_version_qualifiers_subpath = 'pkg:gitlab/gitlab-org/@v14.1.8?param1=one,param2=two#/src/main/sub-path' + r = self.purl_obj.purl_to_dict(purl_with_version_qualifiers_subpath) + self.assertEqual(r, {}) + + def test_gitlab_durl_to_dict_all_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v14.1.8/gitlab-foss-v14.1.8.zip' + r = self.purl_obj.durl_to_dict(durl, 'v14.1.8', 'param1=one,param2=two', '/src/main/sub-path') + self.assertEqual(r['scheme'], 'pkg') + self.assertEqual(r['type'], 'gitlab') + self.assertEqual(r['namespace'], 'gitlab-org') + self.assertEqual(r['name'], 'gitlab-foss') + self.assertEqual(r['version'], 'v14.1.8') + self.assertEqual(r['qualifiers'], 'param1=one,param2=two') + self.assertEqual(r['subpath'], '/src/main/sub-path') + + def test_gitlab_durl_to_dict_version_qualifier_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v14.1.8/gitlab-foss-v14.1.8.zip' + r = self.purl_obj.durl_to_dict(durl, 'v14.1.8', 'param1=one,param2=two') + self.assertEqual(r['scheme'], 'pkg') + self.assertEqual(r['type'], 'gitlab') + self.assertEqual(r['namespace'], 'gitlab-org') + self.assertEqual(r['name'], 'gitlab-foss') + self.assertEqual(r['version'], 'v14.1.8') + self.assertEqual(r['qualifiers'], 'param1=one,param2=two') + + def test_gitlab_durl_to_dict_version_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v14.1.8/gitlab-foss-v14.1.8.zip' + r = self.purl_obj.durl_to_dict(durl, 'v14.1.8') + self.assertEqual(r['scheme'], 'pkg') + self.assertEqual(r['type'], 'gitlab') + self.assertEqual(r['namespace'], 'gitlab-org') + self.assertEqual(r['name'], 'gitlab-foss') + self.assertEqual(r['version'], 'v14.1.8') + + def test_gitlab_durl_to_dict_main_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_dict(durl) + self.assertEqual(r['scheme'], 'pkg') + self.assertEqual(r['type'], 'gitlab') + self.assertEqual(r['namespace'], 'gitlab-org') + self.assertEqual(r['name'], 'gitlab-foss') + + def test_gitlab_durl_to_dict_main_wrong_http_fail(self): + durl = 'https:gitlab.com/gitlab-org/gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_dict(durl) + self.assertEqual(r, {}) + + def test_gitlab_durl_to_dict_main_wrong_type_fail(self): + durl = 'https://gitlab/gitlab-org/gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_dict(durl) + self.assertEqual(r, {}) + + def test_gitlab_durl_to_dict_main_wrong_namespace_fail(self): + durl = 'https://gitlab.com//gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_dict(durl) + self.assertEqual(r, {}) + + def test_gitlab_durl_to_dict_main_wrong_name_fail(self): + durl = 'https://gitlab.com/gitlab-org//-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_dict(durl) + self.assertEqual(r, {}) + + def test_gitlab_durl_to_purl_all_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_purl(durl, '9d8642e1d6b4d6c7de730f531278bc8e69b89b79', 'param1=one,param2=two', + '/src/main/sub-path') + p = 'pkg:gitlab/gitlab-org/gitlab-foss@9d8642e1d6b4d6c7de730f531278bc8e69b89b79?param1=one,param2=two#/src/main/sub-path' + self.assertEqual(r, p) + + def test_gitlab_durl_to_purl_version_qualifier_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_purl(durl, '9d8642e1d6b4d6c7de730f531278bc8e69b89b79', 'param1=one,param2=two') + p = 'pkg:gitlab/gitlab-org/gitlab-foss@9d8642e1d6b4d6c7de730f531278bc8e69b89b79?param1=one,param2=two' + self.assertEqual(r, p) + + def test_gitlab_durl_to_purl_version_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_purl(durl, '9d8642e1d6b4d6c7de730f531278bc8e69b89b79') + p = 'pkg:gitlab/gitlab-org/gitlab-foss@9d8642e1d6b4d6c7de730f531278bc8e69b89b79' + self.assertEqual(r, p) + + def test_gitlab_durl_to_purl_main_success(self): + durl = 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/master/gitlab-foss-master.zip' + r = self.purl_obj.durl_to_purl(durl) + p = 'pkg:gitlab/gitlab-org/gitlab-foss' + self.assertEqual(r, p) + + def test_gitlab_params_to_purl_all_success(self): + r = self.purl_obj.params_to_purl('gitlab', 'gitlab-org', 'gitlab-foss', + '9d8642e1d6b4d6c7de730f531278bc8e69b89b79', 'param1=one,param2=two', + '/src/main/sub-path') + p = 'pkg:gitlab/gitlab-org/gitlab-foss@9d8642e1d6b4d6c7de730f531278bc8e69b89b79?param1=one,param2=two#/src/main/sub-path' + self.assertEqual(r, p) + + def test_gitlab_params_to_purl_version_qualifier_success(self): + r = self.purl_obj.params_to_purl('gitlab', 'gitlab-org', 'gitlab-foss', + '9d8642e1d6b4d6c7de730f531278bc8e69b89b79', 'param1=one,param2=two') + p = 'pkg:gitlab/gitlab-org/gitlab-foss@9d8642e1d6b4d6c7de730f531278bc8e69b89b79?param1=one,param2=two' + self.assertEqual(r, p) + + def test_gitlab_params_to_purl_version_success(self): + r = self.purl_obj.params_to_purl('gitlab', 'gitlab-org', 'gitlab-foss', + '9d8642e1d6b4d6c7de730f531278bc8e69b89b79') + p = 'pkg:gitlab/gitlab-org/gitlab-foss@9d8642e1d6b4d6c7de730f531278bc8e69b89b79' + self.assertEqual(r, p) + + def test_gitlab_params_to_purl_main_success(self): + r = self.purl_obj.params_to_purl('gitlab', 'gitlab-org', 'gitlab-foss') + p = 'pkg:gitlab/gitlab-org/gitlab-foss' + self.assertEqual(r, p) + + +if __name__ == '__main__': + unittest.main()