diff --git a/invenio_github/api.py b/invenio_github/api.py index 7ef0fb2..1298fab 100644 --- a/invenio_github/api.py +++ b/invenio_github/api.py @@ -27,6 +27,7 @@ import json import github3 +from convert_codemeta import crosswalk, validate_codemeta from flask import current_app from invenio_db import db from invenio_oauth2server.models import Token as ProviderToken @@ -44,7 +45,8 @@ from .errors import RepositoryAccessError from .models import ReleaseStatus, Repository from .tasks import sync_hooks -from .utils import get_extra_metadata, iso_utcnow, parse_timestamp, utcnow +from .utils import get_extra_metadata, iso_utcnow, \ + parse_timestamp, utcnow class GitHubAPI(object): @@ -419,15 +421,32 @@ def defaults(self): @cached_property def extra_metadata(self): - """Get extra metadata for file in repository.""" + """Get extra metadata from file in repository.""" return get_extra_metadata( self.gh.api, self.repository['owner']['login'], self.repository['name'], self.release['tag_name'], + current_app.config['GITHUB_METADATA_FILE'] ) + @cached_property + def codemeta(self): + """Get extra metadata from codemeta file in repository.""" + codemeta = get_extra_metadata( + self.gh.api, + self.repository['owner']['login'], + self.repository['name'], + self.release['tag_name'], + 'codemeta.json' + ) + if validate_codemeta(codemeta): + return crosswalk(codemeta, "codemeta", "Zenodo") + else: + return {} + + @cached_property def files(self): """Extract files to download from GitHub payload.""" @@ -468,6 +487,7 @@ def metadata(self): output = dict(self.defaults) if self.use_extra_metadata: output.update(self.extra_metadata) + output.update(self.codemeta) return output @cached_property diff --git a/invenio_github/utils.py b/invenio_github/utils.py index c1e045d..6285394 100644 --- a/invenio_github/utils.py +++ b/invenio_github/utils.py @@ -52,11 +52,11 @@ def parse_timestamp(x): return dt -def get_extra_metadata(gh, owner, repo_name, ref): +def get_extra_metadata(gh, owner, repo_name, ref, metadata_file): """Get the metadata file.""" try: content = gh.repository(owner, repo_name).file_contents( - path=current_app.config['GITHUB_METADATA_FILE'], ref=ref + path=metadata_file, ref=ref ) if not content: # File does not exists in the given ref @@ -64,7 +64,7 @@ def get_extra_metadata(gh, owner, repo_name, ref): return json.loads(content.decoded.decode('utf-8')) except ValueError: raise CustomGitHubMetadataError( - file=current_app.config['GITHUB_METADATA_FILE']) + file=metadata_file) def get_owner(gh, owner): diff --git a/setup.py b/setup.py index 250125b..9ea176b 100644 --- a/setup.py +++ b/setup.py @@ -69,6 +69,7 @@ 'Flask-Menu>=0.5.0', 'Flask>=1.0.4', 'WTForms-Alchemy>=0.15.0,<0.17', + 'convert-codemeta>=0.4.0', 'email-validator>=1.0.5', 'github3.py==1.0.0a4', 'humanize>=0.5.1',