From 0c6c21a44e8640ef853646f6ffb9240f02d23d63 Mon Sep 17 00:00:00 2001 From: Tom Morrell Date: Tue, 9 Jun 2020 21:27:20 +0000 Subject: [PATCH 1/2] metadata: add codemeta --- invenio_github/api.py | 22 ++++++++++++++++++++-- invenio_github/utils.py | 6 +++--- setup.py | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/invenio_github/api.py b/invenio_github/api.py index 1aa8a8c..6b40ff0 100644 --- a/invenio_github/api.py +++ b/invenio_github/api.py @@ -25,6 +25,7 @@ """Invenio module that adds GitHub integration to the platform.""" import github3 +from convert_codemeta import crosswalk from flask import current_app from invenio_db import db from invenio_oauth2server.models import Token as ProviderToken @@ -41,7 +42,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): @@ -406,14 +408,29 @@ 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.""" + metadata = get_extra_metadata( + self.gh.api, + self.repository['owner']['login'], + self.repository['name'], + self.release['tag_name'], + 'codemeta.json' + ) + return crosswalk(metadata, "codemeta", "Zenodo") + + @cached_property def files(self): """Extract files to download from GitHub payload.""" @@ -434,6 +451,7 @@ def metadata(self): """Return extracted metadata.""" output = dict(self.defaults) 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..3f47112 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.2.0', 'email-validator>=1.0.5', 'github3.py==1.0.0a4', 'humanize>=0.5.1', From 72451c0d59db238a6126ae6a842bf45a5de859db Mon Sep 17 00:00:00 2001 From: Tom Morrell Date: Wed, 9 Dec 2020 10:54:03 -0800 Subject: [PATCH 2/2] metadata: Add codemeta validation --- invenio_github/api.py | 10 ++++++---- setup.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/invenio_github/api.py b/invenio_github/api.py index 6b40ff0..9df0590 100644 --- a/invenio_github/api.py +++ b/invenio_github/api.py @@ -25,7 +25,7 @@ """Invenio module that adds GitHub integration to the platform.""" import github3 -from convert_codemeta import crosswalk +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 @@ -421,15 +421,17 @@ def extra_metadata(self): @cached_property def codemeta(self): """Get extra metadata from codemeta file in repository.""" - metadata = get_extra_metadata( + codemeta = get_extra_metadata( self.gh.api, self.repository['owner']['login'], self.repository['name'], self.release['tag_name'], 'codemeta.json' ) - return crosswalk(metadata, "codemeta", "Zenodo") - + if validate_codemeta(codemeta): + return crosswalk(codemeta, "codemeta", "Zenodo") + else: + return {} @cached_property def files(self): diff --git a/setup.py b/setup.py index 3f47112..9ea176b 100644 --- a/setup.py +++ b/setup.py @@ -69,7 +69,7 @@ 'Flask-Menu>=0.5.0', 'Flask>=1.0.4', 'WTForms-Alchemy>=0.15.0,<0.17', - 'convert-codemeta>=0.2.0', + 'convert-codemeta>=0.4.0', 'email-validator>=1.0.5', 'github3.py==1.0.0a4', 'humanize>=0.5.1',