diff --git a/api/gitlab_classes.py b/api/gitlab_classes.py index 38368a5d6836ed3b64bf14426e404713a2816a31..d41c96a3e09e0b18fb7d8bf71f793ee6e3daf1dc 100644 --- a/api/gitlab_classes.py +++ b/api/gitlab_classes.py @@ -1,4 +1,5 @@ from datetime import datetime +from functools import reduce from typing import ClassVar, Dict, Iterable, List, Optional, Set, Union from gitlab import Gitlab, MAINTAINER_ACCESS @@ -229,6 +230,30 @@ class GitlabCommit: deletions += diff['-'] return max(insertions, deletions) + @staticmethod + def _number_of_lines_too_long(lines, subject_line_length, message_line_length): + # noinspection PyUnusedLocal + lines_too_long: int + if len(lines) == 1: + lines_too_long = 0 if len(lines[0]) <= message_line_length else 1 + else: + lines_too_long = 0 if len(lines[0]) <= subject_line_length else 1 + lines_too_long += reduce((lambda x, y: x + y), + list(map(lambda line: 0 if len(line) <= message_line_length else 1, lines[1:]))) + return lines_too_long + + @staticmethod + def _has_blank_line_after_subject(lines): + if len(lines) == 1: + return True + else: + return lines[1] == '' + + def is_well_formatted(self, subject_line_length=72, message_line_length=72) -> bool: + lines: List[str] = self.get_message().rstrip('\n').split('\n') + return self._number_of_lines_too_long(lines, subject_line_length, message_line_length) == 0 \ + and self._has_blank_line_after_subject(lines) + # git_commit fields: # comments # discussions