-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
bbd70e6
58f2a84
1808c3d
f1548c1
5f05855
76d5ecd
ec558ce
493c74e
ab71c60
17b572b
12c1ae2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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' | ||
|
@@ -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. | ||
|
||
|
@@ -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 | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code does not comply to PEP8. PEP8Bear, severity NORMAL, section 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E225 missing whitespace around operator' PycodestyleBear (E225), severity NORMAL, section |
||
if force_body: | ||
|
@@ -260,6 +268,25 @@ def check_body(self, body, | |
'Commit body lines should not exceed {} ' | ||
'characters.'.format(body_line_length)) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code does not comply to PEP8. PEP8Bear, severity NORMAL, section 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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
There was a problem hiding this comment.
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
.