wrolpi/modules/repos/api.py
Roland 5c80ad7ba4
repos: search Repos by name and README
repository_fts (FTS5, external content) indexes each Repo's search_name
(name and owner) and readme_text; a name match outranks a README
mention.  The downloader stores the README as plain text (markdown
syntax removed so snippets read well), writing it only when it changed.

* POST /api/repos/search: by search and/or Tag, with README headlines.
* /api/search_other_estimates takes an optional search_str and returns
  repo_count, so Repos count toward the global search's Other tab.
* /api/search_suggestions suggests Repos by name.

The repository FTS DDL is installed only once the repository table
exists: the baseline migration also runs install_raw_ddl, before that
table is created.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 16:56:09 -06:00

110 lines
4.1 KiB
Python

from http import HTTPStatus
from sanic import Request, Blueprint, response
from sanic_ext.extensions.openapi import openapi
from wrolpi.api_utils import json_response
from wrolpi.common import wrol_mode_check, logger
from wrolpi.downloader import download_manager
from wrolpi.errors import ValidationError
from . import lib, schema
from .models import Repository
repos_bp = Blueprint('Repos', '/api/repos')
logger = logger.getChild(__name__)
@repos_bp.get('/')
@openapi.definition(summary='List all git Repos')
async def get_repos(request: Request):
repos = lib.get_repositories(request.ctx.session)
return json_response(dict(repos=repos))
@repos_bp.post('/')
@openapi.definition(
summary='Add a git Repo; it will be cloned, then kept up to date',
body=schema.RepoCreateRequest,
validate=True,
)
@wrol_mode_check
async def post_repo(request: Request, body: schema.RepoCreateRequest):
session = request.ctx.session
repo = lib.create_repository(
session,
body.url,
tag_name=body.tag_name or None,
frequency=body.frequency or lib.DEFAULT_REPO_FREQUENCY,
mode=body.mode or 'full',
branch=body.branch or None,
name=body.name or None,
description=body.description or None,
)
repo = Repository.find_by_id(session, repo.id, options=Repository.json_options())
return json_response(dict(repo=repo.__json__()), status=HTTPStatus.CREATED)
@repos_bp.post('/search')
@openapi.definition(
summary='Search git Repos by their name and README, and/or by Tag',
body=schema.RepoSearchRequest,
validate=True,
)
async def post_search_repos(request: Request, body: schema.RepoSearchRequest):
limit = min(max(body.limit, 1), 100)
repos, total = lib.search_repos(request.ctx.session, body.search_str, body.tag_names, limit=limit,
offset=max(body.offset, 0))
return json_response(dict(repos=repos, totals=dict(repos=total)))
@repos_bp.get('/<repo_id:int>')
@openapi.definition(summary='Get a git Repo')
async def get_repo(request: Request, repo_id: int):
repo = Repository.find_by_id(request.ctx.session, repo_id, options=Repository.json_options())
return json_response(dict(repo=repo.__json__()))
@repos_bp.put('/<repo_id:int>')
@openapi.definition(
summary="Change a git Repo's settings",
body=schema.RepoUpdateRequest,
validate=True,
)
@wrol_mode_check
async def put_repo(request: Request, repo_id: int, body: schema.RepoUpdateRequest):
session = request.ctx.session
lib.update_repository(session, repo_id, description=body.description, frequency=body.frequency,
mode=body.mode, branch=body.branch)
repo = Repository.find_by_id(session, repo_id, options=Repository.json_options())
return json_response(dict(repo=repo.__json__()))
@repos_bp.delete('/<repo_id:int>')
@openapi.definition(summary='Delete a git Repo. Its files are kept unless `delete_files=true`.')
@wrol_mode_check
async def delete_repo(request: Request, repo_id: int):
delete_files = request.args.get('delete_files', '').lower() in ('1', 'true', 'yes')
await lib.delete_repository(request.ctx.session, repo_id, delete_files=delete_files)
return response.empty()
@repos_bp.post('/<repo_id:int>/update')
@openapi.definition(summary='Update a git Repo now')
@wrol_mode_check
async def post_update_repo(request: Request, repo_id: int):
session = request.ctx.session
repo = Repository.find_by_id(session, repo_id, options=Repository.json_options())
if not (download := repo.download):
raise ValidationError('Repo has no download')
download_manager.restart_download(session, download.id)
return response.empty(HTTPStatus.NO_CONTENT)
@repos_bp.get('/<repo_id:int>/tree')
@openapi.definition(summary="List a directory of a git Repo's files")
@openapi.parameter('path', str, 'query', description='Directory in the repo, relative to its root', required=False)
async def get_repo_tree(request: Request, repo_id: int):
repo = Repository.find_by_id(request.ctx.session, repo_id)
tree = lib.list_repo_tree(repo, request.args.get('path', ''))
return json_response(tree)