Skip to content

Commit

Permalink
Send announcements to stderr when not isatty only when the type annou…
Browse files Browse the repository at this point in the history
…ncement is an error.
  • Loading branch information
yeisonvargasf committed Nov 19, 2022
1 parent 6cd1ae3 commit be1a7a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 7 additions & 4 deletions safety/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from safety.safety import get_packages, read_vulnerabilities, fetch_policy, post_results
from safety.util import get_proxy_dict, get_packages_licenses, output_exception, \
MutuallyExclusiveOption, DependentOption, transform_ignore, SafetyPolicyFile, active_color_if_needed, \
get_processed_options, get_safety_version, json_alias, bare_alias, SafetyContext, is_a_remote_mirror
get_processed_options, get_safety_version, json_alias, bare_alias, SafetyContext, is_a_remote_mirror, \
filter_announcements

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -185,9 +186,11 @@ def check(ctx, key, db, full_report, stdin, files, cache, ignore, output, json,
full_report, packages)

# Announcements are send to stderr if not terminal, it doesn't depend on "exit_code" value
if announcements and (not sys.stdout.isatty() and os.environ.get("SAFETY_OS_DESCRIPTION", None) != 'run'):
LOG.info('sys.stdout is not a tty, announcements are going to be send to stderr')
click.secho(SafetyFormatter(output='text').render_announcements(announcements), fg="red", file=sys.stderr)
stderr_announcements = filter_announcements(announcements=announcements, by_type='error')
if stderr_announcements and (not sys.stdout.isatty() and os.environ.get("SAFETY_OS_DESCRIPTION", None) != 'run'):
LOG.info('sys.stdout is not a tty, error announcements are going to be send to stderr')
click.secho(SafetyFormatter(output='text').render_announcements(stderr_announcements), fg="red",
file=sys.stderr)

found_vulns = list(filter(lambda v: not v.ignored, vulns))
LOG.info('Vulnerabilities found (Not ignored): %s', len(found_vulns))
Expand Down
5 changes: 5 additions & 0 deletions safety/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def get_basic_announcements(announcements):
announcement.get('type', '').lower() != 'primary_announcement']


def filter_announcements(announcements, by_type='error'):
return [announcement for announcement in announcements if
announcement.get('type', '').lower() == by_type]


def build_telemetry_data(telemetry=True):
context = SafetyContext()

Expand Down

0 comments on commit be1a7a8

Please sign in to comment.