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