From 11e156619d0d820e534897bafd5f39e6f9defcbf Mon Sep 17 00:00:00 2001 From: Jon Lockwood Date: Sun, 8 Aug 2021 14:50:34 +0930 Subject: [PATCH] feat(gitlab_release): python module to check if a commit message has gitlab references in the footer #4 --- .../python-module/commit_footer/__main__.py | 7 + .../python-module/commit_footer/cli.py | 14 ++ .../python-module/commit_footer/commits.py | 123 ++++++++++++++++++ .../python-module/commit_footer/setup.py | 14 ++ 4 files changed, 158 insertions(+) create mode 100644 gitlab_release/python-module/commit_footer/__main__.py create mode 100644 gitlab_release/python-module/commit_footer/cli.py create mode 100644 gitlab_release/python-module/commit_footer/commits.py create mode 100644 gitlab_release/python-module/commit_footer/setup.py diff --git a/gitlab_release/python-module/commit_footer/__main__.py b/gitlab_release/python-module/commit_footer/__main__.py new file mode 100644 index 0000000..a352cdc --- /dev/null +++ b/gitlab_release/python-module/commit_footer/__main__.py @@ -0,0 +1,7 @@ +#-*- coding: utf-8 -*- + +from commit_footer.cli import main + +if __name__ == '__main__': + main() + diff --git a/gitlab_release/python-module/commit_footer/cli.py b/gitlab_release/python-module/commit_footer/cli.py new file mode 100644 index 0000000..dcfad25 --- /dev/null +++ b/gitlab_release/python-module/commit_footer/cli.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +#-*- coding: utf-8 -*- + +import sys +from commits import Commits + +def main(): + + commits = Commits() + + if commits.check(): + sys.exit(0) + else: + sys.exit(1) diff --git a/gitlab_release/python-module/commit_footer/commits.py b/gitlab_release/python-module/commit_footer/commits.py new file mode 100644 index 0000000..6a7584c --- /dev/null +++ b/gitlab_release/python-module/commit_footer/commits.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +#-*- coding: utf-8 -*- + +import os +import git +import re + +class Commits: + + + def __init__(self): + + self._repository = git.Repo(os.getcwd()) + + self._failed = [] + merge_base = self._repository.merge_base('development', 'changelog-footer-toggle') + self._merge_base = str(merge_base[0]) + + self.fetch_all() + + + def fetch_all(self) -> bool: # get the commits and filter to only the current branch + + commits = list(self._repository.iter_commits(self._repository.active_branch)) + + clean = True + Branch_commits = [] + for remove in commits: + + if str(remove).lower() == self._merge_base.lower(): + + clean = False + + if clean: + + Branch_commits.append(remove) + + self._commits = Branch_commits + + + def fetch(self, sha1:str) -> str: # fetch a single git message + + for commit in self._commits: + + if str(commit).lower() == sha1.lower(): + + return commit.message + + return '' + + + def footer(self, git_message:str) -> list: # Get the last line of the commit message if has more than 2 lines + + if git_message.count("\n") > 2: + + footer_line = git_message.split("\n") + footer_line = footer_line[(len(footer_line)-1)] + + footer = re.findall(r"([\!|\#][0-9]+)", str(git_message)) + + if len(footer) > 0: + return footer + else: + return False + + return None + + + def junit(self) -> bool: + junit = False + + junit_testsuites = ''.format(len(self._failed), len(self._commits)) + junit_testsuite = ''.format(len(self._failed), len(self._commits)) + + junit_testcase = '' + for commit in self._failed: + for key in commit: + junit_testcase += ''' + + {1} + + + + + + + '''.format(key, str(commit[key])) + + + if junit_testcase == '': + junit_testcase = '' + + junit_close = '' + print(str(junit_testsuites)) + print(str(junit_testsuite)) + print(str(junit_testcase)) + print(str(junit_close)) + + + def check(self) -> bool: + check = True + + start_check = False + + for commit in self._commits: + + if commit.message.count('\n') < 3: + continue + footer = self.footer(commit.message) + + if footer is False: + failed = {str(commit): str(commit.message)} + self._failed.append(failed) + continue + + self.junit() + + if len(self._failed) > 0: + check = False + + return check + + diff --git a/gitlab_release/python-module/commit_footer/setup.py b/gitlab_release/python-module/commit_footer/setup.py new file mode 100644 index 0000000..793d5e4 --- /dev/null +++ b/gitlab_release/python-module/commit_footer/setup.py @@ -0,0 +1,14 @@ +from setuptools import setup + +setup( + name='NFC commit footer validation', + version='0.1.0', + py_modules=['main'], + license='MIT', + long_description='this is a long description', + install_requires=['gitpython'], + entry_points = { + 'console_scripts': ['commit_footer=cli:main'], + }, +) +