Skip to content
Merged
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
25 changes: 18 additions & 7 deletions main/githooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def get_text_file_content(filename):
data = _get_output(['git', 'show', f':{filename}'])
return data


def get_sha():
'''Get the commit sha

Expand All @@ -148,6 +147,14 @@ def get_sha():
'''
return _get_output(['git','rev-parse', get_branch()])

def get_repo():
'''Get the repo name in the format of "owner/repo"'''
if _is_github_event():
return os.environ['GITHUB_REPOSITORY']
else:
url = _get_output(['git', 'config', '--get', 'remote.origin.url'])
org = re.search(r'github\.com[:\/](.+?)(\.git)?$', url)
return org.group(1) if org else url

def get_event():
'''Get the git event'''
Expand Down Expand Up @@ -183,7 +190,7 @@ def get_commit_files():
commands += ['HEAD~..', '--']
else:
commands = ['git', 'diff-index', '--ignore-submodules', 'HEAD', '--cached']

output = _get_output(commands)
result = defaultdict(list)
for line in output.splitlines():
Expand Down Expand Up @@ -462,7 +469,7 @@ def test_trim_trailing_whitespace(self):
content = 'first line\nsecond line \nthird line '
trimmed_content = 'first line\nsecond line\nthird line'

name = NamedTemporaryFile().name
name = NamedTemporaryFile().name
try:
Path(name).write_text(content)
# Trailing whitespace found
Expand Down Expand Up @@ -826,7 +833,7 @@ def check_content(files):
return retval


def check_commit_msg(message, files):
def check_commit_msg(message, files, repo):
'''Check commit message (and file size).

Abort if file size exceeds hard (github.com) limit.
Expand All @@ -843,6 +850,10 @@ def check_commit_msg(message, files):
# Not checking for JIRA or large file in commit message generated by github
return 0

if re.match(r'^ccdc-opensource', repo):
# Do not check for JIRA in opensource repo as we don't want to require external contributors to do this
return 0

if NO_JIRA_MARKER not in message:
if jira_id_pattern.search(message) is None:
_fail('Every commit should contain a Jira issue ID or the text '
Expand Down Expand Up @@ -888,7 +899,7 @@ def _test(input, is_jira=True):
class TestCheckCommitMessage(unittest.TestCase):
def test_various_strings(self):
def _test(input, is_good=True):
rc = check_commit_msg(input, [])
rc = check_commit_msg(input, [], "dummy/repo")
self.assertEqual(rc == 0, is_good)
_test('ABC-1234')
_test('Some changes for ABC-1234 ticket')
Expand Down Expand Up @@ -933,6 +944,6 @@ def commit_msg_hook():
commit_message = Path(sys.argv[1]).read_text()

print(' Check commit message ...')
retval += check_commit_msg(commit_message, files['M'] + files['A'])
retval += check_commit_msg(commit_message, files['M'] + files['A'], get_repo())

return retval
return retval