Skip to content

Commit

Permalink
metadata: add codemeta
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorrell committed Dec 7, 2020
1 parent c86db04 commit 0c6c21a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
22 changes: 20 additions & 2 deletions invenio_github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions invenio_github/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ 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
return {}
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):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0c6c21a

Please sign in to comment.