| 5 |
| CAPTION_BUDGET = 900 |
| 6 |
| 7 | GROUPS = { |
| 8 | "feat": "✨ <b>Features</b>", |
| 9 | "fix": "🔧 <b>Fixes</b>", |
| 10 | "perf": "⚡ <b>Performance</b>", |
| 11 | "refactor": "♻️ <b>Refactor</b>", |
| 12 | "refa": "♻️ <b>Refactor</b>", |
| 13 | "docs": "📝 <b>Docs</b>", |
| 14 | "doc": "📝 <b>Docs</b>", |
| 15 | "test": "✅ <b>Tests</b>", |
| 16 | "build": "📦 <b>Build</b>", |
| 17 | "ci": "⚙️ <b>CI</b>", |
| 18 | "style": "💄 <b>Style</b>", |
| 19 | "chore": "🧹 <b>Chore</b>", |
| 20 | "revert": "⏪ <b>Revert</b>", |
| 21 | "merge": "🔀 <b>Merge</b>", |
| 22 | } |
| 23 | SUBJECT_RE = re.compile(r"^(?P<type>[A-Za-z]+)(?:\([^)]*\))?!?:\s*.+$") |
| 24 |
| 25 |
| 26 | def read_gradle_property(key: str, path: Path = Path("gradle.properties")) -> str: |
| 27 | prefix = f"{key}=" |
| 28 | with path.open(encoding="utf-8") as properties: |
| 29 | for line in properties: |
| 30 | if line.startswith(prefix): |
| 31 | return line.removeprefix(prefix).strip() |
| 32 | return "Unknown" |
| 33 |
| 34 |
| 35 | def render_test_caption( |
| 36 | commit_message: str, |
| 37 | version_name: str, |
| 38 | version_code: str, |
| 39 | commit_sha: str = "", |
| 40 | repository_url: str = "", |
| 41 | ) -> str: |
| 42 | subject = ( |
| 43 | commit_message.replace("\r", "").strip().partition("\n")[0] |
| 44 | or "No commit metadata." |
| 45 | ) |
| 46 | match = SUBJECT_RE.match(subject) |
| 47 | if match: |
| 48 | group = GROUPS.get(match.group("type").lower(), "📌 <b>Other</b>") |
| 49 | elif subject.lower().startswith("merge "): |
| 50 | group = GROUPS["merge"] |
| 51 | else: |
| 52 | group = "📌 <b>Other</b>" |
| 53 | header = ( |
| 54 | f"🧪 <b>Nagram Test</b> <code>{escape(version_name)}</code> " |
| 55 | f"<i>({escape(version_code)})</i>" |
| 56 | ) |
| 57 |
| 58 | commit_sha = commit_sha.strip() |
| 59 | repository_url = repository_url.rstrip("/") |
| 60 | if commit_sha: |
| 61 | chip = f"[{escape(commit_sha[:7])}]" |
| 62 | if repository_url: |
| 63 | chip = f'<a href="{escape(repository_url)}/commit/{escape(commit_sha)}">{chip}</a>' |
| 64 | else: |
| 65 | chip = f"<code>{chip}</code>" |
| 66 | prefix = f"• {chip} " |
| 67 | else: |
| 68 | prefix = "• " |
| 69 |
| 70 | def assemble(text: str) -> str: |
| 71 | return f"{header}\n\n{group}\n{prefix}{escape(text)}" |
| 72 |
| 73 | rendered = assemble(subject) |
| 74 | if len(rendered) <= CAPTION_BUDGET: |
| 75 | return rendered |
| 76 |
| 77 | low, high = 0, len(subject) |
| 78 | while low < high: |
| 79 | mid = (low + high + 1) // 2 |
| 80 | if len(assemble(subject[:mid].rstrip() + "…")) <= CAPTION_BUDGET: |
| 81 | low = mid |
| 82 | else: |
| 83 | high = mid - 1 |
| 84 | return assemble(subject[:low].rstrip() + "…") |
| 85 |