From 7b2e9ada5ff3223484bce9eecfd36f2fa264e4d2 Mon Sep 17 00:00:00 2001 From: Thomas Greenwood <89459745+addepar-tg@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:09:01 +0100 Subject: [PATCH] Compare refs both ways round, other small updates (#4) * add header print function * update images * pretty print * check commits both ways round * update readme * bump version to v0.2.0 --- README.md | 8 ++++---- addepar_redflag/redflag.py | 28 ++++++++++++++++------------ addepar_redflag/util/cli.py | 6 ++---- addepar_redflag/util/config.py | 10 +++++----- addepar_redflag/util/console.py | 16 ++++++++++++---- pyproject.toml | 2 +- 6 files changed, 40 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 864c7f2..a5126f6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
- RedFlag + RedFlag @@ -277,13 +277,13 @@ Say hi to Addepar Security Engineering at [security-engineering@addepar.com](mai [issues-shield]: https://img.shields.io/github/issues/Addepar/RedFlag.svg?style=for-the-badge [issues-url]: https://github.com/Addepar/RedFlag/issues [license-shield]: https://img.shields.io/github/license/Addepar/RedFlag.svg?style=for-the-badge -[license-url]: https://github.com/Addepar/RedFlag/blob/main/LICENSE.txt +[license-url]: https://github.com/Addepar/RedFlag/blob/main/LICENSE.md [linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555 [linkedin-url]: https://www.linkedin.com/company/addepar -[animated-report]: docs/images/Report-Animated.gif +[animated-report]: https://raw.githubusercontent.com/Addepar/RedFlag/main/docs/images/Report-Animated.gif [animated-report-url]: TBD -[batch-workflow]: docs/images/Batch-Workflow.png +[batch-workflow]: https://raw.githubusercontent.com/Addepar/RedFlag/main/docs/images/Batch-Workflow.png [docs-ci-mode]: https://img.shields.io/badge/Read%20More-How%20To%20Use%20RedFlag%20In%20CI%20Mode-blue?style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAMKADAAQAAAABAAAAMAAAAADbN2wMAAABcUlEQVRoBe1ZQU7DMBBsOPMC3gISDwDxAr5BJe5I/Ki3gkS58ROO9MKFdJZmLyOysWIzUNiVVs527Rl7JrnUi8V/iL7vr5CPyC1SFQ9NtMVu71U7Zp7qAwDQlLd4R94gT6pBCwCM0KJgajwFGPbaWCzjmW27e8o2B3gbwD6Vx/Ozg3/DuHEZHNvraDyKmugdU/+D6pZle2xXAuPBvkJ+BvuIl8hf9xF30Ttgu4/6ot6m67qzMa6pb8DXneNhhXz1H4Tj6Wwuc8BiDGDfHe+PrSv9vQS/1IFSTvm8PIBcciJMB0gQeZkOyCUnwnSABJGX6YBcciJMB0gQeZkOyCUnwnSABJGX6YBcciJMB0gQeZkOyCUnwnSABJGX6YBcciJMB0gQefnnHdiapPibW3IzM8e+KQdeBtDrOeA/vgbKXw6XDF/ekZVcQNQcogQ/vCMzcoDcYbit2UjtWtyRTe4z5MAhLpBrpF9841EWT+HmDr25A8AdiN2uwvv8AAAAAElFTkSuQmCC [docs-ci-mode-url]: https://github.com/Addepar/RedFlag/wiki/CI-Mode [docs-eval-mode]: https://img.shields.io/badge/Read%20More-How%20To%20Use%20RedFlag%20In%20Evaluation%20Mode-blue?style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAMKADAAQAAAABAAAAMAAAAADbN2wMAAABcUlEQVRoBe1ZQU7DMBBsOPMC3gISDwDxAr5BJe5I/Ki3gkS58ROO9MKFdJZmLyOysWIzUNiVVs527Rl7JrnUi8V/iL7vr5CPyC1SFQ9NtMVu71U7Zp7qAwDQlLd4R94gT6pBCwCM0KJgajwFGPbaWCzjmW27e8o2B3gbwD6Vx/Ozg3/DuHEZHNvraDyKmugdU/+D6pZle2xXAuPBvkJ+BvuIl8hf9xF30Ttgu4/6ot6m67qzMa6pb8DXneNhhXz1H4Tj6Wwuc8BiDGDfHe+PrSv9vQS/1IFSTvm8PIBcciJMB0gQeZkOyCUnwnSABJGX6YBcciJMB0gQeZkOyCUnwnSABJGX6YBcciJMB0gQeZkOyCUnwnSABJGX6YBcciJMB0gQefnnHdiapPibW3IzM8e+KQdeBtDrOeA/vgbKXw6XDF/ekZVcQNQcogQ/vCMzcoDcYbit2UjtWtyRTe4z5MAhLpBrpF9841EWT+HmDr25A8AdiN2uwvv8AAAAAElFTkSuQmCC diff --git a/addepar_redflag/redflag.py b/addepar_redflag/redflag.py index e93ab85..d751cd3 100644 --- a/addepar_redflag/redflag.py +++ b/addepar_redflag/redflag.py @@ -216,12 +216,22 @@ async def redflag( 'Retrieved PRs', MessageType.SUCCESS ) - - # Get all commits between from and to else: - count = 0 try: - compare = repository.compare(to_commit, from_commit) + # Get all commits between from and to + compare = repository.compare(from_commit, to_commit) + + # If there are no commits, try the other way around + if not compare.ahead_by: + compare = repository.compare(to_commit, from_commit) + + # If we can't find anything, exit + if not compare.ahead_by: + pretty_print( + 'No PRs to evaluate, exiting.', + MessageType.FATAL + ) + exit(0) except UnknownObjectException as e: pretty_print( f'Failed to find the to and from refs: {e}', @@ -229,13 +239,6 @@ async def redflag( ) exit(1) - if not compare.ahead_by: - pretty_print( - 'No PRs to evaluate, exiting.', - MessageType.FATAL - ) - exit(0) - # Flag for truncating SHA hashes in link text to_hash = match('^[a-f0-9]{40}$', to_commit) from_hash = match('^[a-f0-9]{40}$', from_commit) @@ -277,6 +280,7 @@ async def redflag( ) # Iterate over commits and create PR objects + count = 0 with progress: for commit in commits: lines = commit \ @@ -444,7 +448,7 @@ async def redflag( filename = f'{repository.full_name.replace("/", "_")}-{base_filename.replace("/", "-")}-{datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}' jinja = Environment( - loader=PackageLoader("redflag"), + loader=PackageLoader("addepar_redflag"), autoescape=select_autoescape() ) diff --git a/addepar_redflag/util/cli.py b/addepar_redflag/util/cli.py index 1648166..4dbb68a 100644 --- a/addepar_redflag/util/cli.py +++ b/addepar_redflag/util/cli.py @@ -16,6 +16,7 @@ ) from .console import ( pretty_print, + pretty_print_header, pretty_print_traceback, MessageType ) @@ -36,10 +37,7 @@ def common_arguments(parser, default_config): def cli(): - pretty_print( - 'Starting RedFlag 🚩', - MessageType.INFO - ) + pretty_print_header() # Load env var file, if present load_dotenv() diff --git a/addepar_redflag/util/config.py b/addepar_redflag/util/config.py index a2bf45c..1d743a3 100644 --- a/addepar_redflag/util/config.py +++ b/addepar_redflag/util/config.py @@ -2,16 +2,16 @@ from os import getenv from pathlib import Path -from .llm import ( - DEFAULT_ROLE, - DEFAULT_REVIEW_QUESTION, - DEFAULT_TEST_PLAN_QUESTION -) from .console import ( pretty_print, pretty_print_config_table, MessageType ) +from .llm import ( + DEFAULT_ROLE, + DEFAULT_REVIEW_QUESTION, + DEFAULT_TEST_PLAN_QUESTION +) def _update_nested_dict( base, diff --git a/addepar_redflag/util/console.py b/addepar_redflag/util/console.py index 53e9e48..23c54d5 100644 --- a/addepar_redflag/util/console.py +++ b/addepar_redflag/util/console.py @@ -20,10 +20,10 @@ class MessageType(Enum): # Define the styles with emojis styles = { - MessageType.SUCCESS: Style(color="green", bold=True), - MessageType.WARN: Style(color="yellow", bold=True), - MessageType.FATAL: Style(color="red", bold=True, underline=True), - MessageType.INFO: Style(color="blue", bold=True) + MessageType.SUCCESS: Style(color="green", bold=False), + MessageType.WARN: Style(color="yellow", bold=False), + MessageType.FATAL: Style(color="red", bold=False, underline=True), + MessageType.INFO: Style(color="blue", bold=False) } # Define the emojis @@ -42,6 +42,14 @@ def str2bool(string: str) -> bool: return string.lower() in ('yes', 'true', 't', 'y', '1') +# Helper function for standard formatting +def pretty_print_header(): + CONSOLE.print( + "Starting [bold]RedFlag 🚩[/bold]", + style=Style() + ) + + # Helper function for standard formatting def pretty_print( message: str, diff --git a/pyproject.toml b/pyproject.toml index 14a25e0..5126696 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "addepar-redflag" -version = "0.1.0" +version = "0.2.0" description = "RedFlag uses AI to identify high-risk code changes. Run it in batch mode for release candidate testing or in CI pipelines to flag PRs and add reviewers. RedFlag's flexible configuration makes it valuable for any team." authors = ["Addepar Security Engineering "] repository = "https://github.com/Addepar/RedFlag"