From ae0cd5d42d533f0a3971ce19da69b61d7f7aea0f Mon Sep 17 00:00:00 2001 From: Lacky1234union Date: Thu, 15 Oct 2026 19:23:09 +0300 Subject: [PATCH] =?UTF-8?q?feat(bot):=20=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8?= =?UTF-8?q?=D1=82=20=E2=80=94=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5,=20Push,=20=D0=BA=D0=BE=D0=BD=D1=84=D0=BB=D0=B8?= =?UTF-8?q?=D0=BA=D1=82=D1=8B,=20=D0=BF=D1=83=D0=B1=D0=BB=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B2=20=D1=80=D0=B5=D0=BF=D0=BE?= =?UTF-8?q?=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8=D0=B9=20=D0=B7=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=B7=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Publisher: пуши по одному (lock), fetch → проверка конфликта с base_sha → чистое дерево origin/main → черновик → коммит от tgit-bot с трейлером Telegram-User → push; отказ push → rebase и повтор (до 3 раз); защита от путей вне клона; лимит 20 пушей/час - «💬 Сообщение коммита» (1–200 символов, первая строка ≤ 72) и «🚀 Push» с подтверждением; исходы: запушено (sha, ссылка, ⏳ CI) · конфликт → «Перезаписать» · нечего коммитить · лимит · сбой - запись в pushes (chat_id, message_id — для обновления статуса CI/CD в #11), черновик очищается - ruff: RUF001–003 выключены — кириллица в текстах это норма Refs #9 Co-Authored-By: Claude Opus 5.5 --- app/bot/routers/commit.py | 84 ++++++++++++++++- app/config.py | 2 + app/main.py | 11 ++- app/repo/git.py | 4 + app/repo/publisher.py | 171 ++++++++++++++++++++++++++++++++++ app/repo/validate.py | 7 ++ app/texts.py | 16 ++++ config/texts.yaml | 25 +++++ pyproject.toml | 2 + tests/bot/test_commit_push.py | 65 +++++++++++++ tests/repo/test_publisher.py | 143 ++++++++++++++++++++++++++++ 11 files changed, 526 insertions(+), 4 deletions(-) create mode 100644 app/repo/publisher.py create mode 100644 tests/bot/test_commit_push.py create mode 100644 tests/repo/test_publisher.py diff --git a/app/bot/routers/commit.py b/app/bot/routers/commit.py index 965bd5e..866f8ee 100644 --- a/app/bot/routers/commit.py +++ b/app/bot/routers/commit.py @@ -20,10 +20,12 @@ from app.bot.menu import Nav, NavAction from app.db.models import ChangeOp, User from app.repo.drafts import DraftFull, DraftService, DraftView from app.repo.git import Entry, GitError, UploadsRepo +from app.repo.publisher import Publisher, PushResult, PushStatus from app.repo.validate import ( MAX_FILE_BYTES, ValidationError, check_content, + check_message, check_name, diff_stat, is_protected, @@ -53,6 +55,10 @@ class Act(StrEnum): UNSTAGE = "x" FORCE = "y" FIX = "z" + MESSAGE = "m" + PUSH = "ps" + PUSH_GO = "pg" + PUSH_FORCE = "pf" class CommitCb(CallbackData, prefix="c"): @@ -66,6 +72,7 @@ class Flow(StatesGroup): dir_name = State() content = State() syntax = State() + message = State() class Mark(StrEnum): @@ -201,7 +208,9 @@ def to_dir_keyboard(texts: Texts) -> InlineKeyboardMarkup: return kb.as_markup() -def build_router(texts: Texts, repo: UploadsRepo, drafts: DraftService) -> Router: +def build_router( + texts: Texts, repo: UploadsRepo, drafts: DraftService, publisher: Publisher, commit_url_template: str = "" +) -> Router: router = Router(name="commit") t = texts.commit @@ -236,15 +245,59 @@ def build_router(texts: Texts, repo: UploadsRepo, drafts: DraftService) -> Route ops = {ChangeOp.ADD: t.op_add, ChangeOp.MODIFY: t.op_modify, ChangeOp.DELETE: t.op_delete} lines = [t.draft_title.format(count=len(changes)), ""] lines += [f"{ops[c.op]} /{c.path}" for c in changes] + message = draft.message if draft else None + lines += ["", t.message_current.format(message=message) if message else t.message_missing] text = "\n".join(lines) for i, c in enumerate(changes): kb.button( text=t.unstage.format(name=c.path.rsplit("/", 1)[-1]), callback_data=CommitCb(a=Act.UNSTAGE, i=i) ) + if changes: + kb.button(text=t.message_button, callback_data=CommitCb(a=Act.MESSAGE)) + if draft and draft.message: + kb.button(text=t.push_button, callback_data=CommitCb(a=Act.PUSH)) kb.button(text=t.to_dir, callback_data=CommitCb(a=Act.TO_DIR)) kb.adjust(1) return text, kb.as_markup() + def summary(draft: DraftView) -> str: + ops = {ChangeOp.ADD: t.op_add, ChangeOp.MODIFY: t.op_modify, ChangeOp.DELETE: t.op_delete} + return "\n".join(f"{ops[c.op]} /{c.path}" for c in draft.changes) + + async def do_push(msg: Message, state: FSMContext, member: User, *, overwrite: bool) -> None: + draft = await drafts.get(member.tg_id) + if draft is None or not draft.changes or not draft.message: + text, kb = await draft_screen(state, member.tg_id) + await msg.edit_text(text, reply_markup=kb) + return + await msg.edit_text(t.pushing) + result: PushResult = await publisher.push(member, draft, draft.message, overwrite=overwrite) + match result.status: + case PushStatus.OK: + assert result.sha is not None + first_line = draft.message.splitlines()[0] + lines = [t.pushed.format(sha=result.sha[:7], message=first_line)] + if commit_url_template: + lines.append(commit_url_template.format(sha=result.sha)) + lines.append(t.ci_pending) + await msg.edit_text("\n".join(lines)) + await publisher.record(member, result, draft.message, draft, msg.chat.id, msg.message_id) + await drafts.clear(member.tg_id) + await state.update_data(commit_virtual=[]) + case PushStatus.CONFLICT: + builder = InlineKeyboardBuilder() + builder.button(text=t.overwrite, callback_data=CommitCb(a=Act.PUSH_FORCE)) + builder.button(text=texts.menu.cancel, callback_data=CommitCb(a=Act.DRAFT)) + paths = "\n".join(f"/{p}" for p in result.conflicts) + await msg.edit_text(t.conflict.format(paths=paths), reply_markup=builder.as_markup()) + case PushStatus.EMPTY: + await drafts.clear(member.tg_id) + await msg.edit_text(t.push_empty) + case PushStatus.LIMIT: + await msg.edit_text(t.push_limit, reply_markup=to_dir_keyboard(texts)) + case PushStatus.FAILED: + await msg.edit_text(t.push_failed, reply_markup=to_dir_keyboard(texts)) + async def file_card(user_id: int, path: str, size: int, mark: Mark) -> str: card = t.file_card.format(path=path, size=human_size(size)) if mark is Mark.NEW: @@ -411,6 +464,23 @@ def build_router(texts: Texts, repo: UploadsRepo, drafts: DraftService) -> Route base64.b64decode(pending), ) return + case Act.MESSAGE: + await state.set_state(Flow.message) + await msg.edit_text(t.ask_message, reply_markup=to_dir_keyboard(texts)) + case Act.PUSH: + draft = await drafts.get(uid) + if draft is None or not draft.changes or not draft.message: + await query.answer() + return + builder = InlineKeyboardBuilder() + builder.button(text=t.push_go, callback_data=CommitCb(a=Act.PUSH_GO)) + builder.button(text=texts.menu.back, callback_data=CommitCb(a=Act.DRAFT)) + confirm = t.push_confirm.format(summary=summary(draft), message=draft.message) + await msg.edit_text(confirm, reply_markup=builder.as_markup()) + case Act.PUSH_GO | Act.PUSH_FORCE: + await query.answer() + await do_push(msg, state, member, overwrite=callback_data.a is Act.PUSH_FORCE) + return case Act.FIX: await state.set_state(Flow.content) await state.update_data(commit_pending=None) @@ -470,6 +540,18 @@ def build_router(texts: Texts, repo: UploadsRepo, drafts: DraftService) -> Route text, kb = await dir_screen(state, member.tg_id, path, 0) await message.answer(text, reply_markup=kb) + @router.message(Flow.message, F.text) + async def got_message(message: Message, state: FSMContext, member: User) -> None: + try: + text = check_message(message.text or "") + except ValidationError: + await message.answer(t.bad_message) + return + await drafts.set_message(member.tg_id, text) + await state.set_state(None) + screen, kb = await draft_screen(state, member.tg_id) + await message.answer(screen, reply_markup=kb) + @router.message(Flow.content, F.text | F.document) async def got_content(message: Message, state: FSMContext, member: User, bot: Bot) -> None: data = await state.get_data() diff --git a/app/config.py b/app/config.py index 0f0fe45..689f438 100644 --- a/app/config.py +++ b/app/config.py @@ -74,6 +74,8 @@ class GitSettings(BaseSettings): # container user (uid 10001 vs the owner's 0600). Written to a private temp file at startup. ssh_key_b64: SecretStr | None = None branch: str = "main" + # Web link to a commit in the uploads repo, e.g. https://sourcecraft.dev/{repo}/commit/{sha}; empty — no link + commit_url_template: str = "" clone_dir: Path = Path("data/uploads") # Browsing re-fetches at most this often; a push always fetches first (#9) fetch_interval: float = Field(default=10.0, ge=0) diff --git a/app/main.py b/app/main.py index d0a4d49..789e390 100644 --- a/app/main.py +++ b/app/main.py @@ -19,19 +19,24 @@ from app.health import heartbeat from app.logs_setup import setup_logging from app.repo.drafts import DraftService from app.repo.git import UploadsRepo +from app.repo.publisher import Publisher from app.texts import Texts, load_texts log = logging.getLogger("tgit") -def build_dispatcher(texts: Texts, sessions: SessionMaker, access: AccessService, repo: UploadsRepo) -> Dispatcher: +def build_dispatcher( + texts: Texts, sessions: SessionMaker, access: AccessService, repo: UploadsRepo, commit_url_template: str = "" +) -> Dispatcher: dp = Dispatcher(storage=make_storage(sessions)) gate = AccessMiddleware(access, texts) # Outer middleware: runs before filters, so nothing — not even a stale button — reaches a handler unchecked. dp.message.outer_middleware(gate) dp.callback_query.outer_middleware(gate) dp.include_router(members.build_router(texts, access)) - dp.include_router(commit.build_router(texts, repo, DraftService(sessions))) + dp.include_router( + commit.build_router(texts, repo, DraftService(sessions), Publisher(repo, sessions), commit_url_template) + ) dp.include_router(common.build_router(texts, access)) return dp @@ -43,7 +48,7 @@ async def run(settings: Settings) -> None: sessions = make_sessionmaker(engine) access = AccessService(sessions, settings.access) await access.bootstrap_admins() - dp = build_dispatcher(texts, sessions, access, UploadsRepo(settings.git)) + dp = build_dispatcher(texts, sessions, access, UploadsRepo(settings.git), settings.git.commit_url_template) rt = settings.runtime background = [ asyncio.create_task(heartbeat(rt.heartbeat_file, rt.heartbeat_interval)), diff --git a/app/repo/git.py b/app/repo/git.py index 38de956..f1bfeb9 100644 --- a/app/repo/git.py +++ b/app/repo/git.py @@ -52,6 +52,10 @@ class UploadsRepo: self._fetched_at: float | None = None self._key_path = self._materialize_key() + @property + def branch(self) -> str: + return self._s.branch + @property def ref(self) -> str: return f"origin/{self._s.branch}" diff --git a/app/repo/publisher.py b/app/repo/publisher.py new file mode 100644 index 0000000..08dcfc5 --- /dev/null +++ b/app/repo/publisher.py @@ -0,0 +1,171 @@ +"""Turns a member's draft into one commit on the uploads repo's main branch (SPEC C-20…C-25, ARCHITECTURE §2.3). + +Pushes are serialized with a lock: two members pushing at once never fight over the shared working tree. +""" + +import asyncio +import logging +from collections.abc import Awaitable, Callable +from dataclasses import dataclass, field +from datetime import datetime, timedelta +from enum import StrEnum +from pathlib import Path + +from sqlalchemy import func, select + +from app.db.models import ChangeOp, Push, User, utcnow +from app.db.session import SessionMaker +from app.repo.drafts import DraftView +from app.repo.git import GitError, UploadsRepo + +log = logging.getLogger(__name__) + +BOT_NAME = "tgit-bot" +BOT_EMAIL = "tgit-bot@users.noreply.tgit" +PUSHES_PER_HOUR = 20 +PUSH_ATTEMPTS = 3 + + +class PushStatus(StrEnum): + OK = "ok" + CONFLICT = "conflict" # someone changed the same files in main since the draft began + EMPTY = "empty" # the draft matches main already + LIMIT = "limit" + FAILED = "failed" + + +@dataclass(frozen=True) +class PushResult: + status: PushStatus + sha: str | None = None + conflicts: list[str] = field(default_factory=list) + + +class Publisher: + def __init__( + self, + repo: UploadsRepo, + sessions: SessionMaker, + clock: Callable[[], datetime] = utcnow, + before_push: Callable[[], Awaitable[None]] | None = None, # test hook: simulate a concurrent push + ) -> None: + self._repo = repo + self._sessions = sessions + self._clock = clock + self._before_push = before_push + self._lock = asyncio.Lock() + + async def push(self, member: User, draft: DraftView, message: str, *, overwrite: bool = False) -> PushResult: + if await self._pushes_last_hour(member.tg_id) >= PUSHES_PER_HOUR: + return PushResult(PushStatus.LIMIT) + async with self._lock: + try: + return await self._push(member, draft, message, overwrite) + except GitError: + log.exception("push failed") + await self._reset() + return PushResult(PushStatus.FAILED) + + async def record( + self, member: User, result: PushResult, message: str, draft: DraftView, chat_id: int, message_id: int + ) -> None: + assert result.sha is not None + async with self._sessions.begin() as s: + s.add( + Push( + user_id=member.tg_id, + sha=result.sha, + message=message, + paths=[c.path for c in draft.changes], + chat_id=chat_id, + message_id=message_id, + created_at=self._clock(), + ) + ) + + async def _push(self, member: User, draft: DraftView, message: str, overwrite: bool) -> PushResult: + repo = self._repo + await repo.refresh(force=True) + if not overwrite: + conflicts = await self._conflicts(draft) + if conflicts: + return PushResult(PushStatus.CONFLICT, conflicts=conflicts) + + await self._reset() + self._apply(draft) + await repo.git("add", "-A") + try: + await repo.git("diff", "--cached", "--quiet") + return PushResult(PushStatus.EMPTY) # exit 0: nothing staged + except GitError: + pass # exit 1: there are changes + + handle = f"@{member.username}" if member.username else member.full_name + full_message = f"{message.strip()}\n\nTelegram-User: {handle} ({member.tg_id})" + await repo.git( + "-c", f"user.name={BOT_NAME}", "-c", f"user.email={BOT_EMAIL}", "commit", "-q", "-m", full_message + ) + + branch = repo.branch + for attempt in range(1, PUSH_ATTEMPTS + 1): + if self._before_push is not None: + await self._before_push() + try: + await repo.git("push", "-q", "origin", f"HEAD:{branch}") + break + except GitError: + if attempt == PUSH_ATTEMPTS: + raise + log.info("push rejected, rebasing (attempt %d)", attempt) + await repo.git("fetch", "-q", "origin") + try: + await repo.git( + "-c", f"user.name={BOT_NAME}", "-c", f"user.email={BOT_EMAIL}", "rebase", "-q", repo.ref + ) + except GitError: + await repo.git("rebase", "--abort") + return PushResult(PushStatus.CONFLICT, conflicts=await self._conflicts(draft)) + sha = (await repo.git("rev-parse", "HEAD")).decode().strip() + return PushResult(PushStatus.OK, sha=sha) + + async def _conflicts(self, draft: DraftView) -> list[str]: + try: + out = await self._repo.git("diff", "--name-only", draft.base_sha, self._repo.ref) + except GitError: + return [c.path for c in draft.changes] # base unknown (e.g. history rewritten): be safe + changed = set(out.decode().splitlines()) + return [c.path for c in draft.changes if c.path in changed] + + async def _reset(self) -> None: + repo = self._repo + await repo.git("checkout", "-q", "-B", repo.branch, repo.ref) + await repo.git("reset", "-q", "--hard", repo.ref) + await repo.git("clean", "-q", "-fdx") + + def _apply(self, draft: DraftView) -> None: + root = self._repo.path.resolve() + for change in draft.changes: + target = (root / change.path).resolve() + if root not in target.parents: # validated earlier; this is the last line of defence + raise GitError(f"path escapes the repo: {change.path}") + if change.op is ChangeOp.DELETE: + target.unlink(missing_ok=True) + _prune_empty_dirs(target.parent, root) + else: + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(change.content or b"") + + async def _pushes_last_hour(self, user_id: int) -> int: + async with self._sessions() as s: + count = await s.scalar( + select(func.count()) + .select_from(Push) + .where(Push.user_id == user_id, Push.created_at > self._clock() - timedelta(hours=1)) + ) + return count or 0 + + +def _prune_empty_dirs(directory: Path, root: Path) -> None: + while directory != root and directory.is_dir() and not any(directory.iterdir()): + directory.rmdir() + directory = directory.parent diff --git a/app/repo/validate.py b/app/repo/validate.py index e0a7447..9c28f28 100644 --- a/app/repo/validate.py +++ b/app/repo/validate.py @@ -74,3 +74,10 @@ def diff_stat(old: str, new: str) -> tuple[int, int]: elif line.startswith("- "): removed += 1 return added, removed + + +def check_message(message: str) -> str: + message = message.strip() + if not 1 <= len(message) <= 200 or len(message.splitlines()[0]) > 72: + raise ValidationError("message") + return message diff --git a/app/texts.py b/app/texts.py index 53d7f79..803e65a 100644 --- a/app/texts.py +++ b/app/texts.py @@ -104,6 +104,22 @@ class CommitTexts(_Strict): op_add: str op_modify: str op_delete: str + message_button: str + message_current: str + message_missing: str + ask_message: str + bad_message: str + push_button: str + push_confirm: str + push_go: str + pushing: str + pushed: str + ci_pending: str + conflict: str + overwrite: str + push_empty: str + push_limit: str + push_failed: str class Texts(_Strict): diff --git a/config/texts.yaml b/config/texts.yaml index 8dcd4b1..7df9dd1 100644 --- a/config/texts.yaml +++ b/config/texts.yaml @@ -107,3 +107,28 @@ commit: op_add: "A" op_modify: "M" op_delete: "D" + message_button: "💬 Сообщение коммита" + message_current: "💬 {message}" + message_missing: "💬 Сообщение коммита не задано." + ask_message: "Сообщение коммита: до 200 символов, первая строка — до 72." + bad_message: "Сообщение: 1–200 символов, первая строка — до 72. Пришлите другое." + push_button: "🚀 Push" + push_confirm: |- + Запушить в main? + + {summary} + + 💬 {message} + push_go: "✅ Запушить" + pushing: "⏳ Пушу…" + pushed: "✅ Запушено: {sha} — {message}" + ci_pending: "⏳ CI…" + conflict: |- + ⚠️ Пока вы готовили черновик, в main изменились те же файлы: + {paths} + + Перезаписать их вашей версией? + overwrite: "Перезаписать" + push_empty: "В main уже всё так же — коммитить нечего. Черновик очищен." + push_limit: "Лимит — 20 пушей в час. Попробуйте позже." + push_failed: "Не удалось запушить: репозиторий недоступен. Черновик сохранён — попробуйте позже." diff --git a/pyproject.toml b/pyproject.toml index dee183b..dd0ca9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,6 +42,8 @@ extend-exclude = [".claude", "docs"] [tool.ruff.lint] select = ["E", "F", "I", "B", "UP", "SIM", "ASYNC", "RUF"] +# Russian UI texts, commit messages and comments are normal here, not homoglyph attacks +ignore = ["RUF001", "RUF002", "RUF003"] [tool.mypy] python_version = "3.12" diff --git a/tests/bot/test_commit_push.py b/tests/bot/test_commit_push.py new file mode 100644 index 0000000..158e7bc --- /dev/null +++ b/tests/bot/test_commit_push.py @@ -0,0 +1,65 @@ +from pathlib import Path + +import pytest +from aiogram.methods import EditMessageText, SendMessage +from aiogram.types import InlineKeyboardMarkup + +from app.access.service import AccessService +from app.bot.routers.commit import Act, CommitCb +from app.db.session import SessionMaker +from app.main import build_dispatcher +from app.repo.git import UploadsRepo +from app.texts import Texts +from tests.conftest import ADMIN, git +from tests.fakes import TgClient + + +@pytest.fixture +def tg(texts: Texts, sessions: SessionMaker, access: AccessService, repo: UploadsRepo) -> TgClient: + return TgClient(build_dispatcher(texts, sessions, access, repo, "https://example.test/commit/{sha}")) + + +def cb(action: Act, i: int = 0) -> str: + return CommitCb(a=action, i=i).pack() + + +def labels(markup: object) -> list[str]: + assert isinstance(markup, InlineKeyboardMarkup) + return [b.text for row in markup.inline_keyboard for b in row] + + +async def test_message_then_push_lands_in_remote(tg: TgClient, texts: Texts, remote: Path, tmp_path: Path) -> None: + t = texts.commit + await tg.send(ADMIN, texts.menu.commit) + await tg.press(ADMIN, cb(Act.NEW_FILE)) + await tg.send(ADMIN, "notes.txt") + await tg.send(ADMIN, "привет из телеграма") + await tg.press(ADMIN, cb(Act.DRAFT)) + draft = tg.session.sent(EditMessageText)[-1] + assert t.message_missing in draft.text and t.push_button not in labels(draft.reply_markup) + + await tg.press(ADMIN, cb(Act.MESSAGE)) + await tg.send(ADMIN, "x" * 80) # first line too long + assert tg.session.sent(SendMessage)[-1].text == t.bad_message + await tg.send(ADMIN, "docs: заметки") + with_message = tg.session.sent(SendMessage)[-1] + assert t.message_current.format(message="docs: заметки") in with_message.text + assert t.push_button in labels(with_message.reply_markup) + + await tg.press(ADMIN, cb(Act.PUSH)) + assert tg.session.sent(EditMessageText)[-1].text.startswith("Запушить в main?") + await tg.press(ADMIN, cb(Act.PUSH_GO)) + + pushing, done = tg.session.sent(EditMessageText)[-2:] + assert pushing.text == t.pushing + assert done.text.startswith("✅ Запушено: ") and "docs: заметки" in done.text + assert "https://example.test/commit/" in done.text and done.text.endswith(t.ci_pending) + + clone = tmp_path / "verify" + git(tmp_path, "clone", "-q", str(remote), str(clone)) + assert (clone / "notes.txt").read_text(encoding="utf-8") == "привет из телеграма" + + # the draft is gone after a push + tg.session.requests.clear() + await tg.send(ADMIN, texts.menu.commit) + assert not any(label.startswith("🧾") for label in labels(tg.session.sent(SendMessage)[-1].reply_markup)) diff --git a/tests/repo/test_publisher.py b/tests/repo/test_publisher.py new file mode 100644 index 0000000..bdf7e08 --- /dev/null +++ b/tests/repo/test_publisher.py @@ -0,0 +1,143 @@ +from datetime import timedelta +from pathlib import Path + +import pytest +from sqlalchemy import select + +from app.access.service import AccessService +from app.db.models import ChangeOp, Push, User, utcnow +from app.db.session import SessionMaker +from app.repo.drafts import Change, DraftView +from app.repo.git import UploadsRepo +from app.repo.publisher import PUSHES_PER_HOUR, Publisher, PushStatus +from tests.conftest import ADMIN, git + + +@pytest.fixture +async def member(access: AccessService) -> User: + user = await access.get_member(ADMIN) + assert user is not None + return user + + +def remote_log(remote: Path, tmp_path: Path, name: str = "check") -> tuple[Path, str]: + clone = tmp_path / name + git(tmp_path, "clone", "-q", str(remote), str(clone)) + return clone, git(clone, "log", "-1", "--format=%an <%ae>%n%B") + + +def foreign_commit(remote: Path, tmp_path: Path, path: str, text: str, name: str) -> None: + other = tmp_path / name + git(tmp_path, "clone", "-q", str(remote), str(other)) + target = other / path + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(text, encoding="utf-8") + git(other, "add", "-A") + git(other, "commit", "-q", "-m", f"foreign {path}") + git(other, "push", "-q", "origin", "main") + + +async def draft_on(repo: UploadsRepo, *changes: Change) -> DraftView: + await repo.refresh(force=True) + return DraftView(base_sha=await repo.head(), message=None, changes=list(changes)) + + +async def test_push_applies_changes_with_trailer( + repo: UploadsRepo, sessions: SessionMaker, member: User, remote: Path, tmp_path: Path +) -> None: + draft = await draft_on( + repo, + Change("services/hello/utils.py", ChangeOp.ADD, b"X = 1\n"), + Change("services/hello/main.py", ChangeOp.MODIFY, b"print('v2')\n"), + Change("docs/logo.bin", ChangeOp.DELETE, None), + Change("docs/мои заметки.md", ChangeOp.DELETE, None), + ) + publisher = Publisher(repo, sessions) + + result = await publisher.push(member, draft, "feat: v2\n\nподробности") + + assert result.status is PushStatus.OK and result.sha is not None + clone, log = remote_log(remote, tmp_path) + assert log.startswith("tgit-bot \nfeat: v2\n\nподробности\n\nTelegram-User: ") + assert f"({ADMIN})" in log + assert (clone / "services/hello/utils.py").read_text() == "X = 1\n" + assert (clone / "services/hello/main.py").read_text() == "print('v2')\n" + assert not (clone / "docs").exists() # both files gone → the folder is gone too + assert git(clone, "rev-parse", "HEAD").strip() == result.sha + + +async def test_nothing_to_commit(repo: UploadsRepo, sessions: SessionMaker, member: User) -> None: + draft = await draft_on(repo, Change("README.md", ChangeOp.MODIFY, b"# uploads\n")) + + assert (await Publisher(repo, sessions).push(member, draft, "noop")).status is PushStatus.EMPTY + + +async def test_conflict_then_overwrite( + repo: UploadsRepo, sessions: SessionMaker, member: User, remote: Path, tmp_path: Path +) -> None: + draft = await draft_on(repo, Change("README.md", ChangeOp.MODIFY, b"mine\n")) + foreign_commit(remote, tmp_path, "README.md", "theirs\n", "other") + publisher = Publisher(repo, sessions) + + conflict = await publisher.push(member, draft, "edit readme") + assert conflict.status is PushStatus.CONFLICT and conflict.conflicts == ["README.md"] + + forced = await publisher.push(member, draft, "edit readme", overwrite=True) + assert forced.status is PushStatus.OK + clone, _ = remote_log(remote, tmp_path) + assert (clone / "README.md").read_text() == "mine\n" + + +async def test_unrelated_change_in_main_is_not_a_conflict( + repo: UploadsRepo, sessions: SessionMaker, member: User, remote: Path, tmp_path: Path +) -> None: + draft = await draft_on(repo, Change("a.txt", ChangeOp.ADD, b"a")) + foreign_commit(remote, tmp_path, "b.txt", "b", "other") + + assert (await Publisher(repo, sessions).push(member, draft, "add a")).status is PushStatus.OK + clone, _ = remote_log(remote, tmp_path) + assert (clone / "a.txt").exists() and (clone / "b.txt").exists() + + +async def test_rejected_push_is_rebased( + repo: UploadsRepo, sessions: SessionMaker, member: User, remote: Path, tmp_path: Path +) -> None: + raced = False + + async def someone_pushes_first() -> None: + nonlocal raced + if not raced: + raced = True + foreign_commit(remote, tmp_path, "race.txt", "r", "racer") + + draft = await draft_on(repo, Change("mine.txt", ChangeOp.ADD, b"m")) + result = await Publisher(repo, sessions, before_push=someone_pushes_first).push(member, draft, "add mine") + + assert result.status is PushStatus.OK and raced + clone, log = remote_log(remote, tmp_path) + assert "add mine" in log and (clone / "race.txt").exists() and (clone / "mine.txt").exists() + + +async def test_path_escape_is_refused(repo: UploadsRepo, sessions: SessionMaker, member: User, tmp_path: Path) -> None: + draft = await draft_on(repo, Change("../evil.txt", ChangeOp.ADD, b"x")) + + assert (await Publisher(repo, sessions).push(member, draft, "evil")).status is PushStatus.FAILED + assert not (tmp_path / "evil.txt").exists() + + +async def test_rate_limit_and_record(repo: UploadsRepo, sessions: SessionMaker, member: User) -> None: + publisher = Publisher(repo, sessions) + draft = await draft_on(repo, Change("x.txt", ChangeOp.ADD, b"x")) + result = await publisher.push(member, draft, "x") + await publisher.record(member, result, "x", draft, chat_id=ADMIN, message_id=77) + + async with sessions() as s: + push = await s.scalar(select(Push)) + assert push is not None and push.sha == result.sha and push.paths == ["x.txt"] and push.message_id == 77 + for i in range(PUSHES_PER_HOUR): + s.add( + Push(user_id=ADMIN, sha=f"{i:040d}", message="m", chat_id=1, created_at=utcnow() - timedelta(minutes=1)) + ) + await s.commit() + + assert (await publisher.push(member, draft, "y")).status is PushStatus.LIMIT