Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve comment text formatting and error handling #45

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions github_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def comment(self, comment_text):
comment_text = re.sub(
r"([^a-zA-Z0-9_/])((?:[@][\w-]+)(?:[@.][\w.-]+)?)",
lambda m: f"{m.group(1)}`{m.group(2)}`",
comment_text
comment_text,
)
comment_text = re.sub(
r"([^a-zA-Z0-9_/])([#][\w]+)",
lambda m: f"{m.group(1)}`{m.group(2)}`",
comment_text
comment_text,
)
print(comment_text)
print(f"\nGitHub Comments:\n\n---\n{comment_text}")
if (
not comment_text
or not self.github_token
Expand All @@ -56,9 +56,7 @@ def comment(self, comment_text):
data = {"body": str(comment_body)}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 201:
raise Exception(
f"Failed to create github comment!, {response.json()}"
)
raise Exception(f"Failed to create github comment!, {response.json()}")
return True

def get_files(self):
Expand Down Expand Up @@ -112,16 +110,6 @@ def get_files(self):

gs = galaxy_social(args.preview, args.json_out)

lint_errors = []
for file_path in files_to_process:
result, status = gs.lint_markdown_file(file_path)
if not status:
lint_errors.append(file_path)
print(result)
if lint_errors:
github.comment(f"Please check your files: {', '.join(lint_errors)}")
sys.exit(1)

try:
message = gs.process_files(files_to_process)
github.comment(message)
Expand Down
61 changes: 37 additions & 24 deletions lib/galaxy_social.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,25 @@ def __init__(self, preview: bool, json_out: str):
module_name, class_name = plugin["class"].rsplit(".", 1)
module_path = f"{'lib.' if not os.path.dirname(os.path.abspath(sys.argv[0])).endswith('lib') else ''}plugins.{module_name}"

config = {}
if plugin.get("config"):
for key, value in plugin["config"].items():
if isinstance(value, str) and value.startswith("$"):
try:
config[key] = os.environ[value[1:]]
except KeyError:
print(
f"Missing environment variable {value[1:]} for {plugin['class']}."
)
else:
config[key] = value
else:
print(f"Missing config for {plugin['class']}.")

self.plugins_config_dict[plugin["name"].lower()] = (
module_path,
class_name,
config,
plugin.get("config", {}),
)

def init_plugin(self, plugin: str):
module_path, class_name, config = self.plugins_config_dict[plugin]
missing_env_vars = []
for key, value in config.items():
if isinstance(value, str) and value.startswith("$"):
if value[1:] in os.environ:
config[key] = os.environ[value[1:]]
else:
missing_env_vars.append(value[1:])
if missing_env_vars:
raise Exception(
f"Missing environment variables: {', '.join(missing_env_vars)} for {plugin} plugin."
)
try:
module = import_module(module_path)
plugin_class = getattr(module, class_name)
Expand All @@ -78,23 +74,30 @@ def lint_markdown_file(self, file_path):
return e, False

def parse_markdown_file(self, file_path):
errors = ""
result, status = self.lint_markdown_file(file_path)
if not status:
raise Exception(f"Failed to parse {file_path}.\n{result}")
return (
"",
{},
f"Please check your meradata schema.",
)

metadata, text = result

if "media" not in metadata:
raise Exception(f"Missing media in metadata of {file_path}.")
return (
"",
{},
f"Missing media in metadata.\nAdd media to metadata.",
)

metadata["media"] = [media.lower() for media in metadata["media"]]

for media in metadata["media"]:
if media not in self.plugins_config_dict:
raise Exception(
f"Invalid media {media} in {file_path}.\nConsider enabling/adding it in plugins.yml or check the spelling."
)
if media not in self.plugins:
errors += f"- Invalid media `{media}` in metadata. Consider check the spelling or enabling/adding it to `plugins.yml`.\n"
elif media not in self.plugins:
self.init_plugin(media)

metadata["mentions"] = (
Expand All @@ -108,6 +111,14 @@ def parse_markdown_file(self, file_path):
else {}
)

mentions_invalid = metadata["mentions"].keys() - metadata["media"]
if mentions_invalid:
errors += f"- Mentions for `{', '.join(mentions_invalid)}` social medias are not in medias list of metadata.\n"

hashtags_invalid = metadata["hashtags"].keys() - metadata["media"]
if hashtags_invalid:
errors += f"- Hashtags for `{', '.join(hashtags_invalid)}` social medias are not in medias list of metadata.\n"

image_pattern = re.compile(r"!\[(.*?)\]\((.*?)\)")
images = image_pattern.findall(text)
plain_content = re.sub(image_pattern, "", text).strip()
Expand All @@ -116,10 +127,12 @@ def parse_markdown_file(self, file_path):
{"url": image[1], "alt_text": image[0]} for image in images
]

return plain_content, metadata
return plain_content, metadata, errors

def process_markdown_file(self, file_path, processed_files):
content, metadata = self.parse_markdown_file(file_path)
content, metadata, errors = self.parse_markdown_file(file_path)
if errors:
return processed_files, f"Failed to process `{file_path}`.\n{errors}"
formatting_results = {}
for media in metadata["media"]:
try:
Expand Down