Skip to content

Commit eef1893

Browse files
infer bug_tracking_url and code_view_url from vcs_url
Signed-off-by: Kaushik <kaushikrjpm10@gmail.com>
1 parent f620729 commit eef1893

42 files changed

Lines changed: 239 additions & 99 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/packagedcode/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ def from_data(cls, package_data, package_only=False):
768768
else:
769769
package_data.normalize_extracted_license_statement()
770770

771+
package_data.infer_vcs_urls()
772+
771773
return package_data
772774

773775
@property
@@ -857,6 +859,16 @@ def populate_license_fields(self):
857859

858860
self.normalize_extracted_license_statement()
859861

862+
def infer_vcs_urls(self):
863+
from packagedcode.utils import parse_vcs_urls
864+
if not self.vcs_url:
865+
return
866+
code_view, bug_track = parse_vcs_urls(self.vcs_url, version=self.version)
867+
if not self.code_view_url and code_view:
868+
self.code_view_url = code_view
869+
if not self.bug_tracking_url and bug_track:
870+
self.bug_tracking_url = bug_track
871+
860872
def update_purl_fields(self, package_data, replace=False):
861873

862874
if not self.type == package_data.type:

src/packagedcode/utils.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10+
from urllib.parse import urlsplit
11+
1012
from packageurl import PackageURL
1113

1214
try:
@@ -114,6 +116,62 @@ def normalize_vcs_url(repo_url, vcs_tool=None):
114116
return repo_url
115117

116118

