|
1 | 1 | """Utilities.""" |
2 | 2 |
|
3 | | -import inspect |
4 | 3 | import json |
5 | 4 | import re |
6 | 5 | import subprocess |
|
28 | 27 |
|
29 | 28 | from src.downloader.sources import APK_MIRROR_APK_CHECK |
30 | 29 | from src.exceptions import ScrapingError |
| 30 | +from src.metadata.github import GithubSourceMetadata |
31 | 31 |
|
32 | 32 | default_build = [ |
33 | 33 | "youtube", |
|
57 | 57 |
|
58 | 58 | updates_file = "updates.json" |
59 | 59 | updates_file_url = "https://raw.githubusercontent.com/{github_repository}/{branch_name}/{updates_file}" |
60 | | -changelogs: dict[str, dict[str, str]] = {} |
| 60 | +changelogs: dict[str, GithubSourceMetadata] = {} |
61 | 61 | time_zone = "Asia/Kolkata" |
62 | 62 | app_version_key = "app_version" |
63 | 63 | patches_versions_key = "patches_versions" |
@@ -97,60 +97,49 @@ def update_changelog(name: str, response: dict[str, str]) -> None: |
97 | 97 | in the dictionary represent the type of change (e.g., "bug fix", "feature", "documentation"), and |
98 | 98 | the values represent the specific changes made for each type. |
99 | 99 | """ |
100 | | - app_change_log = format_changelog(name, response) |
101 | | - changelogs[name] = app_change_log |
| 100 | + source_metadata = GithubSourceMetadata.from_json(response) |
| 101 | + changelogs[name] = source_metadata |
102 | 102 |
|
103 | 103 |
|
104 | | -def format_changelog(name: str, response: dict[str, str]) -> dict[str, str]: |
| 104 | +def format_changelog(metadata: GithubSourceMetadata) -> str: |
105 | 105 | """The `format_changelog` returns formatted changelog string. |
106 | 106 |
|
107 | 107 | Parameters |
108 | 108 | ---------- |
109 | | - name : str |
110 | | - The `name` parameter is a string that represents the name of the changelog. It is used to create a |
111 | | - collapsible section in the formatted changelog. |
112 | | - response : Dict[str, str] |
113 | | - The `response` parameter is a dictionary that contains information about a release. It has the |
114 | | - following keys: |
| 109 | + metadata : GithubSourceMetadata |
| 110 | + Represents the release metadata with sufficient info. |
115 | 111 |
|
116 | 112 | Returns |
117 | 113 | ------- |
118 | | - a formatted changelog as a dict. |
| 114 | + a formatted changelog as str |
119 | 115 | """ |
120 | | - final_name = f"[{name}]({response['html_url']})" |
121 | | - return { |
122 | | - "ResourceName": final_name, |
123 | | - "Version": response["tag_name"], |
124 | | - "Changelog": response["body"], |
125 | | - "PublishedOn": response["published_at"], |
126 | | - } |
| 116 | + content = ( |
| 117 | + f"# {metadata.name}\n\n" |
| 118 | + f"***Release Version: [{metadata.tag}]({metadata.html_url})*** \n" |
| 119 | + f"***Release Date: {metadata.get_release_date()}*** \n" |
| 120 | + f"<details>\n<summary><b><i>Changelog:</i></b></summary>\n\n{metadata.body}</details>\n\n" |
| 121 | + ) |
| 122 | + return f"{content}" |
127 | 123 |
|
128 | 124 |
|
129 | 125 | def write_changelog_to_file(updates_info: dict[str, Any]) -> None: |
130 | 126 | """The function `write_changelog_to_file` writes a given changelog json to a file.""" |
131 | | - markdown_table = inspect.cleandoc( |
132 | | - """ |
133 | | - | Resource Name | Version | Changelog | Published On | Build By| |
134 | | - |---------------|---------|-----------|--------------|---------| |
135 | | - """, |
136 | | - ) |
137 | | - for app_data in changelogs.values(): |
138 | | - name_link = app_data["ResourceName"] |
139 | | - version = app_data["Version"] |
140 | | - changelog = app_data["Changelog"] |
141 | | - published_at = app_data["PublishedOn"] |
142 | | - built_by = get_parent_repo() |
143 | | - |
144 | | - # Clean up changelog for markdown |
145 | | - changelog = changelog.replace("\r\n", "<br>") |
146 | | - changelog = changelog.replace("\n", "<br>") |
147 | | - changelog = changelog.replace("|", "\\|") |
148 | | - |
149 | | - # Add row to the Markdown table string |
150 | | - markdown_table += f"\n| {name_link} | {version} | {changelog} | {published_at} | {built_by} |" |
| 127 | + changelog_doc = "" |
| 128 | + metadata_list = GithubSourceMetadata.sort_by_latest_release(changelogs.values()) |
| 129 | + for data in metadata_list: |
| 130 | + changelog_doc += format_changelog(data) |
151 | 131 | with Path(changelog_file).open("w", encoding="utf_8") as file1: |
152 | | - file1.write(markdown_table) |
153 | | - Path(changelog_json_file).write_text(json.dumps(changelogs, indent=4) + "\n") |
| 132 | + file1.write(changelog_doc) |
| 133 | + |
| 134 | + def encoder_default(obj: Any) -> Any: |
| 135 | + if isinstance(obj, datetime): |
| 136 | + return obj.isoformat() |
| 137 | + if hasattr(obj, "__dict__"): |
| 138 | + return obj.__dict__ |
| 139 | + msg = f"Object of type {type(obj).__name__} is not JSON serializable" |
| 140 | + raise TypeError(msg) |
| 141 | + |
| 142 | + Path(changelog_json_file).write_text(json.dumps(changelogs, default=encoder_default, indent=4) + "\n") |
154 | 143 | Path(updates_file).write_text(json.dumps(updates_info, indent=4, default=str) + "\n") |
155 | 144 |
|
156 | 145 |
|
|
0 commit comments