Skip to content
Merged
Changes from 2 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
20 changes: 14 additions & 6 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,12 @@ 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:
return _get_output(['git', 'config', '--get', 'remote.origin.url']).strip().split(':')[-1].rstrip('.git')
Comment thread
layfield-ccdc marked this conversation as resolved.
Outdated

Copilot AI Feb 11, 2026

Copy link

Choose a reason for hiding this comment

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

New behavior is introduced in get_repo() (CI vs local parsing) but there’s no unit test coverage for it. Since this file already contains unittest coverage for commit message validation, add tests for get_repo() (at least HTTPS and SSH remote formats), and also add a focused test that verifies opensource repos bypass only the Jira requirement (and still enforce file-size rules if that’s the intended policy).

Copilot uses AI. Check for mistakes.

def get_event():
'''Get the git event'''
Expand Down Expand Up @@ -183,7 +188,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 +467,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 +831,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 @@ -835,6 +840,9 @@ def check_commit_msg(message, files):
does not contain required marker.

'''
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
Comment thread
layfield-ccdc marked this conversation as resolved.
Outdated
if re.match(r'^Merge ((remote-tracking )?branch|commit) \'.+?\'( of [^\s]+)? into .+', message):
# Not checking for JIRA or large file in commit message generated by github
return 0
Expand Down Expand Up @@ -888,7 +896,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 +941,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