Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Codemeta #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions invenio_github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
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.4.0',
'email-validator>=1.0.5',
'github3.py==1.0.0a4',
'humanize>=0.5.1',
Expand Down