Skip to content

Commit

Permalink
fix: overhaul emoji use and fallbacks in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Feb 13, 2022
1 parent 57b96f4 commit e521a56
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Added new `HARVEY_PATH`, `GIT_TIMEOUT`, `DEPLPOY_TIMEOUT`, and `DEPLOY_ON_TAG` env vars for customization
* Better syncing of the version string for the release and what appears in logs
* Better error handling around `404`s at the API level
* Emojis will now be converted to a fallback string representation in pipeline logs while they will remain emojis in Slack messages
* Various bug fixes and refactoring improvements

## v0.16.0 (2022-01-18)
Expand Down
7 changes: 0 additions & 7 deletions harvey/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,3 @@ class Config:
logger_name = 'harvey'
log_level = os.getenv('LOG_LEVEL', 'INFO').upper()
log_location = os.path.join(harvey_path, 'logs')

# Emoji (used for Slack messages, set defaults if slack isn't in use)
# TODO: Defaults are nice for when slack isn't in use; however, the emoji text will
# still show up in log files, we should be writting the fallback message to logs instead of emojis
work_emoji = ':hammer_and_wrench:' if use_slack else ''
success_emoji = ':white_check_mark:' if use_slack else 'Success!'
failure_emoji = ':skull_and_crossbones:' if use_slack else 'Failure!'
5 changes: 5 additions & 0 deletions harvey/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@


class Message:
# Emojis - used for Slack messages with sensible defaults if slack is not in use
work_emoji = ':hammer_and_wrench:' if Config.use_slack else ''
success_emoji = ':white_check_mark:' if Config.use_slack else 'Success!'
failure_emoji = ':skull_and_crossbones:' if Config.use_slack else 'Failure!'

@staticmethod
def send_slack_message(message: str):
"""Send a Slack message via a Slackbot."""
Expand Down
8 changes: 4 additions & 4 deletions harvey/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def initialize_pipeline(webhook: Dict[str, Any]) -> Tuple[Dict[str, Any], str, d
pipeline = Utils.kill(final_output, webhook)

pipeline_started_message = (
f'{Config.work_emoji} Harvey has started a `{config["pipeline"]}` pipeline for'
f'{Message.work_emoji} Harvey has started a `{config["pipeline"]}` pipeline for'
f' `{Webhook.repo_full_name(webhook)}`.'
)
if Config.use_slack:
Expand Down Expand Up @@ -106,9 +106,9 @@ def run_pipeline(webhook: Dict[str, Any]):
container_healthcheck = Container.run_container_healthcheck(docker_client, container, webhook)
container_healthcheck_statuses[container] = container_healthcheck
if container_healthcheck is True:
healthcheck_message = f'\n{container} Healthcheck: {Config.success_emoji}'
healthcheck_message = f'\n{container} Healthcheck: {Message.success_emoji}'
else:
healthcheck_message = f'\n{container} Healthcheck: {Config.failure_emoji}'
healthcheck_message = f'\n{container} Healthcheck: {Message.failure_emoji}'
healthcheck_messages += healthcheck_message

healthcheck_values = container_healthcheck_statuses.values()
Expand All @@ -128,7 +128,7 @@ def run_pipeline(webhook: Dict[str, Any]):
elif pipeline == 'pull':
# We simply assign the final message because if we got this far, the repo has already been pulled
pull_success_message = (
f'Harvey pulled {Webhook.repo_full_name(webhook)} successfully. {Config.success_emoji}'
f'Harvey pulled {Webhook.repo_full_name(webhook)} successfully. {Message.success_emoji}'
)
logger.info(pull_success_message)
final_output = f'{webhook_output}\n{pull_success_message}'
Expand Down
15 changes: 13 additions & 2 deletions harvey/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def kill(final_output: str, webhook: Dict[str, Any]):
logger.warning(failure_message)

pipeline_logs = final_output + failure_message
Utils.store_pipeline_details(webhook, pipeline_logs)
Utils.store_pipeline_details(webhook, Utils._strip_emojis_from_logs(pipeline_logs))

if Config.use_slack:
Message.send_slack_message(pipeline_logs)
Expand All @@ -41,7 +41,7 @@ def success(final_output: str, webhook: Dict[str, Any]):
logger.info(success_message)

pipeline_logs = final_output + success_message
Utils.store_pipeline_details(webhook, pipeline_logs)
Utils.store_pipeline_details(webhook, Utils._strip_emojis_from_logs(pipeline_logs))

if Config.use_slack:
Message.send_slack_message(pipeline_logs)
Expand All @@ -51,6 +51,17 @@ def success(final_output: str, webhook: Dict[str, Any]):
# Close the thread safely
sys.exit()

@staticmethod
def _strip_emojis_from_logs(output: str) -> str:
"""Replace the emojis for logs since they won't render properly there."""
logs_without_emoji = (
output.replace(Message.success_emoji, 'Success!')
.replace(Message.failure_emoji, 'Failure!')
.replace(Message.work_emoji, '')
)

return logs_without_emoji

@staticmethod
def store_pipeline_details(webhook: Dict[str, Any], final_output: str = 'NA'):
"""Store the pipeline's details including logs and metadata to a Sqlite database.
Expand Down

0 comments on commit e521a56

Please sign in to comment.