Skip to content

Commit

Permalink
update-dashboard-issue, update-language-issues: add date updated (#140)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastiaan Speck <[email protected]>
Co-authored-by: Sebastiaan Speck <[email protected]>
  • Loading branch information
3 people authored Oct 7, 2024
1 parent c7d5081 commit 135a95c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
33 changes: 32 additions & 1 deletion scripts/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from enum import Enum
from pathlib import Path
from datetime import datetime, timezone
import os
import re
import json
Expand Down Expand Up @@ -149,7 +150,12 @@ def get_github_issue(title: str = None) -> list[dict]:
data = json.loads(result.stdout)

simplified_data = [
{"number": issue["number"], "title": issue["title"], "url": issue["html_url"]}
{
"number": issue["number"],
"title": issue["title"],
"body": issue["body"],
"url": issue["html_url"],
}
for issue in data
]

Expand All @@ -159,6 +165,7 @@ def get_github_issue(title: str = None) -> list[dict]:
{
"number": issue["number"],
"title": issue["title"],
"body": issue["body"],
"url": issue["html_url"],
}
for issue in data
Expand Down Expand Up @@ -206,6 +213,30 @@ def update_github_issue(issue_number, title, body):
return result


def get_datetime_pretty():
# Guarantee UTC to be fair to everyone, since we can't make this dynamic based on the browser's timezone
date = datetime.now(timezone.utc)
return date.strftime("%Y-%m-%d %H:%M:%S UTC")


def strip_dynamic_content(markdown):
"""
Removes any dynamic content enclosed within `<!-- __NOUPDATE__ -->` and `<!-- __END_NOUPDATE__ -->` tags from the provided Markdown string.
This function is used to remove any dynamic content (e.g. the last updated time) from the given string before updating a GitHub issue, ensuring that the issue content remains static if not *actual* content has changed
Args:
markdown (str): The Markdown content to be processed.
Returns:
str: The Markdown content with the dynamic content removed.
"""
regex = re.compile(
r"<!--\s*__NOUPDATE__(.|\n)*__END_NOUPDATE__\s*-->", re.MULTILINE
)
return re.sub(regex, "", markdown)


def replace_characters_for_link(page):
return str(
page.replace("[", "\\[")
Expand Down
17 changes: 16 additions & 1 deletion scripts/update-dashboard-issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pathlib import Path
from enum import Enum
from _common import (
get_datetime_pretty,
strip_dynamic_content,
get_github_issue,
update_github_issue,
generate_github_link,
Expand Down Expand Up @@ -168,7 +170,12 @@ def add_metric_details(lines, data, topic_name, topic, file_name):
def generate_dashboard(data):
DETAILS_OPENING = "<details>\n"
DETAILS_CLOSING = "\n</details>\n"
markdown = "# Translation Dashboard Status\n\n## Overview\n\n"

markdown = "# Translation Dashboard Status\n\n"
markdown += "<!-- __NOUPDATE__ -->\n"
markdown += f"**Last updated:** {get_datetime_pretty()}\n"
markdown += "<!-- __END_NOUPDATE__ -->\n"
markdown += "## Overview\n"
markdown += "| Metric | Value |\n"
markdown += "|--------|-------|\n"

Expand Down Expand Up @@ -236,6 +243,14 @@ def main():

markdown_content = generate_dashboard(parsed_data)

if strip_dynamic_content(markdown_content) == strip_dynamic_content(
issue_data["body"]
):
print(
"new issue body (sans dynamic content) identical to existing issue body, not updating"
)
sys.exit(0)

result = update_github_issue(
issue_data["number"], issue_title, markdown_content
)
Expand Down
14 changes: 14 additions & 0 deletions scripts/update-language-issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
get_tldr_root,
get_check_pages_dir,
get_locale,
get_datetime_pretty,
strip_dynamic_content,
create_github_issue,
get_github_issue,
update_github_issue,
Expand Down Expand Up @@ -72,6 +74,10 @@ def parse_language_directory(directory):

def generate_markdown_for_language(language, data):
markdown = f"## {language} language Issues\n"
markdown += "<!-- __NOUPDATE__ -->\n"
markdown += f"**Last updated:** {get_datetime_pretty()}\n"
markdown += "<!-- __END_NOUPDATE__ -->\n"

has_issues = False

for topic, items in data.items():
Expand Down Expand Up @@ -128,6 +134,14 @@ def main():
lang_data = parse_language_directory(lang_dir)
markdown_content += generate_markdown_for_language(locale, lang_data)

if strip_dynamic_content(markdown_content) == strip_dynamic_content(
issue_data["body"]
):
print(
f"new issue body (sans dynamic content) for language {locale} identical to existing issue body, not updating"
)
continue

update_github_issue(issue_data["number"], title, markdown_content)
else:
print("Not in a CI or incorrect repository, refusing to run.", file=sys.stderr)
Expand Down

0 comments on commit 135a95c

Please sign in to comment.