Skip to content

Commit

Permalink
api: check git name and email is configuration
Browse files Browse the repository at this point in the history
(closes #216)
  • Loading branch information
jirikuncar committed Jul 11, 2018
1 parent f38a908 commit e8d2bb3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions renku/api/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def init_repository(

self.git.description = name or path.name

# Check that an author can be determined from Git.
from renku.models.datasets import Author
Author.from_git(self.git)

# TODO read existing gitignore and create a unique set of rules
import pkg_resources
gitignore_default = pkg_resources.resource_stream(
Expand Down
26 changes: 26 additions & 0 deletions renku/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ class ConfigurationError(RenkuException, click.ClickException):
"""Raise in case of misconfiguration."""


class MissingUsername(ConfigurationError):
"""Raise when the username is not configured."""

def __init__(self, message=None):
"""Build a custom message."""
message = message or (
'The user name is not configured. '
'Please use the "git config" command to configure it.\n\n'
'\tgit config --set user.name "John Doe"\n'
)
super(MissingUsername, self).__init__(message)


class MissingEmail(ConfigurationError):
"""Raise when the email is not configured."""

def __init__(self, message=None):
"""Build a custom message."""
message = message or (
'The email address is not configured. '
'Please use the "git config" command to configure it.\n\n'
'\tgit config --set user.email "john.doe@example.com"\n'
)
super(MissingUsername, self).__init__(message)


class AuthenticationError(RenkuException, click.ClickException):
"""Raise when there is a problem with authentication."""

Expand Down
24 changes: 20 additions & 4 deletions renku/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# limitations under the License.
"""Model objects representing datasets."""

import configparser
import datetime
import re
import uuid
Expand All @@ -26,6 +27,7 @@
from attr.validators import instance_of
from dateutil.parser import parse as parse_date

from renku import errors
from renku._compat import Path

from . import _jsonld as jsonld
Expand Down Expand Up @@ -84,10 +86,24 @@ def check_email(self, attribute, value):
def from_git(cls, git):
"""Create an instance from a Git repo."""
git_config = git.config_reader()
return cls(
name=git_config.get('user', 'name'),
email=git_config.get('user', 'email'),
)
try:
name = git_config.get_value('user', 'name', None)
email = git_config.get_value('user', 'email', None)
except configparser.NoOptionError: # pragma: no cover
raise errors.ConfigurationError(
'The user name and email are not configured. '
'Please use the "git config" command to configure them.\n\n'
'\tgit config --set user.name "John Doe"\n'
'\tgit config --set user.email "john.doe@example.com"\n'
)

# Check the git configuration.
if name is None: # pragma: no cover
raise errors.MissingUsername()
if email is None: # pragma: no cover
raise errors.MissingEmail()

return cls(name=name, email=email)

@classmethod
def from_commit(cls, commit):
Expand Down

0 comments on commit e8d2bb3

Please sign in to comment.