| 5 |
| _TAG_RE = re.compile(r"<[^>]+>") |
| 6 |
| 7 |
| 8 | def visible_length(html_text: str) -> int: |
| 9 | """Return the number of characters Telegram displays (tags stripped, entities decoded).""" |
| 10 | return len(unescape(_TAG_RE.sub("", html_text))) |
| 11 |
| 12 | CAPTION_BUDGET = 900 |
| 13 | CHANGELOG_IGNORE_MARKER = "[ignore]" |
| 14 |
| 15 | GROUPS = { |
| 16 | "feat": "✨ <b>Features</b>", |
| 17 | "fix": "🔧 <b>Fixes</b>", |
| 18 | "perf": "⚡ <b>Performance</b>", |
| 19 | "refactor": "♻️ <b>Refactor</b>", |
| 20 | "refa": "♻️ <b>Refactor</b>", |
| 21 | "docs": "📝 <b>Docs</b>", |
| 22 | "doc": "📝 <b>Docs</b>", |
| 23 | "test": "✅ <b>Tests</b>", |
| 24 | "build": "📦 <b>Build</b>", |
| 25 | "ci": "⚙️ <b>CI</b>", |
| 26 | "style": "💄 <b>Style</b>", |
| 27 | "chore": "🧹 <b>Chore</b>", |
| 28 | "revert": "⏪ <b>Revert</b>", |
| 29 | "merge": "🔀 <b>Merge</b>", |
| 30 | } |
| 31 | SUBJECT_RE = re.compile(r"^(?P<type>[A-Za-z]+)(?:\([^)]*\))?!?:\s*.+$") |
| 32 | APK_VERSION_RE = re.compile(r"-v(?P<name>.+)\((?P<code>\d+)\)-[^/]+\.apk$") |
| 33 | PR_REF_RE = re.compile(r"\(#(\d+)\)") |
| 34 |
| 35 |
| 36 | def linkify_prs(text: str, repository_url: str) -> str: |
| 37 | """Replace parenthesized PR references (e.g. PR 123 written as "(#123)") with HTML anchor links.""" |
| 38 | if not repository_url: |
| 39 | return text |
| 40 |
| 41 | def replace(m: re.Match) -> str: |
| 42 | number = m.group(1) |
| 43 | link = f'<a href="{escape(repository_url)}/pull/{number}">#{number}</a>' |
| 44 | return f"({link})" |
| 45 |
| 46 | return PR_REF_RE.sub(replace, text) |
| 47 |
| 48 | def is_changelog_ignored(commit_message: str) -> bool: |
| 49 | return CHANGELOG_IGNORE_MARKER in commit_message |
| 50 |
| 51 |
| 52 | def read_gradle_property(key: str, path: Path = Path("gradle.properties")) -> str: |
| 53 | prefix = f"{key}=" |
| 54 | with path.open(encoding="utf-8") as properties: |
| 55 | for line in properties: |
| 56 | if line.startswith(prefix): |
| 57 | return line.removeprefix(prefix).strip() |
| 58 | return "Unknown" |
| 59 |
| 60 |
| 61 | def read_apk_version(path: Path) -> tuple[str, int]: |
| 62 | match = APK_VERSION_RE.search(path.name) |
| 63 | if match is None: |
| 64 | raise ValueError(f"Cannot read version from APK filename: {path.name}") |
| 65 | return match.group("name"), int(match.group("code")) |
| 66 |
| 67 |
| 68 | def render_test_caption( |
| 69 | commit_message: str, |
| 70 | version_name: str, |
| 71 | version_code: str, |
| 72 | commit_sha: str = "", |
| 73 | repository_url: str = "", |
| 74 | ) -> str: |
| 75 | normalized_message = commit_message.replace("\r", "").strip() |
| 76 | header = ( |
| 77 | f"🧪 <b>Nagram</b> <code>{escape(version_name)}</code> " |
| 78 | f"<i>({escape(version_code)})</i> · <b>Test Version</b>" |
| 79 | ) |
| 80 | if is_changelog_ignored(normalized_message): |
| 81 | return header |
| 82 |
| 83 | subject, separator, detail = normalized_message.partition("\n") |
| 84 | subject = subject or "No commit metadata." |
| 85 | detail = detail.strip() if separator else "" |
| 86 | match = SUBJECT_RE.match(subject) |
| 87 | if match: |
| 88 | group = GROUPS.get(match.group("type").lower(), "📌 <b>Other</b>") |
| 89 | elif subject.lower().startswith("merge "): |
| 90 | group = GROUPS["merge"] |
| 91 | else: |
| 92 | group = "📌 <b>Other</b>" |
| 93 |
| 94 | commit_sha = commit_sha.strip() |
| 95 | repository_url = repository_url.rstrip("/") |
| 96 | if commit_sha: |
| 97 | chip = f"[{escape(commit_sha[:7])}]" |
| 98 | if repository_url: |
| 99 | chip = f'<a href="{escape(repository_url)}/commit/{escape(commit_sha)}">{chip}</a>' |
| 100 | else: |
| 101 | chip = f"<code>{chip}</code>" |
| 102 | prefix = f"• {chip} " |
| 103 | else: |
| 104 | prefix = "• " |
| 105 |
| 106 | def assemble(subject_text: str, detail_text: str = "") -> str: |
| 107 | rendered = ( |
| 108 | f"{header}\n\n{group}\n" |
| 109 | f"{prefix}{linkify_prs(escape(subject_text), repository_url)}" |
| 110 | ) |
| 111 | if detail_text: |
| 112 | rendered += ( |
| 113 | f"\n\n💬 <b>Detail</b>\n" |
| 114 | f"{linkify_prs(escape(detail_text), repository_url)}" |
| 115 | ) |
| 116 | return rendered |
| 117 |
| 118 | rendered = assemble(subject, detail) |
| 119 | if visible_length(rendered) <= CAPTION_BUDGET: |
| 120 | return rendered |
| 121 |
| 122 | low, high = 0, len(detail) |
| 123 | while low < high: |
| 124 | mid = (low + high + 1) // 2 |
| 125 | if visible_length(assemble(subject, detail[:mid].rstrip() + "…")) <= CAPTION_BUDGET: |
| 126 | low = mid |
| 127 | else: |
| 128 | high = mid - 1 |
| 129 | if low: |
| 130 | return assemble(subject, detail[:low].rstrip() + "…") |
| 131 | if visible_length(assemble(subject)) <= CAPTION_BUDGET: |
| 132 | return assemble(subject) |
| 133 |
| 134 | low, high = 0, len(subject) |
| 135 | while low < high: |
| 136 | mid = (low + high + 1) // 2 |
| 137 | if visible_length(assemble(subject[:mid].rstrip() + "…")) <= CAPTION_BUDGET: |
| 138 | low = mid |
| 139 | else: |
| 140 | high = mid - 1 |
| 141 | return assemble(subject[:low].rstrip() + "…") |
| 142 |