Skip to content
Snippets Groups Projects

add color to NOK, OK, ???, and error prints

Merged Anjan Momi requested to merge anjandev/mrhlpr:anjan/color-print into master
All threads resolved!
Files
2
+ 74
34
@@ -3,6 +3,7 @@
"""Pretty outputs and such."""
import argparse
from enum import Enum, unique
import logging
import sys
from typing import Any
@@ -19,6 +20,41 @@ from . import mr
from . import version
@unique
class ColorCode(str, Enum):
OKGREEN = "\033[1;32m"
ORANGE = "\033[93m"
RED = "\033[91m"
ENDC = "\033[0m"
@unique
class ActionType(str, Enum):
OK = "[OK ]"
NOK = "[NOK]"
UNKNOWN = "[???]"
@property
def color(self) -> ColorCode:
match self:
case self.NOK:
return ColorCode.RED
case self.OK:
return ColorCode.OKGREEN
case self.UNKNOWN:
return ColorCode.ORANGE
case _:
raise ValueError(f"No defined color for action {self}")
def print_action(msg_type: ActionType, message: str) -> None:
print(f"{msg_type.color.value}{msg_type} {message}{ColorCode.ENDC.value}")
def print_error(message: str) -> None:
print(f"{ColorCode.RED.value}{message}{ColorCode.ENDC.value}")
def print_status(mr_id: int, no_cache: bool = False) -> None:
"""Print the merge request status. Most info is only visible, when the
branch is checked out locally. Always display a checklist of things to
@@ -66,103 +102,107 @@ def print_status(mr_id: int, no_cache: bool = False) -> None:
print()
if status.state == "closed":
print("ERROR: MR has been closed.")
print_error("ERROR: MR has been closed.")
exit(1)
elif status.state == "merged":
print("ERROR: MR has been merged.")
print_error("ERROR: MR has been merged.")
exit(1)
# Changes allowed by maintainers
if status.allow_push:
print("[OK ] Changes allowed")
action = ActionType.OK
else:
print("[NOK] Changes allowed")
action = ActionType.NOK
print_action(action, "Changes allowed")
# Clean worktree
if clean_worktree is None:
print("[???] Clean worktree")
action = ActionType.UNKNOWN
elif clean_worktree:
print("[OK ] Clean worktree")
action = ActionType.OK
else:
print("[NOK] Clean worktree")
action = ActionType.NOK
print_action(action, "Clean worktree")
# Rebase on target branch
if is_rebased is None:
print(f"[???] Rebase on {target_branch}")
action = ActionType.UNKNOWN
elif is_rebased:
print(f"[OK ] Rebase on {target_branch}")
action = ActionType.OK
else:
print(f"[NOK] Rebase on {target_branch}")
action = ActionType.NOK
print_action(action, f"Rebase on {target_branch}")
# MR-ID in all commit messages
if commits_have_id is None:
print("[???] MR-ID in commit msgs")
action = ActionType.UNKNOWN
elif commits_have_id:
print("[OK ] MR-ID in commit msgs")
action = ActionType.OK
else:
print("[NOK] MR-ID in commit msgs")
action = ActionType.NOK
print_action(action, "MR-ID in commit msgs")
# All commits follow formatting
if commits_follow_format is None:
print("[???] Commit subjects follow format")
for line in subj_err:
print(" " + line)
action = ActionType.UNKNOWN
elif commits_follow_format:
print("[OK ] Commit subjects follow format")
action = ActionType.OK
else:
print("[NOK] Commit subjects follow format")
for line in subj_err:
print(" " + line)
action = ActionType.NOK
print_action(action, "Commit subjects follow format")
for line in subj_err:
print(" " + line)
# Commits are signed
if commits_are_signed is None:
print("[???] Commits are signed")
action = ActionType.UNKNOWN
elif commits_are_signed:
print("[OK ] Commits are signed")
action = ActionType.OK
else:
print("[NOK] Commits are signed")
action = ActionType.NOK
print_action(action, "Commits are signed")
# Checklist
print()
print("Checklist:")
if not status.allow_push:
print(
print_error(
"* Ask MR author to tick 'Allow commits from members who can"
" merge to the target branch.'"
)
print("* Check again ('mrhlpr -n status')")
print_error("* Check again ('mrhlpr -n status')")
return
if not is_checked_out:
print("* Checkout this MR ('mrhlpr checkout " + str(mr_id) + "')")
print_error("* Checkout this MR ('mrhlpr checkout " + str(mr_id) + "')")
return
if not clean_worktree:
print("* Commit or stash changes in your worktree")
print("* Check again ('mrhlpr status')")
print_error("* Commit or stash changes in your worktree")
print_error("* Check again ('mrhlpr status')")
return
if len(commits) > 1:
print(
print_error(
f"* {len(commits)} commits: consider squashing"
f" ('git rebase -i origin/{target_branch}')"
)
if not is_rebased:
print(f"* Rebase on {target_branch} ('git" f" rebase origin/{target_branch}')")
print("* Check again ('mrhlpr status')")
print_error(f"* Rebase on {target_branch} ('git" f" rebase origin/{target_branch}')")
print_error("* Check again ('mrhlpr status')")
return
if not commits_have_id or not commits_are_signed:
print("* Add the MR-ID to all commits and sign them ('mrhlpr fixmsg')")
print_error("* Add the MR-ID to all commits and sign them ('mrhlpr fixmsg')")
return
if commits_follow_format is False:
print("* Fix commit subjects that don't follow the correct formatting")
print_error("* Fix commit subjects that don't follow the correct formatting")
return
if commits_follow_format is None:
print("* Manually check if the commit subjects are correct")
print_error("* Manually check if the commit subjects are correct")
print("* Pretty 'git log -" + str(len(commits)) + " --pretty'?" + " (consider copying MR desc)")
print("* Finish the MR: push, approve, merge, comment ('mrhlpr finish')")
Loading