From 1d06423081b04a54d69376f4ac0cea0b40dc5b2d Mon Sep 17 00:00:00 2001 From: "bozo.kopic" Date: Sun, 27 Mar 2022 20:18:44 +0200 Subject: WIP server --- src_py/hatter/ui.py | 135 ++++++++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 62 deletions(-) (limited to 'src_py/hatter/ui.py') diff --git a/src_py/hatter/ui.py b/src_py/hatter/ui.py index cb827bd..a10eede 100644 --- a/src_py/hatter/ui.py +++ b/src_py/hatter/ui.py @@ -22,18 +22,17 @@ async def create(host: str, app = aiohttp.web.Application() get_routes = ( aiohttp.web.get(path, handler) for path, handler in ( - ('/', ui._get_root_handler), - ('/repo/{repo}', ui._get_repo_handler), - ('/repo/{repo}/commit/{commit}', ui._get_commit_handler))) + ('/', ui._process_get_root), + ('/repo/{repo}', ui._process_get_repo), + ('/repo/{repo}/commit/{commit}', ui._process_get_commit))) post_routes = ( aiohttp.web.post(path, handler) for path, handler in ( - ('/repo/{repo}/webhook', ui._post_webhook_handler), - ('/repo/{repo}/add', ui._post_add_handler), - ('/repo/{repo}/commit/{commit}/rerun', ui._post_rerun_handler), - ('/repo/{repo}/commit/{commit}/remove', ui._post_remove_handler))) - app.add_routes([*get_routes, - *post_routes, - aiohttp.web.static('/', static_dir)]) + ('/repo/{repo}/run', ui._process_post_run), + ('/repo/{repo}/commit/{commit}/remove', ui._process_post_remove))) + webhook_route = aiohttp.web.route('*', '/repo/{repo}/webhook', + ui._process_webhook) + static_route = aiohttp.web.static('/', static_dir) + app.add_routes([*get_routes, *post_routes, webhook_route, static_route]) runner = aiohttp.web.AppRunner(app) await runner.setup() @@ -60,70 +59,68 @@ class UI(aio.Resource): def async_group(self): return self._async_group - async def _get_root_handler(self, request): + async def _process_get_root(self, request): commits = await self._server.get_commits(None) body = (f'{_generate_repos(self._server.repos)}\n' f'{_generate_commits(commits)}') return _create_html_response('hatter', body) - async def _get_repo_handler(self, request): - repo = _parse_repo(request, self._server.repos) - + async def _process_get_repo(self, request): + repo = self._get_repo(request) commits = await self._server.get_commits(repo) + title = f'hatter - {repo}' body = (f'{_generate_commits(commits)}\n' - f'{_generate_add(repo)}') - return _create_html_response(f'hatter - {repo}', body) - - async def _get_commit_handler(self, request): - repo = _parse_repo(request, self._server.repos) + f'{_generate_run(repo)}') + return _create_html_response(title, body) - commit_hash = request.match_info['commit'] - commit = await self._server.get_commit(repo, commit_hash) + async def _process_get_commit(self, request): + commit = await self._get_commit(request) + title = f'hatter - {commit.repo}/{commit.hash}' body = _generate_commit(commit) - return _create_html_response(f'hatter - {repo}/{commit_hash}', body) - - async def _post_webhook_handler(self, request): - repo = _parse_repo(request, self._server.repos) - - self._server.sync_repo(repo) - - return aiohttp.web.Response() + return _create_html_response(title, body) - async def _post_add_handler(self, request): - repo = _parse_repo(request, self._server.repos) + async def _process_post_run(self, request): + repo = self._get_repo(request) body = await request.post() commit_hash = body['hash'] if not commit_hash: raise aiohttp.web.HTTPBadRequest() - raise aiohttp.web.HTTPFound(f'/repo/{repo}/commit/{commit_hash}') + commit = await self._server.run_commit(repo, commit_hash) - async def _post_rerun_handler(self, request): - repo = _parse_repo(request, self._server.repos) + url = f'/repo/{commit.repo}/commit/{commit.hash}' + raise aiohttp.web.HTTPFound(url) - commit_hash = request.match_info['commit'] - await self._server.rerun_commit(repo, commit_hash) + async def _process_post_remove(self, request): + commit = await self._get_commit(request) - raise aiohttp.web.HTTPFound(f'/repo/{repo}/commit/{commit_hash}') + await self._server.remove_commit(commit) - async def _post_remove_handler(self, request): - repo = _parse_repo(request, self._server.repos) + raise aiohttp.web.HTTPFound(f'/repo/{commit.repo}') - commit_hash = request.match_info['commit'] - await self._server.remove_commit(repo, commit_hash) + async def _process_webhook(self, request): + repo = self._get_repo(request) - raise aiohttp.web.HTTPFound(f'/repo/{repo}') + self._server.sync_repo(repo) + return aiohttp.web.Response() + def _get_repo(self, request): + repo = request.match_info['repo'] + if repo not in self._server.repos: + raise aiohttp.web.HTTPBadRequest() + return repo -def _parse_repo(request, repos): - repo = request.match_info['repo'] - if repo not in repos: - raise aiohttp.web.HTTPBadRequest() - return repo + async def _get_commit(self, request): + repo = self._get_repo(request) + commit_hash = request.match_info['commit'] + commit = await self._server.get_commit(repo, commit_hash) + if not commit: + raise aiohttp.web.HTTPBadRequest() + return commit def _create_html_response(title, body): @@ -155,8 +152,8 @@ def _generate_commits(commits): tbody = '\n'.join( (f'\n' f'{_format_time(commit.change)}\n' - f'{commit.repo}\n' # NOQA - f'{commit.hash}\n' # NOQA + f'{_generate_repo_link(commit.repo)}\n' + f'{_generate_commit_link(commit)}\n' f'{commit.status.name}\n' f'') for commit in commits) @@ -175,33 +172,47 @@ def _generate_commits(commits): def _generate_commit(commit): - buttons = '\n'.join( - (f'
\n' - f'\n' - f'
') - for value, action in ( - ('Rerun', f'/repo/{commit.repo}/commit/{commit.hash}/rerun'), - ('Remove', f'/repo/{commit.repo}/commit/{commit.hash}/remove'))) + run_action = f'/repo/{commit.repo}/run' + run_button = (f'
\n' + f'\n' + f'\n' + f'
') + + remove_action = f'/repo/{commit.repo}/commit/{commit.hash}/remove' + remove_button = (f'
\n' + f'\n' + f'
') + + repo_link = _generate_repo_link(commit.repo) return (f'
\n' - f'
{commit.repo}
\n' # NOQA + f'
{repo_link}
\n' f'
{commit.hash}
\n' f'
{_format_time(commit.change)}
\n' f'
{commit.status.name}
\n' f'
{commit.output}
\n' - f'
{buttons}
\n' + f'
{run_button}{remove_button}
\n' f'
') -def _generate_add(repo): - return (f'
\n' - f'
\n' +def _generate_run(repo): + return (f'
\n' + f'\n' f'\n' - f'\n' + f'\n' f'\n' f'
') +def _generate_repo_link(repo): + return f'{repo}' + + +def _generate_commit_link(commit): + url = f'/repo/{commit.repo}/commit/{commit.hash}' + return f'{commit.hash}' + + def _format_time(t): return datetime.datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S") -- cgit v1.2.3-70-g09d2