119+
BUG_TRACKING_SUFFIXES = {
120+
'github.com': '/issues',
121+
'gitlab.com': '/-/issues',
122+
'codeberg.org': '/issues',
123+
'bitbucket.org': '/issues',
124+
}
125+
126+
# each platform has a different path scheme for browsing a tagged tree
127+
CODE_VIEW_SUFFIXES = {
128+
'github.com': '/tree/{version}',
129+
'gitlab.com': '/-/tree/{version}',
130+
'codeberg.org': '/src/tag/{version}',
131+
'bitbucket.org': '/src/{version}',
132+
}
133+
134+
135+
def parse_vcs_urls(vcs_url, version=None):
136+
"""
137+
Given a ``vcs_url`` and an optional ``version``, return a
138+
(code_view_url, bug_tracking_url) tuple for recognized hosting platforms,
139+
or (None, None) otherwise.
140+
141+
``code_view_url`` points to the source tree at a specific version tag and
142+
is only populated when a concrete ``version`` is provided.
143+
"""
144+
cleaned = normalize_vcs_url(vcs_url)
145+
if not cleaned:
146+
return None, None
147+
148+
if cleaned.endswith('.git'):
149+
cleaned = cleaned[:-4]
150+
151+
# urlsplit can't parse git@ SSH form like git@github.com:owner/repo
152+
if cleaned.startswith('git@'):
153+
cleaned = 'https://' + cleaned[4:].replace(':', '/')
154+
155+
parsed = urlsplit(cleaned)
156+
host = parsed.netloc.lower()
157+
path = parsed.path.rstrip('/')
158+
base = f'https://{host}{path}'
159+
160+
bug_suffix = BUG_TRACKING_SUFFIXES.get(host)
161+
if not bug_suffix:
162+
return None, None
163+
164+
bug_tracking_url = base + bug_suffix
165+
166+
code_view_url = None
167+
if version:
168+
code_suffix = CODE_VIEW_SUFFIXES.get(host)
169+
if code_suffix:
170+
code_view_url = base + code_suffix.format(version=version)
171+
172+
return code_view_url, bug_tracking_url
173+
174+
117175
def build_description(summary, description):
118176
"""
119177
Return a description string from a summary and description

tests/packagedcode/data/cargo/cargo_toml/clap/Cargo.toml.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"md5": null,
3434
"sha256": null,
3535
"sha512": null,
36-
"bug_tracking_url": null,
37-
"code_view_url": null,
36+
"bug_tracking_url": "https://github.com/clap-rs/clap/issues",
37+
"code_view_url": "https://github.com/clap-rs/clap/tree/2.32.0",
3838
"vcs_url": "https://github.com/clap-rs/clap",
3939
"copyright": null,
4040
"holder": null,

tests/packagedcode/data/cargo/cargo_toml/clippy/Cargo.toml.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
"md5": null,
6161
"sha256": null,
6262
"sha512": null,
63-
"bug_tracking_url": null,
64-
"code_view_url": null,
63+
"bug_tracking_url": "https://github.com/rust-lang/rust-clippy/issues",
64+
"code_view_url": "https://github.com/rust-lang/rust-clippy/tree/0.0.212",
6565
"vcs_url": "https://github.com/rust-lang/rust-clippy",
6666
"copyright": null,
6767
"holder": null,

tests/packagedcode/data/cargo/cargo_toml/mdbook/Cargo.toml.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"md5": null,
4646
"sha256": null,
4747
"sha512": null,
48-
"bug_tracking_url": null,
49-
"code_view_url": null,
48+
"bug_tracking_url": "https://github.com/rust-lang-nursery/mdBook/issues",
49+
"code_view_url": "https://github.com/rust-lang-nursery/mdBook/tree/0.2.4-alpha.0",
5050
"vcs_url": "https://github.com/rust-lang-nursery/mdBook",
5151
"copyright": null,
5252
"holder": null,

tests/packagedcode/data/cargo/cargo_toml/rustfmt/Cargo.toml.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"md5": null,
3636
"sha256": null,
3737
"sha512": null,
38-
"bug_tracking_url": null,
39-
"code_view_url": null,
38+
"bug_tracking_url": "https://github.com/rust-lang/rustfmt/issues",
39+
"code_view_url": "https://github.com/rust-lang/rustfmt/tree/1.0.3",
4040
"vcs_url": "https://github.com/rust-lang/rustfmt",
4141
"copyright": null,
4242
"holder": null,

tests/packagedcode/data/cargo/cargo_toml/rustup/Cargo.toml.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"md5": null,
3232
"sha256": null,
3333
"sha512": null,
34-
"bug_tracking_url": null,
35-
"code_view_url": null,
34+
"bug_tracking_url": "https://github.com/rust-lang/rustup.rs/issues",
35+
"code_view_url": "https://github.com/rust-lang/rustup.rs/tree/1.17.0",
3636
"vcs_url": "https://github.com/rust-lang/rustup.rs",
3737
"copyright": null,
3838
"holder": null,

tests/packagedcode/data/cargo/cargo_toml/single-file-scan/Cargo.toml.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"md5": null,
3838
"sha256": null,
3939
"sha512": null,
40-
"bug_tracking_url": null,
41-
"code_view_url": null,
40+
"bug_tracking_url": "https://github.com/cesarb/constant_time_eq/issues",
41+
"code_view_url": "https://github.com/cesarb/constant_time_eq/tree/0.4.2",
4242
"vcs_url": "https://github.com/cesarb/constant_time_eq",
4343
"copyright": null,
4444
"holder": null,
@@ -123,4 +123,4 @@
123123
"scan_errors": []
124124
}
125125
]
126-
}
126+
}

tests/packagedcode/data/cargo/scan-package-only.expected.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@
327327
"md5": null,
328328
"sha256": null,
329329
"sha512": null,
330-
"bug_tracking_url": null,
331-
"code_view_url": null,
330+
"bug_tracking_url": "https://github.com/indygreg/PyOxidizer/issues",
331+
"code_view_url": "https://github.com/indygreg/PyOxidizer/tree/0.4.0-pre",
332332
"vcs_url": "https://github.com/indygreg/PyOxidizer.git",
333333
"copyright": null,
334334
"holder": null,
@@ -669,8 +669,8 @@
669669
"md5": null,
670670
"sha256": null,
671671
"sha512": null,
672-
"bug_tracking_url": null,
673-
"code_view_url": null,
672+
"bug_tracking_url": "https://github.com/daac-tools/daachorse/issues",
673+
"code_view_url": "https://github.com/daac-tools/daachorse/tree/0.4.1",
674674
"vcs_url": "https://github.com/daac-tools/daachorse",
675675
"copyright": null,
676676
"holder": null,

tests/packagedcode/data/cargo/scan.expected.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"md5": null,
3131
"sha256": null,
3232
"sha512": null,
33-
"bug_tracking_url": null,
34-
"code_view_url": null,
33+
"bug_tracking_url": "https://github.com/indygreg/PyOxidizer/issues",
34+
"code_view_url": "https://github.com/indygreg/PyOxidizer/tree/0.4.0-pre",
3535
"vcs_url": "https://github.com/indygreg/PyOxidizer.git",
3636
"copyright": null,
3737
"holder": null,
@@ -128,8 +128,8 @@
128128
"md5": null,
129129
"sha256": null,
130130
"sha512": null,
131-
"bug_tracking_url": null,
132-
"code_view_url": null,
131+
"bug_tracking_url": "https://github.com/daac-tools/daachorse/issues",
132+
"code_view_url": "https://github.com/daac-tools/daachorse/tree/0.4.1",
133133
"vcs_url": "https://github.com/daac-tools/daachorse",
134134
"copyright": null,
135135
"holder": null,
@@ -1173,8 +1173,8 @@
11731173
"md5": null,
11741174
"sha256": null,
11751175
"sha512": null,
1176-
"bug_tracking_url": null,
1177-
"code_view_url": null,
1176+
"bug_tracking_url": "https://github.com/indygreg/PyOxidizer/issues",
1177+
"code_view_url": "https://github.com/indygreg/PyOxidizer/tree/0.4.0-pre",
11781178
"vcs_url": "https://github.com/indygreg/PyOxidizer.git",
11791179
"copyright": null,
11801180
"holder": null,
@@ -1540,8 +1540,8 @@
15401540
"md5": null,
15411541
"sha256": null,
15421542
"sha512": null,
1543-
"bug_tracking_url": null,
1544-
"code_view_url": null,
1543+
"bug_tracking_url": "https://github.com/daac-tools/daachorse/issues",
1544+
"code_view_url": "https://github.com/daac-tools/daachorse/tree/0.4.1",
15451545
"vcs_url": "https://github.com/daac-tools/daachorse",
15461546
"copyright": null,
15471547
"holder": null,

0 commit comments

Comments
 (0)