diff --git a/app/bot/routers/commit.py b/app/bot/routers/commit.py index 7864a17..965bd5e 100644 --- a/app/bot/routers/commit.py +++ b/app/bot/routers/commit.py @@ -1,20 +1,37 @@ -"""«📝 Коммит»: browsing the uploads repo tree with buttons (SPEC C-1…C-4). Changes and push — #26, #9.""" +"""«📝 Коммит»: browse the uploads repo tree and collect changes into a draft (SPEC C-1…C-16). Push — #9.""" +import base64 import html +import io import logging +from dataclasses import dataclass from enum import StrEnum from typing import Any -from aiogram import F, Router +from aiogram import Bot, F, Router from aiogram.enums import ParseMode from aiogram.filters.callback_data import CallbackData from aiogram.fsm.context import FSMContext +from aiogram.fsm.state import State, StatesGroup from aiogram.types import BufferedInputFile, CallbackQuery, InlineKeyboardMarkup, Message from aiogram.utils.keyboard import InlineKeyboardBuilder 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.texts import Texts +from app.repo.validate import ( + MAX_FILE_BYTES, + ValidationError, + check_content, + check_name, + diff_stat, + is_protected, + join, + python_syntax, + strip_fence, +) +from app.texts import CommitTexts, Texts log = logging.getLogger(__name__) @@ -32,6 +49,10 @@ class Act(StrEnum): DELETE = "d" NEW_FILE = "f" NEW_DIR = "n" + DRAFT = "dr" + UNSTAGE = "x" + FORCE = "y" + FIX = "z" class CommitCb(CallbackData, prefix="c"): @@ -40,6 +61,34 @@ class CommitCb(CallbackData, prefix="c"): i: int = 0 +class Flow(StatesGroup): + file_name = State() + dir_name = State() + content = State() + syntax = State() + + +class Mark(StrEnum): + NONE = "" + NEW = "new" + MODIFIED = "mod" + DELETED = "del" + + +@dataclass(frozen=True) +class Row: + """A line of the directory screen: a repo entry with the draft laid over it.""" + + path: str + kind: str + size: int + mark: Mark + + @property + def name(self) -> str: + return self.path.rsplit("/", 1)[-1] + + def human_size(n: int) -> str: size = float(n) for unit in ("Б", "КБ", "МБ"): @@ -53,26 +102,61 @@ def parent(path: str) -> str: return path.rsplit("/", 1)[0] if "/" in path else "" -def render_dir(texts: Texts, path: str, entries: list[Entry], page: int) -> tuple[str, InlineKeyboardMarkup]: +def overlay(path: str, entries: list[Entry], draft: DraftView | None, virtual_dirs: list[str]) -> list[Row]: + """Repo listing + what the draft adds, changes or deletes in this directory.""" + changes = {c.path: c for c in draft.changes} if draft else {} + rows: dict[str, Row] = {} + for e in entries: + change = changes.get(e.path) + mark, size = Mark.NONE, e.size + if change is not None: + mark = Mark.DELETED if change.op is ChangeOp.DELETE else Mark.MODIFIED + if change.content is not None: + size = len(change.content) # show the size the file will have after the push + rows[e.path] = Row(e.path, e.kind, size, mark) + prefix = f"{path}/" if path else "" + for change in changes.values(): + if change.op is not ChangeOp.ADD or not change.path.startswith(prefix): + continue + rest = change.path[len(prefix) :] + if "/" in rest: # a file deeper down: show its top folder here + sub = prefix + rest.split("/", 1)[0] + rows.setdefault(sub, Row(sub, "dir", 0, Mark.NEW)) + else: + rows[change.path] = Row(change.path, "file", len(change.content or b""), Mark.NEW) + for vd in virtual_dirs: + if parent(vd) == path: + rows.setdefault(vd, Row(vd, "dir", 0, Mark.NEW)) + return sorted(rows.values(), key=lambda r: (r.kind != "dir", r.name.lower())) + + +def mark_prefix(t: CommitTexts, mark: Mark) -> str: + return {Mark.NONE: "", Mark.NEW: t.mark_new, Mark.MODIFIED: t.mark_modified, Mark.DELETED: t.mark_deleted}[mark] + + +def render_dir( + texts: Texts, path: str, rows: list[Row], page: int, draft_count: int +) -> tuple[str, InlineKeyboardMarkup]: t = texts.commit - pages = max(1, -(-len(entries) // PAGE_SIZE)) + pages = max(1, -(-len(rows) // PAGE_SIZE)) page = min(max(page, 0), pages - 1) lines = [t.title.format(path=path)] - if not entries: + if not rows: lines.append(t.empty) elif pages > 1: lines.append(t.page.format(page=page + 1, pages=pages)) kb = InlineKeyboardBuilder() start = page * PAGE_SIZE - for index, e in enumerate(entries[start : start + PAGE_SIZE], start=start): - label = ( - t.dir_button.format(name=e.name) - if e.kind == "dir" - else t.file_button.format(name=e.name, size=human_size(e.size)) + shown = rows[start : start + PAGE_SIZE] + for index, r in enumerate(shown, start=start): + base = ( + t.dir_button.format(name=r.name) + if r.kind == "dir" + else t.file_button.format(name=r.name, size=human_size(r.size)) ) - kb.button(text=label, callback_data=CommitCb(a=Act.OPEN, i=index)) - sizes = [1] * min(PAGE_SIZE, max(0, len(entries) - start)) + kb.button(text=mark_prefix(t, r.mark) + base, callback_data=CommitCb(a=Act.OPEN, i=index)) + sizes = [1] * len(shown) paging: list[tuple[str, CommitCb]] = [] if page > 0: @@ -87,6 +171,9 @@ def render_dir(texts: Texts, path: str, entries: list[Entry], page: int) -> tupl kb.button(text=t.new_file, callback_data=CommitCb(a=Act.NEW_FILE)) kb.button(text=t.new_dir, callback_data=CommitCb(a=Act.NEW_DIR)) sizes.append(2) + if draft_count: + kb.button(text=t.draft_button.format(count=draft_count), callback_data=CommitCb(a=Act.DRAFT)) + sizes.append(1) if path: kb.button(text=t.up, callback_data=CommitCb(a=Act.UP)) sizes.append(1) @@ -108,80 +195,60 @@ def file_keyboard(texts: Texts) -> InlineKeyboardMarkup: return kb.as_markup() -def build_router(texts: Texts, repo: UploadsRepo) -> Router: +def to_dir_keyboard(texts: Texts) -> InlineKeyboardMarkup: + kb = InlineKeyboardBuilder() + kb.button(text=texts.commit.to_dir, callback_data=CommitCb(a=Act.TO_DIR)) + return kb.as_markup() + + +def build_router(texts: Texts, repo: UploadsRepo, drafts: DraftService) -> Router: router = Router(name="commit") t = texts.commit - async def show_dir(message: Message, state: FSMContext, path: str, page: int, *, edit: bool) -> None: - entries = await repo.list_dir(path) + async def dir_screen(state: FSMContext, user_id: int, path: str, page: int) -> tuple[str, InlineKeyboardMarkup]: + data = await state.get_data() + virtual: list[str] = data.get("commit_virtual", []) + draft = await drafts.get(user_id) + try: + entries = await repo.list_dir(path) + except GitError: + # a folder that exists only in the draft (or a virtual one) has nothing in the repo yet + if path not in virtual and not (draft and any(c.path.startswith(f"{path}/") for c in draft.changes)): + raise + entries = [] + rows = overlay(path, entries, draft, virtual) await state.update_data( commit_path=path, commit_page=page, - commit_entries=[[e.path, e.kind, e.size] for e in entries], + commit_rows=[[r.path, r.kind, r.size, r.mark.value] for r in rows], commit_file=None, ) - text, kb = render_dir(texts, path, entries, page) - if edit: - await message.edit_text(text, reply_markup=kb) + return render_dir(texts, path, rows, page, len(draft.changes) if draft else 0) + + async def draft_screen(state: FSMContext, user_id: int) -> tuple[str, InlineKeyboardMarkup]: + draft = await drafts.get(user_id) + changes = draft.changes if draft else [] + await state.update_data(commit_draft=[c.path for c in changes]) + kb = InlineKeyboardBuilder() + if not changes: + text = t.draft_empty else: - await message.answer(text, reply_markup=kb) + 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] + 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) + ) + kb.button(text=t.to_dir, callback_data=CommitCb(a=Act.TO_DIR)) + kb.adjust(1) + return text, kb.as_markup() - @router.message(F.text == texts.menu.commit) - async def open_commit(message: Message, state: FSMContext) -> None: - await state.clear() - try: - await repo.refresh() - await show_dir(message, state, "", 0, edit=False) - except GitError: - log.exception("uploads repo unavailable") - await message.answer(t.unavailable) - - @router.callback_query(CommitCb.filter()) - async def on_action(query: CallbackQuery, callback_data: CommitCb, state: FSMContext) -> None: - if not isinstance(query.message, Message): - await query.answer() - return - msg = query.message - data: dict[str, Any] = await state.get_data() - path: str = data.get("commit_path", "") - entries: list[list[Any]] = data.get("commit_entries", []) - try: - match callback_data.a: - case Act.OPEN: - if not 0 <= callback_data.i < len(entries): - await query.answer() # stale button from an older screen - return - target, kind, size = entries[callback_data.i] - if kind == "dir": - await repo.refresh() - await show_dir(msg, state, target, 0, edit=True) - else: - await state.update_data(commit_file=target) - await msg.edit_text(await file_card(target, size), reply_markup=file_keyboard(texts)) - case Act.UP: - await show_dir(msg, state, parent(path), 0, edit=True) - case Act.PAGE: - await state.update_data(commit_page=callback_data.i) - real = [Entry(p, k, s) for p, k, s in entries] - text, kb = render_dir(texts, path, real, callback_data.i) - await msg.edit_text(text, reply_markup=kb) - case Act.TO_DIR: - await show_dir(msg, state, path, data.get("commit_page", 0), edit=True) - case Act.VIEW: - file_path = data.get("commit_file") - if file_path: - await send_file(msg, file_path) - case Act.REPLACE | Act.DELETE | Act.NEW_FILE | Act.NEW_DIR: - await query.answer(t.soon, show_alert=True) - return - except GitError: - log.exception("uploads repo unavailable") - await query.answer(t.unavailable, show_alert=True) - return - await query.answer() - - async def file_card(path: str, size: int) -> str: + 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: + return f"{card}\n{t.in_draft_new}" info = await repo.last_commit(path) tail = ( t.last_commit.format( @@ -190,11 +257,21 @@ def build_router(texts: Texts, repo: UploadsRepo) -> Router: if info else t.no_commits ) - return f"{card}\n{tail}" + note = {Mark.MODIFIED: f"\n{t.in_draft_modified}", Mark.DELETED: f"\n{t.in_draft_deleted}"}.get(mark, "") + return f"{card}\n{tail}{note}" - async def send_file(msg: Message, path: str) -> None: - content = await repo.read(path) - name = path.rsplit("/", 1)[-1] + async def current_content(user_id: int, path: str) -> bytes | None: + """What the member sees as the file now: the draft version if there is one.""" + draft = await drafts.get(user_id) + change = draft.change_for(path) if draft else None + if change is not None: + return change.content # None for a deleted file + return await repo.read(path) + + async def send_file(msg: Message, user_id: int, path: str) -> None: + content = await current_content(user_id, path) + if content is None: + return try: text = content.decode("utf-8") except UnicodeDecodeError: @@ -202,6 +279,224 @@ def build_router(texts: Texts, repo: UploadsRepo) -> Router: if text is not None and "\0" not in text and len(text) <= INLINE_VIEW_LIMIT: await msg.answer(f"/{html.escape(path)}\n
{html.escape(text)}
", parse_mode=ParseMode.HTML) else: - await msg.answer_document(BufferedInputFile(content, name), caption=t.sent_as_file) + await msg.answer_document(BufferedInputFile(content, path.rsplit("/", 1)[-1]), caption=t.sent_as_file) + + async def stage(message: Message, state: FSMContext, member: User, path: str, op: ChangeOp, content: bytes) -> None: + old = await current_content(member.tg_id, path) if op is ChangeOp.MODIFY else None + try: + await drafts.put(member.tg_id, await repo.head(), path, op, content) + except DraftFull: + await message.answer(t.draft_full) + return + if op is ChangeOp.ADD: + note = t.added.format(path=path) + else: + added, removed = diff_stat((old or b"").decode("utf-8", errors="replace"), content.decode("utf-8")) + note = t.modified.format(path=path, added=added, removed=removed) + data = await state.get_data() + # a virtual folder becomes real as soon as a file lands in it + virtual = [v for v in data.get("commit_virtual", []) if not path.startswith(f"{v}/")] + await state.set_state(None) + await state.update_data(commit_virtual=virtual, commit_pending=None) + text, kb = await dir_screen(state, member.tg_id, data.get("commit_path", ""), 0) + await message.answer(f"{note}\n\n{text}", reply_markup=kb) + + # ---- entry point ------------------------------------------------------------------------------------------- + + @router.message(F.text == texts.menu.commit) + async def open_commit(message: Message, state: FSMContext, member: User) -> None: + await state.clear() + try: + await repo.refresh() + text, kb = await dir_screen(state, member.tg_id, "", 0) + except GitError: + log.exception("uploads repo unavailable") + await message.answer(t.unavailable) + return + await message.answer(text, reply_markup=kb) + + # ---- buttons ----------------------------------------------------------------------------------------------- + + @router.callback_query(CommitCb.filter()) + async def on_action(query: CallbackQuery, callback_data: CommitCb, state: FSMContext, member: User) -> None: + if not isinstance(query.message, Message): + await query.answer() + return + msg = query.message + data: dict[str, Any] = await state.get_data() + path: str = data.get("commit_path", "") + rows: list[list[Any]] = data.get("commit_rows", []) + uid = member.tg_id + try: + match callback_data.a: + case Act.OPEN: + if not 0 <= callback_data.i < len(rows): + await query.answer() # stale button from an older screen + return + target, kind, size, mark = rows[callback_data.i] + if kind == "dir": + await repo.refresh() + text, kb = await dir_screen(state, uid, target, 0) + await msg.edit_text(text, reply_markup=kb) + else: + await state.update_data(commit_file=target) + await msg.edit_text( + await file_card(uid, target, size, Mark(mark)), reply_markup=file_keyboard(texts) + ) + case Act.UP: + text, kb = await dir_screen(state, uid, parent(path), 0) + await msg.edit_text(text, reply_markup=kb) + case Act.PAGE: + real = [Row(p, k, s, Mark(m)) for p, k, s, m in rows] + draft = await drafts.get(uid) + await state.update_data(commit_page=callback_data.i) + text, kb = render_dir(texts, path, real, callback_data.i, len(draft.changes) if draft else 0) + await msg.edit_text(text, reply_markup=kb) + case Act.TO_DIR: + await state.set_state(None) + text, kb = await dir_screen(state, uid, path, data.get("commit_page", 0)) + await msg.edit_text(text, reply_markup=kb) + case Act.VIEW: + if data.get("commit_file"): + await send_file(msg, uid, data["commit_file"]) + case Act.NEW_FILE: + await state.set_state(Flow.file_name) + await msg.edit_text(t.ask_file_name.format(dir=path), reply_markup=to_dir_keyboard(texts)) + case Act.NEW_DIR: + await state.set_state(Flow.dir_name) + await msg.edit_text(t.ask_dir_name.format(dir=path), reply_markup=to_dir_keyboard(texts)) + case Act.REPLACE: + target = data.get("commit_file") + if not target: + await query.answer() + return + if is_protected(target): + await query.answer(t.protected, show_alert=True) + return + await state.set_state(Flow.content) + await state.update_data(commit_target=target, commit_op=ChangeOp.MODIFY.value) + await msg.edit_text(t.ask_content.format(path=target), reply_markup=to_dir_keyboard(texts)) + case Act.DELETE: + target = data.get("commit_file") + if not target: + await query.answer() + return + if is_protected(target): + await query.answer(t.protected, show_alert=True) + return + await drafts.put(uid, await repo.head(), target, ChangeOp.DELETE, None) + text, kb = await dir_screen(state, uid, path, data.get("commit_page", 0)) + await msg.edit_text(f"{t.deleted.format(path=target)}\n\n{text}", reply_markup=kb) + case Act.DRAFT: + text, kb = await draft_screen(state, uid) + await msg.edit_text(text, reply_markup=kb) + case Act.UNSTAGE: + paths: list[str] = data.get("commit_draft", []) + if 0 <= callback_data.i < len(paths): + await drafts.drop(uid, paths[callback_data.i]) + text, kb = await draft_screen(state, uid) + await msg.edit_text(text, reply_markup=kb) + case Act.FORCE: + pending = data.get("commit_pending") + if pending is None or await state.get_state() != Flow.syntax.state: + await query.answer() + return + await query.answer() + await stage( + msg, + state, + member, + data["commit_target"], + ChangeOp(data["commit_op"]), + base64.b64decode(pending), + ) + return + case Act.FIX: + await state.set_state(Flow.content) + await state.update_data(commit_pending=None) + await msg.edit_text( + t.ask_content.format(path=data.get("commit_target", "")), reply_markup=to_dir_keyboard(texts) + ) + except GitError: + log.exception("uploads repo unavailable") + await query.answer(t.unavailable, show_alert=True) + return + except DraftFull: + await query.answer(t.draft_full, show_alert=True) + return + await query.answer() + + # ---- text input -------------------------------------------------------------------------------------------- + + @router.message(Flow.file_name, F.text) + async def got_file_name(message: Message, state: FSMContext, member: User) -> None: + data = await state.get_data() + directory = data.get("commit_path", "") + try: + name = check_name(message.text or "") + except ValidationError: + await message.answer(t.bad_name) + return + path = join(directory, name) + if is_protected(path): + await message.answer(t.protected) + return + taken = {row[0] for row in data.get("commit_rows", [])} + if path in taken: + await message.answer(t.exists) + return + await state.set_state(Flow.content) + await state.update_data(commit_target=path, commit_op=ChangeOp.ADD.value) + await message.answer(t.ask_content.format(path=path), reply_markup=to_dir_keyboard(texts)) + + @router.message(Flow.dir_name, F.text) + async def got_dir_name(message: Message, state: FSMContext, member: User) -> None: + data = await state.get_data() + directory = data.get("commit_path", "") + try: + name = check_name(message.text or "") + except ValidationError: + await message.answer(t.bad_name) + return + path = join(directory, name) + if is_protected(path + "/x"): + await message.answer(t.protected) + return + if path in {row[0] for row in data.get("commit_rows", [])}: + await message.answer(t.exists) + return + await state.set_state(None) + await state.update_data(commit_virtual=[*data.get("commit_virtual", []), path]) + text, kb = await dir_screen(state, member.tg_id, path, 0) + await message.answer(text, 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() + target: str = data["commit_target"] + if message.document is not None: + if (message.document.file_size or 0) > MAX_FILE_BYTES: + await message.answer(t.too_big) + return + buf = io.BytesIO() + await bot.download(message.document, destination=buf) + raw = buf.getvalue() + else: + raw = strip_fence(message.text or "").encode("utf-8") + try: + text = check_content(raw) + except ValidationError as e: + await message.answer({"too_big": t.too_big, "binary": t.binary, "encoding": t.encoding}[str(e)]) + return + issue = python_syntax(target, text) + if issue is not None: + await state.set_state(Flow.syntax) + await state.update_data(commit_pending=base64.b64encode(raw).decode()) + kb = InlineKeyboardBuilder() + kb.button(text=t.fix, callback_data=CommitCb(a=Act.FIX)) + kb.button(text=t.force, callback_data=CommitCb(a=Act.FORCE)) + await message.answer(t.syntax.format(line=issue.line, message=issue.message), reply_markup=kb.as_markup()) + return + await stage(message, state, member, target, ChangeOp(data["commit_op"]), raw) return router diff --git a/app/main.py b/app/main.py index 1be8a8e..d0a4d49 100644 --- a/app/main.py +++ b/app/main.py @@ -17,6 +17,7 @@ from app.db.migrate import upgrade from app.db.session import SessionMaker, make_engine, make_sessionmaker 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.texts import Texts, load_texts @@ -30,7 +31,7 @@ def build_dispatcher(texts: Texts, sessions: SessionMaker, access: AccessService 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)) + dp.include_router(commit.build_router(texts, repo, DraftService(sessions))) dp.include_router(common.build_router(texts, access)) return dp diff --git a/app/repo/drafts.py b/app/repo/drafts.py new file mode 100644 index 0000000..81ca43e --- /dev/null +++ b/app/repo/drafts.py @@ -0,0 +1,107 @@ +"""A member's draft: pending add/modify/delete changes until Push (SPEC C-14, C-15). One draft per member.""" + +from collections.abc import Callable +from dataclasses import dataclass +from datetime import UTC, datetime, timedelta + +from sqlalchemy import delete, select +from sqlalchemy.ext.asyncio import AsyncSession +from sqlalchemy.orm import selectinload + +from app.db.models import ChangeOp, Draft, DraftChange, utcnow +from app.db.session import SessionMaker + +MAX_CHANGES = 50 + + +class DraftFull(Exception): + pass + + +@dataclass(frozen=True) +class Change: + path: str + op: ChangeOp + content: bytes | None + + +@dataclass(frozen=True) +class DraftView: + base_sha: str + message: str | None + changes: list[Change] + + def change_for(self, path: str) -> Change | None: + return next((c for c in self.changes if c.path == path), None) + + +class DraftService: + def __init__( + self, sessions: SessionMaker, ttl: timedelta = timedelta(hours=24), clock: Callable[[], datetime] = utcnow + ): + self._sessions = sessions + self._ttl = ttl + self._clock = clock + + async def get(self, user_id: int) -> DraftView | None: + async with self._sessions() as s: + draft = await self._load(s, user_id) + if draft is None: + return None + return DraftView( + base_sha=draft.base_sha, + message=draft.message, + changes=[Change(c.path, c.op, c.content) for c in draft.changes], + ) + + async def put(self, user_id: int, base_sha: str, path: str, op: ChangeOp, content: bytes | None) -> None: + """Record a change. Deleting a file that only exists in the draft just drops it from the draft.""" + async with self._sessions.begin() as s: + draft = await self._load(s, user_id) + if draft is None: + draft = Draft( + user_id=user_id, base_sha=base_sha, created_at=self._clock(), expires_at=self._clock() + self._ttl + ) + s.add(draft) + existing = next((c for c in draft.changes if c.path == path), None) + if existing is not None: + if op is ChangeOp.DELETE and existing.op is ChangeOp.ADD: + draft.changes.remove(existing) + return + # editing a file added in this draft keeps it an "add" + existing.op = ChangeOp.ADD if existing.op is ChangeOp.ADD and op is ChangeOp.MODIFY else op + existing.content = content + else: + if len(draft.changes) >= MAX_CHANGES: + raise DraftFull + draft.changes.append(DraftChange(path=path, op=op, content=content)) + draft.expires_at = self._clock() + self._ttl + + async def drop(self, user_id: int, path: str) -> None: + async with self._sessions.begin() as s: + draft = await self._load(s, user_id) + if draft is None: + return + draft.changes = [c for c in draft.changes if c.path != path] + + async def set_message(self, user_id: int, message: str) -> None: + async with self._sessions.begin() as s: + draft = await self._load(s, user_id) + if draft is not None: + draft.message = message + + async def clear(self, user_id: int) -> None: + async with self._sessions.begin() as s: + await s.execute(delete(Draft).where(Draft.user_id == user_id)) + + async def _load(self, s: AsyncSession, user_id: int) -> Draft | None: + draft = await s.scalar(select(Draft).where(Draft.user_id == user_id).options(selectinload(Draft.changes))) + if draft is not None and _aware(draft.expires_at) <= self._clock(): + await s.delete(draft) + await s.flush() + return None + return draft + + +def _aware(value: datetime) -> datetime: + return value if value.tzinfo is not None else value.replace(tzinfo=UTC) diff --git a/app/repo/validate.py b/app/repo/validate.py new file mode 100644 index 0000000..e0a7447 --- /dev/null +++ b/app/repo/validate.py @@ -0,0 +1,76 @@ +"""Checks for names and file contents before anything goes into a draft (SPEC C-10, C-12, C-16). No code runs here.""" + +import ast +import difflib +import re +from dataclasses import dataclass + +MAX_FILE_BYTES = 100 * 1024 +NAME = re.compile(r"^[A-Za-z0-9._-]{1,100}$") +# Bot users can't touch the CI of the uploads repo or its helper scripts (SPEC C-16, ADR-014) +PROTECTED_PREFIXES = (".sourcecraft/", ".tgit/", ".git/") +PROTECTED_FILES = {"ruff.toml"} +_FENCE = re.compile(r"^\s*```[\w+-]*[ \t]*\n(.*?)\n?```\s*$", re.DOTALL) + + +class ValidationError(ValueError): + pass + + +@dataclass(frozen=True) +class SyntaxIssue: + line: int + message: str + + +def check_name(name: str) -> str: + name = name.strip() + if not NAME.match(name) or name in {".", ".."}: + raise ValidationError("name") + return name + + +def is_protected(path: str) -> bool: + return path in PROTECTED_FILES or any(path.startswith(p) or path + "/" == p for p in PROTECTED_PREFIXES) + + +def join(directory: str, name: str) -> str: + return f"{directory}/{name}" if directory else name + + +def strip_fence(text: str) -> str: + """Code pasted as a Telegram message often comes wrapped in ``` — keep only what's inside.""" + match = _FENCE.match(text) + return match.group(1) if match else text + + +def check_content(data: bytes) -> str: + """Size, encoding, NUL bytes. Returns the decoded text.""" + if len(data) > MAX_FILE_BYTES: + raise ValidationError("too_big") + if b"\0" in data: + raise ValidationError("binary") + try: + return data.decode("utf-8") + except UnicodeDecodeError: + raise ValidationError("encoding") from None + + +def python_syntax(path: str, text: str) -> SyntaxIssue | None: + if not path.endswith(".py"): + return None + try: + ast.parse(text, filename=path) + except SyntaxError as e: + return SyntaxIssue(line=e.lineno or 0, message=e.msg) + return None + + +def diff_stat(old: str, new: str) -> tuple[int, int]: + added = removed = 0 + for line in difflib.ndiff(old.splitlines(), new.splitlines()): + if line.startswith("+ "): + added += 1 + elif line.startswith("- "): + removed += 1 + return added, removed diff --git a/app/texts.py b/app/texts.py index e1c0724..53d7f79 100644 --- a/app/texts.py +++ b/app/texts.py @@ -75,6 +75,35 @@ class CommitTexts(_Strict): soon: str unavailable: str sent_as_file: str + ask_file_name: str + ask_dir_name: str + bad_name: str + exists: str + protected: str + ask_content: str + too_big: str + binary: str + encoding: str + syntax: str + fix: str + force: str + added: str + modified: str + deleted: str + draft_button: str + draft_title: str + draft_empty: str + unstage: str + draft_full: str + in_draft_new: str + in_draft_modified: str + in_draft_deleted: str + mark_new: str + mark_modified: str + mark_deleted: str + op_add: str + op_modify: str + op_delete: str class Texts(_Strict): diff --git a/config/texts.yaml b/config/texts.yaml index 9ff6a6b..8dcd4b1 100644 --- a/config/texts.yaml +++ b/config/texts.yaml @@ -78,3 +78,32 @@ commit: soon: "Будет в следующей версии." unavailable: "Репозиторий загрузок недоступен, попробуйте через минуту." sent_as_file: "Файл большой или бинарный — отправил документом." + ask_file_name: "Имя нового файла в /{dir} (латиница, цифры, «.», «_», «-»):" + ask_dir_name: "Имя новой папки в /{dir}:" + bad_name: "Недопустимое имя: латиница, цифры, «.», «_», «-», до 100 символов. Пришлите другое." + exists: "Такое имя уже есть в этой папке. Пришлите другое." + protected: "Это служебный путь репозитория — из бота его менять нельзя." + ask_content: "Пришлите содержимое /{path} — текстом сообщения или файлом (до 100 КБ)." + too_big: "Больше 100 КБ — не принимаю." + binary: "Похоже на бинарный файл — принимаю только текст." + encoding: "Файл не в кодировке UTF-8." + syntax: "⚠️ Синтаксическая ошибка, строка {line}: {message}" + fix: "✏️ Исправить" + force: "Всё равно добавить" + added: "🆕 /{path} — добавлен в черновик." + modified: "✏️ /{path} — в черновике: +{added} −{removed}." + deleted: "🗑 /{path} — в черновике на удаление." + draft_button: "🧾 Черновик · {count}" + draft_title: "🧾 Черновик · изменений: {count}" + draft_empty: "Черновик пуст." + unstage: "↩️ убрать {name}" + draft_full: "В черновике уже 50 изменений — запушьте или уберите лишнее." + in_draft_new: "В черновике: новый файл." + in_draft_modified: "В черновике: изменён." + in_draft_deleted: "В черновике: удалён." + mark_new: "🆕 " + mark_modified: "✏️ " + mark_deleted: "🗑 " + op_add: "A" + op_modify: "M" + op_delete: "D" diff --git a/tests/bot/test_commit.py b/tests/bot/test_commit.py index 43f8096..0e04cc4 100644 --- a/tests/bot/test_commit.py +++ b/tests/bot/test_commit.py @@ -87,13 +87,11 @@ async def test_pagination(tg: TgClient, texts: Texts) -> None: assert labels(second.reply_markup)[0].startswith("📄 f20.txt") and texts.commit.prev in labels(second.reply_markup) -async def test_unfinished_actions_and_stale_buttons(tg: TgClient, texts: Texts) -> None: +async def test_stale_button_is_ignored(tg: TgClient, texts: Texts) -> None: await tg.send(ADMIN, texts.menu.commit) - await tg.press(ADMIN, cb(Act.NEW_FILE)) await tg.press(ADMIN, cb(Act.OPEN, 99)) - soon, stale = tg.session.sent(AnswerCallbackQuery) - assert soon.text == texts.commit.soon and soon.show_alert + [stale] = tg.session.sent(AnswerCallbackQuery) assert stale.text is None assert tg.session.sent(EditMessageText) == [] diff --git a/tests/bot/test_commit_edit.py b/tests/bot/test_commit_edit.py new file mode 100644 index 0000000..47601f1 --- /dev/null +++ b/tests/bot/test_commit_edit.py @@ -0,0 +1,162 @@ +import pytest +from aiogram.methods import AnswerCallbackQuery, 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 +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)) + + +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] + + +def last_text(tg: TgClient) -> SendMessage: + return tg.session.sent(SendMessage)[-1] + + +async def to_hello(tg: TgClient, texts: Texts) -> None: + await tg.send(ADMIN, texts.menu.commit) + await tg.press(ADMIN, cb(Act.OPEN, 2)) # services + await tg.press(ADMIN, cb(Act.OPEN, 0)) # services/hello + + +async def test_new_file_from_fenced_text(tg: TgClient, texts: Texts) -> None: + await to_hello(tg, texts) + await tg.press(ADMIN, cb(Act.NEW_FILE)) + await tg.send(ADMIN, "utils.py") + await tg.send(ADMIN, "```python\ndef add(a, b):\n return a + b\n```") + + reply = last_text(tg) + assert reply.text.startswith(texts.commit.added.format(path="services/hello/utils.py")) + got = labels(reply.reply_markup) + assert "🆕 📄 utils.py · 31 Б" in got # fence stripped, no trailing newline + assert texts.commit.draft_button.format(count=1) in got + + await tg.press(ADMIN, cb(Act.OPEN, 1)) # utils.py (main.py is 0) + await tg.press(ADMIN, cb(Act.VIEW)) + view = last_text(tg) + assert "def add(a, b):" in view.text and "```" not in view.text + + +async def test_replace_with_document_shows_diff(tg: TgClient, texts: Texts) -> None: + await to_hello(tg, texts) + await tg.press(ADMIN, cb(Act.OPEN, 0)) # main.py + await tg.press(ADMIN, cb(Act.REPLACE)) + await tg.send_document(ADMIN, "main.py", b"print('hello')\nprint('bye')\n") + + reply = last_text(tg) + assert reply.text.startswith(texts.commit.modified.format(path="services/hello/main.py", added=1, removed=0)) + assert "✏️ 📄 main.py · 28 Б" in labels(reply.reply_markup) + + +async def test_delete_marks_and_draft_lists(tg: TgClient, texts: Texts) -> None: + await tg.send(ADMIN, texts.menu.commit) + await tg.press(ADMIN, cb(Act.OPEN, 3)) # README.md + await tg.press(ADMIN, cb(Act.DELETE)) + deleted = tg.session.sent(EditMessageText)[-1] + assert deleted.text.startswith(texts.commit.deleted.format(path="README.md")) + assert "🗑 📄 README.md · 10 Б" in labels(deleted.reply_markup) + + await tg.press(ADMIN, cb(Act.DRAFT)) + draft = tg.session.sent(EditMessageText)[-1] + assert "D /README.md" in draft.text + + await tg.press(ADMIN, cb(Act.UNSTAGE, 0)) + assert tg.session.sent(EditMessageText)[-1].text == texts.commit.draft_empty + + +async def test_syntax_error_force_or_fix(tg: TgClient, texts: Texts) -> None: + await to_hello(tg, texts) + await tg.press(ADMIN, cb(Act.NEW_FILE)) + await tg.send(ADMIN, "bad.py") + await tg.send(ADMIN, "def f(:\n pass") + + warn = last_text(tg) + assert warn.text.startswith("⚠️") and "1" in warn.text + assert labels(warn.reply_markup) == [texts.commit.fix, texts.commit.force] + + await tg.press(ADMIN, cb(Act.FIX)) + assert tg.session.sent(EditMessageText)[-1].text == texts.commit.ask_content.format(path="services/hello/bad.py") + await tg.send(ADMIN, "def f(:\n pass") + await tg.press(ADMIN, cb(Act.FORCE)) + assert last_text(tg).text.startswith(texts.commit.added.format(path="services/hello/bad.py")) + + +async def test_virtual_folder_becomes_real_with_a_file(tg: TgClient, texts: Texts) -> None: + await tg.send(ADMIN, texts.menu.commit) + await tg.press(ADMIN, cb(Act.OPEN, 2)) # services + await tg.press(ADMIN, cb(Act.NEW_DIR)) + await tg.send(ADMIN, "api") + inside = last_text(tg) + assert inside.text == f"{texts.commit.title.format(path='services/api')}\n{texts.commit.empty}" + + await tg.press(ADMIN, cb(Act.NEW_FILE)) + await tg.send(ADMIN, "main.py") + await tg.send(ADMIN, "print('api')") + await tg.press(ADMIN, cb(Act.UP)) + + services = tg.session.sent(EditMessageText)[-1] + assert "🆕 📁 api" in labels(services.reply_markup) + + +async def test_names_rejected(tg: TgClient, texts: Texts) -> None: + await tg.send(ADMIN, texts.menu.commit) + await tg.press(ADMIN, cb(Act.NEW_FILE)) + for bad in ["../etc", "README.md", "ruff.toml"]: + await tg.send(ADMIN, bad) + + assert [m.text for m in tg.session.sent(SendMessage)][-3:] == [ + texts.commit.bad_name, + texts.commit.exists, + texts.commit.protected, + ] + + +async def test_binary_document_rejected(tg: TgClient, texts: Texts) -> None: + await to_hello(tg, texts) + await tg.press(ADMIN, cb(Act.NEW_FILE)) + await tg.send(ADMIN, "img.png") + await tg.send_document(ADMIN, "img.png", b"\x89PNG\x00\x00") + + assert last_text(tg).text == texts.commit.binary + + +async def test_draft_survives_a_restart( + texts: Texts, sessions: SessionMaker, access: AccessService, repo: UploadsRepo +) -> None: + first = TgClient(build_dispatcher(texts, sessions, access, repo)) + await first.send(ADMIN, texts.menu.commit) + await first.press(ADMIN, cb(Act.OPEN, 3)) + await first.press(ADMIN, cb(Act.DELETE)) + + second = TgClient(build_dispatcher(texts, sessions, access, repo)) # same DB, new bot process + await second.send(ADMIN, texts.menu.commit) + assert texts.commit.draft_button.format(count=1) in labels(second.session.sent(SendMessage)[-1].reply_markup) + + +async def test_protected_file_cannot_be_replaced(tg: TgClient, texts: Texts, repo: UploadsRepo) -> None: + await tg.send(ADMIN, texts.menu.commit) + # fake a card for a protected path via FSM: open README then rewrite the selection + await tg.press(ADMIN, cb(Act.OPEN, 3)) + state = tg.dp.fsm.get_context(tg.bot, chat_id=ADMIN, user_id=ADMIN) + await state.update_data(commit_file=".sourcecraft/ci.yaml") + await tg.press(ADMIN, cb(Act.REPLACE)) + + answer = tg.session.sent(AnswerCallbackQuery)[-1] + assert answer.text == texts.commit.protected and answer.show_alert diff --git a/tests/fakes.py b/tests/fakes.py index f03867d..a563e04 100644 --- a/tests/fakes.py +++ b/tests/fakes.py @@ -7,9 +7,9 @@ from typing import Any from aiogram import Bot, Dispatcher from aiogram.client.session.base import BaseSession -from aiogram.methods import EditMessageText, GetMe, SendMessage, SendPhoto, TelegramMethod +from aiogram.methods import EditMessageText, GetFile, GetMe, SendMessage, SendPhoto, TelegramMethod from aiogram.methods.base import TelegramType -from aiogram.types import Chat, Message, Update, User +from aiogram.types import Chat, File, Message, Update, User BOT_TOKEN = "42:TEST-TOKEN" @@ -19,6 +19,7 @@ class FakeSession(BaseSession): super().__init__() self.requests: list[TelegramMethod[Any]] = [] self._ids = count(1000) + self.files: dict[str, bytes] = {} # file_id -> content, for documents users "upload" async def make_request( self, @@ -27,6 +28,10 @@ class FakeSession(BaseSession): timeout: int | None = None, # noqa: ASYNC109 - signature fixed by aiogram BaseSession ) -> TelegramType: self.requests.append(method) + if isinstance(method, GetFile): + return File( # type: ignore[return-value] + file_id=method.file_id, file_unique_id=method.file_id, file_path=f"documents/{method.file_id}" + ) if isinstance(method, GetMe): return User(id=42, is_bot=True, first_name="tgit", username="tgit_test_bot") # type: ignore[return-value] if isinstance(method, SendPhoto): @@ -54,9 +59,9 @@ class FakeSession(BaseSession): timeout: int = 30, # noqa: ASYNC109 - signature fixed by aiogram BaseSession chunk_size: int = 65536, raise_for_status: bool = True, - ) -> AsyncGenerator[bytes, None]: # pragma: no cover - not used - raise NotImplementedError - yield b"" + ) -> AsyncGenerator[bytes, None]: + file_id = url.rsplit("/", 1)[-1] + yield self.files[file_id] async def close(self) -> None: pass @@ -92,6 +97,18 @@ class TgClient: message["entities"] = [{"type": "bot_command", "offset": 0, "length": len(cmd)}] await self._feed({"message": message}) + async def send_document(self, tg_id: int, name: str, content: bytes) -> None: + file_id = f"doc{next(self._message_ids)}" + self.session.files[file_id] = content + message = { + "message_id": next(self._message_ids), + "date": int(datetime.now(UTC).timestamp()), + "chat": {"id": tg_id, "type": "private"}, + "from": self.user(tg_id), + "document": {"file_id": file_id, "file_unique_id": file_id, "file_name": name, "file_size": len(content)}, + } + await self._feed({"message": message}) + async def press(self, tg_id: int, data: str, message_text: str = "section") -> None: callback = { "id": str(next(self._update_ids)), diff --git a/tests/repo/test_drafts.py b/tests/repo/test_drafts.py new file mode 100644 index 0000000..5802d4f --- /dev/null +++ b/tests/repo/test_drafts.py @@ -0,0 +1,72 @@ +from datetime import UTC, datetime, timedelta + +import pytest + +from app.db.models import ChangeOp, User +from app.db.session import SessionMaker +from app.repo.drafts import MAX_CHANGES, DraftFull, DraftService + +UID = 7 +SHA = "a" * 40 + + +class Clock: + now = datetime(2026, 10, 15, tzinfo=UTC) + + def __call__(self) -> datetime: + return self.now + + +@pytest.fixture +async def svc(sessions: SessionMaker) -> DraftService: + async with sessions.begin() as s: + s.add(User(tg_id=UID, full_name="u")) + return DraftService(sessions, clock=Clock()) + + +async def test_put_get_and_order(svc: DraftService) -> None: + await svc.put(UID, SHA, "b.py", ChangeOp.ADD, b"b") + await svc.put(UID, "other-sha", "a.py", ChangeOp.MODIFY, b"a") + + draft = await svc.get(UID) + assert draft is not None and draft.base_sha == SHA # base fixed when the draft started + assert [(c.path, c.op) for c in draft.changes] == [("b.py", ChangeOp.ADD), ("a.py", ChangeOp.MODIFY)] + + +async def test_editing_a_new_file_keeps_it_added_and_deleting_it_drops_it(svc: DraftService) -> None: + await svc.put(UID, SHA, "n.py", ChangeOp.ADD, b"1") + await svc.put(UID, SHA, "n.py", ChangeOp.MODIFY, b"2") + draft = await svc.get(UID) + assert draft is not None and draft.changes[0].op is ChangeOp.ADD and draft.changes[0].content == b"2" + + await svc.put(UID, SHA, "n.py", ChangeOp.DELETE, None) + draft = await svc.get(UID) + assert draft is not None and draft.changes == [] + + +async def test_drop_message_clear(svc: DraftService) -> None: + await svc.put(UID, SHA, "a.py", ChangeOp.ADD, b"1") + await svc.put(UID, SHA, "b.py", ChangeOp.DELETE, None) + await svc.drop(UID, "a.py") + await svc.set_message(UID, "feat: x") + + draft = await svc.get(UID) + assert draft is not None and [c.path for c in draft.changes] == ["b.py"] and draft.message == "feat: x" + await svc.clear(UID) + assert await svc.get(UID) is None + + +async def test_expired_draft_is_gone(svc: DraftService) -> None: + await svc.put(UID, SHA, "a.py", ChangeOp.ADD, b"1") + Clock.now += timedelta(hours=25) + try: + assert await svc.get(UID) is None + finally: + Clock.now -= timedelta(hours=25) + + +async def test_draft_size_limit(svc: DraftService) -> None: + for i in range(MAX_CHANGES): + await svc.put(UID, SHA, f"f{i}.py", ChangeOp.ADD, b"") + with pytest.raises(DraftFull): + await svc.put(UID, SHA, "one-more.py", ChangeOp.ADD, b"") diff --git a/tests/repo/test_validate.py b/tests/repo/test_validate.py new file mode 100644 index 0000000..5c48fdb --- /dev/null +++ b/tests/repo/test_validate.py @@ -0,0 +1,74 @@ +import pytest + +from app.repo.validate import ( + ValidationError, + check_content, + check_name, + diff_stat, + is_protected, + join, + python_syntax, + strip_fence, +) + + +@pytest.mark.parametrize("name", ["main.py", "a-b_c.1", ".gitignore", "X" * 100]) +def test_good_names(name: str) -> None: + assert check_name(f" {name} ") == name + + +@pytest.mark.parametrize("name", ["", ".", "..", "a/b", "привет.py", "a b", "X" * 101, "a;rm"]) +def test_bad_names(name: str) -> None: + with pytest.raises(ValidationError): + check_name(name) + + +@pytest.mark.parametrize( + ("path", "protected"), + [ + (".sourcecraft/ci.yaml", True), + (".tgit/x.py", True), + ("ruff.toml", True), + (".sourcecraft", True), + ("services/a/ruff.toml", False), + ("README.md", False), + ("services/a/main.py", False), + ], +) +def test_protected_paths(path: str, protected: bool) -> None: + assert is_protected(path) is protected + + +def test_join() -> None: + assert join("", "a.py") == "a.py" and join("s/x", "a.py") == "s/x/a.py" + + +@pytest.mark.parametrize( + ("text", "expected"), + [ + ("```python\nprint(1)\n```", "print(1)"), + ("```\na\nb\n```", "a\nb"), + ("print(1)", "print(1)"), + ("x ``` y", "x ``` y"), + ], +) +def test_strip_fence(text: str, expected: str) -> None: + assert strip_fence(text) == expected + + +def test_content_checks() -> None: + assert check_content("привет".encode()) == "привет" + for data, reason in [(b"a\0b", "binary"), (b"\xff\xfe", "encoding"), (b"x" * (100 * 1024 + 1), "too_big")]: + with pytest.raises(ValidationError, match=reason): + check_content(data) + + +def test_python_syntax_only_for_py() -> None: + issue = python_syntax("a.py", "def f(:\n pass\n") + assert issue is not None and issue.line == 1 + assert python_syntax("a.py", "x = 1\n") is None + assert python_syntax("a.txt", "def f(:") is None + + +def test_diff_stat() -> None: + assert diff_stat("a\nb\nc", "a\nB\nc\nd") == (2, 1)