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

GitCommitBear: Block invalid email addresses #6

Open
wants to merge 11 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
2 changes: 2 additions & 0 deletions bear-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cmakelint~=1.3
cppclean~=0.12.0
cpplint~=1.3
dennis~=0.8
dnspython~=1.15.0
docutils-ast-writer~=0.1.2
eradicate~=0.1.6
guess-language-spirit~=0.5.2
Expand All @@ -24,6 +25,7 @@ proselint~=0.7.0
pycodestyle~=2.2
pydocstyle~=2.0
pyflakes~=1.5.0
pyisemail~=1.3.1
pylint~=1.6
pyroma~=2.2.0
pyyaml~=3.12
Expand Down
31 changes: 29 additions & 2 deletions bears/vcs/git/GitCommitBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import os
from urllib.parse import urlparse
from pyisemail import is_email

from coalib.bears.GlobalBear import GlobalBear
from dependency_management.requirements.PipRequirement import PipRequirement
Expand All @@ -15,7 +16,8 @@

class GitCommitBear(GlobalBear):
LANGUAGES = {'Git'}
REQUIREMENTS = {PipRequirement('nltk', '3.2')}
REQUIREMENTS = {PipRequirement('nltk', '3.2'),
PipRequirement('pyisemail', '1.3.1')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
Expand Down Expand Up @@ -223,7 +225,9 @@ def check_body(self, body,
body_line_length: int=72,
force_body: bool=False,
ignore_length_regex: typed_list(str)=(),
body_regex: str=None):
body_regex: str=None,
valid_email_check: bool=True,
email_dns_check: bool=False):
"""
Checks the given commit body.

Expand All @@ -236,6 +240,10 @@ def check_body(self, body,
expressions in this list will be ignored.
:param body_regex: If provided, checks the presence of regex
in the commit body.
:param valid_email_check: If True, it checks if emails are in valid
format or not
:param email_dns_check: If True, it checks if domains in the emails
are valid or not
"""
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E225 missing whitespace around operator'

PycodestyleBear (E225), severity NORMAL, section autopep8.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

PEP8Bear, severity NORMAL, section autopep8.

The issue can be fixed by applying the following patch:

--- a/bears/vcs/git/GitCommitBear.py
+++ b/bears/vcs/git/GitCommitBear.py
@@ -274,8 +274,8 @@
             for line in body:
                 for email in re.findall(r'\S*@\S*', line):
                     email_check = re.match(r"(^[a-zA-Z0-9_.+-]"
-                                           +r"+@[a-zA-Z0-9-]+\."
-                                           +r"[a-zA-Z0-9-.]+$)", email)
+                                           + r"+@[a-zA-Z0-9-]+\."
+                                           + r"[a-zA-Z0-9-.]+$)", email)
                     # if eamil do not match regex there is no need to
                     # look up dns
                     if email_dns_check and email_check:

if len(body) == 0:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E225 missing whitespace around operator'

PycodestyleBear (E225), severity NORMAL, section autopep8.

if force_body:
Expand All @@ -260,6 +268,25 @@ def check_body(self, body,
'Commit body lines should not exceed {} '
'characters.'.format(body_line_length))

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E128 continuation line under-indented for visual indent'

PycodestyleBear (E128), severity NORMAL, section autopep8.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

PEP8Bear, severity NORMAL, section autopep8.

The issue can be fixed by applying the following patch:

--- a/bears/vcs/git/GitCommitBear.py
+++ b/bears/vcs/git/GitCommitBear.py
@@ -280,7 +280,7 @@
                     # look up dns
                     if email_dns_check and email_check:
                         email_check = not is_email(email, diagnose=True,
-                                               check_dns=True).ERROR_CODES
+                                                   check_dns=True).ERROR_CODES
                     if not email_check:
                         invalid_emails = True
                         result_message += ' ' + email + '\n'

if valid_email_check:
result_message = 'Body contains these invalid emails:\n'
invalid_emails = False
for line in body:
for email in re.findall(r'\S*@\S*', line):
email_check = re.match(r"(^[a-zA-Z0-9_.+-]"
+r"+@[a-zA-Z0-9-]+\."
+r"[a-zA-Z0-9-.]+$)", email)
# if eamil do not match regex there is no need to
# look up dns
if email_dns_check and email_check:
email_check = not is_email(email, diagnose=True,
check_dns=True).ERROR_CODES
if not email_check:
invalid_emails = True
result_message += ' ' + email + '\n'
if invalid_emails:
yield Result(self, result_message)

def check_issue_reference(self, body,
body_close_issue: bool=False,
body_close_issue_full_url: bool=False,
Expand Down
18 changes: 18 additions & 0 deletions tests/vcs/git/GitCommitBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,24 @@ def test_body_checks(self):
body_regex=r'TICKER\s*CLOSE\s+[1-9][0-9]*'), [])
self.assert_no_msgs()

# Testing if check recognises invalid emails
self.git_commit('Shortlog\n\n'
'invalid#!format!*[email protected]\n'
'another.*invalid@format\n'
'[email protected]\n'
'[email protected]\n'
'Fix 2017')
message = 'Body contains these invalid emails:\n'
message += ' invalid#!format!*[email protected]\n'
message += ' another.*invalid@format\n'
# Test with email_dns_check set to False
self.assertEqual(self.run_uut(), [message])
# Test with email_dns_check set to True
message += " [email protected]\n"
self.assertEqual(self.run_uut(email_dns_check=True), [message])
# Test with valid_email_check set to False
self.assertEqual(self.run_uut(valid_email_check=False), [])

def test_check_issue_reference(self):
# Commit with no remotes configured
self.git_commit('Shortlog\n\n'
Expand Down