From 0ccd05d1f3deb32bc91cd749b9674a962c7ec83a Mon Sep 17 00:00:00 2001 From: Gordon Lin <75815453+gordlin@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:22:26 -0500 Subject: [PATCH] Slackbot: Escape special characters --- .github/slack-bot.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/slack-bot.py b/.github/slack-bot.py index aa45abf2..2a79a968 100644 --- a/.github/slack-bot.py +++ b/.github/slack-bot.py @@ -11,6 +11,9 @@ summarizer = pipeline('summarization', model='tuner007/pegasus_summarizer') neutral_summary = summarizer(pr_body, max_length=200, min_length=25, do_sample=False)[0]['summary_text'] +# Escape double quotes and backslashes for safe inclusion in environment variables +escaped_summary = neutral_summary.replace('\\', '\\\\').replace('"', '\\"') + # PR label types with associated metadata pr_label_types = { 'PR: Active': ['Active', 'https://github.com/search?q=org%3Aramp4-pcar4+state%3A%22open%22+type%3A%22pr%22+label%3A%22PR%3A+Active%22&type=pullrequests'], @@ -24,18 +27,19 @@ f"<{pr_label_types[label['name']][1]}|{pr_label_types[label['name']][0]}>" for label in pr_labels if pr_label_types.get(label['name']) ) -if (len(pr_label_str) == 0): +if len(pr_label_str) == 0: pr_label_str = "None" # Handle regular labels (not in pr_label_types) reg_label_str = ", ".join( label['name'] for label in pr_labels if pr_label_types.get(label['name']) is None ) -if (len(reg_label_str) == 0): +if len(reg_label_str) == 0: reg_label_str = "None" -# Write all results to environment +# Validate and write all results to the environment with open(os.environ['GITHUB_ENV'], 'a') as env_file: - env_file.write(f'NEUTRAL_SUMMARY={neutral_summary}\n') - env_file.write(f'PR_LABEL_STR={pr_label_str}\n') - env_file.write(f'REG_LABEL_STR={reg_label_str}\n') \ No newline at end of file + # Double-check escaped summary + env_file.write(f'NEUTRAL_SUMMARY="{escaped_summary}"\n') # Quotes around the value for safety + env_file.write(f'PR_LABEL_STR="{pr_label_str}"\n') # Ensure consistent escaping + env_file.write(f'REG_LABEL_STR="{reg_label_str}"\n') \ No newline at end of file