Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary/github.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Any, Tuple | from typing import Any, Tuple | ||||
from rdflib import RDF, BNode, Graph, Literal, URIRef | from rdflib import RDF, BNode, Graph, Literal, URIRef | ||||
from swh.indexer.codemeta import CROSSWALK_TABLE | from swh.indexer.codemeta import CROSSWALK_TABLE | ||||
from swh.indexer.namespaces import ACTIVITYSTREAMS, CODEMETA, FORGEFED, SCHEMA | from swh.indexer.namespaces import ACTIVITYSTREAMS, CODEMETA, FORGEFED, SCHEMA | ||||
from .base import BaseExtrinsicMapping, JsonMapping, produce_terms | from .base import BaseExtrinsicMapping, JsonMapping, produce_terms | ||||
from .utils import prettyprint_graph # noqa | from .utils import add_url_if_valid, prettyprint_graph # noqa | ||||
SPDX = URIRef("https://spdx.org/licenses/") | SPDX = URIRef("https://spdx.org/licenses/") | ||||
class GitHubMapping(BaseExtrinsicMapping, JsonMapping): | class GitHubMapping(BaseExtrinsicMapping, JsonMapping): | ||||
name = "github" | name = "github" | ||||
mapping = { | mapping = { | ||||
**CROSSWALK_TABLE["GitHub"], | **CROSSWALK_TABLE["GitHub"], | ||||
Show All 17 Lines | class GitHubMapping(BaseExtrinsicMapping, JsonMapping): | ||||
def extrinsic_metadata_formats(cls) -> Tuple[str, ...]: | def extrinsic_metadata_formats(cls) -> Tuple[str, ...]: | ||||
return ("application/vnd.github.v3+json",) | return ("application/vnd.github.v3+json",) | ||||
def extra_translation(self, graph, root, content_dict): | def extra_translation(self, graph, root, content_dict): | ||||
graph.remove((root, RDF.type, SCHEMA.SoftwareSourceCode)) | graph.remove((root, RDF.type, SCHEMA.SoftwareSourceCode)) | ||||
graph.add((root, RDF.type, FORGEFED.Repository)) | graph.add((root, RDF.type, FORGEFED.Repository)) | ||||
if content_dict.get("has_issues"): | if content_dict.get("has_issues"): | ||||
graph.add( | add_url_if_valid( | ||||
( | graph, | ||||
root, | root, | ||||
CODEMETA.issueTracker, | CODEMETA.issueTracker, | ||||
URIRef(content_dict["html_url"] + "/issues"), | URIRef(content_dict["html_url"] + "/issues"), | ||||
) | ) | ||||
) | |||||
def get_root_uri(self, content_dict: dict) -> URIRef: | def get_root_uri(self, content_dict: dict) -> URIRef: | ||||
if isinstance(content_dict.get("html_url"), str): | if isinstance(content_dict.get("html_url"), str): | ||||
return URIRef(content_dict["html_url"]) | return URIRef(content_dict["html_url"]) | ||||
else: | else: | ||||
raise ValueError( | raise ValueError( | ||||
f"GitHub metadata has missing/invalid html_url: {content_dict}" | f"GitHub metadata has missing/invalid html_url: {content_dict}" | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 75 Lines • Show Last 20 Lines |