Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd636cce20 | ||
|
|
6dd7d3f465 | ||
|
|
4a15e9fee0 | ||
|
|
7a965e5dfc | ||
|
|
2f69f49690 | ||
|
|
1266204ecb | ||
|
|
6492b22219 | ||
|
|
8631722116 | ||
|
|
2f014deb05 | ||
|
|
3d8b240d9b | ||
|
|
5c2fc4022e | ||
|
|
bf23ba3f38 | ||
|
|
493dc874f0 | ||
|
|
614a85c429 | ||
|
|
9956d6a2c4 | ||
|
|
25066d687e |
+98
@@ -0,0 +1,98 @@
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi.responses import HTMLResponse
|
||||
from dotenv import load_dotenv
|
||||
import logging
|
||||
import os
|
||||
|
||||
from utils import clone_repository, execute_runner_script
|
||||
from setup_logging import setup_logging
|
||||
from gitea_request import GiteaRequest
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get('/', response_class=HTMLResponse)
|
||||
async def root():
|
||||
content_str = """
|
||||
<html>
|
||||
<head>
|
||||
<title>Gitea Runner</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome to Gitea Runner</h1>
|
||||
<p>see /docs for more information</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return HTMLResponse(content=content_str, status_code=200)
|
||||
|
||||
|
||||
@app.post('/test')
|
||||
async def test(request: Request):
|
||||
""" Test connection to server. """
|
||||
logging.debug('Content-Type: ', request.headers.get('content-type'))
|
||||
raise HTTPException(
|
||||
status_code=200,
|
||||
detail='test completed'
|
||||
)
|
||||
|
||||
|
||||
@app.post('/event')
|
||||
def handle_event(request: GiteaRequest):
|
||||
""" Handle event from webhook. """
|
||||
# extract information
|
||||
commit_msg = request.head_commit['message']
|
||||
repo_url = request.repository['clone_url']
|
||||
# branch_name = request.ref.split('/')[-1]
|
||||
# stop early if asked
|
||||
if commit_msg.lower().startswith('[skip ci]'):
|
||||
raise HTTPException(
|
||||
status_code=200,
|
||||
detail='skipping ci'
|
||||
)
|
||||
# clone repository
|
||||
local_repo_dir = clone_repository(repo_url)
|
||||
# locate runner script
|
||||
runner_script_path = local_repo_dir / 'runner_script.yml'
|
||||
if not runner_script_path.exists():
|
||||
raise HTTPException(
|
||||
status_code=200,
|
||||
detail="no runner_script.yml in repository root"
|
||||
)
|
||||
# execute runner script
|
||||
logging.info('rexecuting runner script')
|
||||
execute_runner_script(path=runner_script_path)
|
||||
# # TODO: create virtualenv?
|
||||
# # install requirements
|
||||
# assert pipeline.requirements is not None
|
||||
# requirements_path = local_repo_dir / pipeline.requirements
|
||||
# os.system(f'pip install -r {requirements_path}')
|
||||
# for cmd in pipeline.script:
|
||||
# try:
|
||||
# os.system(cmd)
|
||||
# except Exception as e:
|
||||
# raise HTTPException(
|
||||
# status_code=503,
|
||||
# detail=f'failed running {cmd}: {e}'
|
||||
# )
|
||||
|
||||
# send response
|
||||
raise HTTPException(
|
||||
status_code=202,
|
||||
detail='received task'
|
||||
)
|
||||
# consider to include link to page that show progress
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
load_dotenv('dev.env')
|
||||
setup_logging()
|
||||
listen_ip = os.getenv('LISTEN_IP', default='0.0.0.0')
|
||||
listen_port = int(os.getenv('LISTEN_PORT', default=8000))
|
||||
logging.info(f'starting FastAPI server ({listen_ip}:{listen_port})')
|
||||
uvicorn.run(
|
||||
app=app,
|
||||
host=listen_ip,
|
||||
port=listen_port
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
[main]
|
||||
logger_level=debug
|
||||
|
||||
[fastapi]
|
||||
listen_ip=0.0.0.0
|
||||
listen_port=8000
|
||||
@@ -0,0 +1,15 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
|
||||
|
||||
class GiteaRequest(BaseModel):
|
||||
ref: str
|
||||
before: str
|
||||
after: str
|
||||
compare_url: str
|
||||
commits: List[dict]
|
||||
total_commits: int
|
||||
head_commit: dict
|
||||
repository: dict
|
||||
pusher: dict
|
||||
sender: dict
|
||||
@@ -0,0 +1,31 @@
|
||||
import uvicorn
|
||||
from setup_logging import setup_logging
|
||||
from dotenv import load_dotenv
|
||||
from app import app
|
||||
import logging
|
||||
import os
|
||||
from configparser import ConfigParser
|
||||
from pathlib import Path
|
||||
import socket
|
||||
|
||||
|
||||
# load default values
|
||||
config = ConfigParser()
|
||||
config.read(Path(__file__).parent / 'defaults.ini')
|
||||
LISTEN_IP = config.get('fastapi', 'listen_ip')
|
||||
LISTEN_PORT = int(config.get('fastapi', 'listen_port'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# setup
|
||||
load_dotenv()
|
||||
setup_logging()
|
||||
listen_ip = os.getenv('LISTEN_IP', default=LISTEN_IP)
|
||||
listen_port = int(os.getenv('LISTEN_PORT', default=LISTEN_PORT))
|
||||
own_ip = socket.gethostbyname(socket.gethostname())
|
||||
logging.info(f'starting FastAPI server ({own_ip}:{listen_port})')
|
||||
uvicorn.run(
|
||||
app=app,
|
||||
host=listen_ip,
|
||||
port=listen_port
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
import logging
|
||||
|
||||
|
||||
class RunnerScript(BaseModel):
|
||||
script: List[str]
|
||||
|
||||
@staticmethod
|
||||
def from_file(path: Path):
|
||||
assert path.exists()
|
||||
with open(path, 'r') as fh:
|
||||
content_dict = yaml.safe_load(fh)
|
||||
script_list = content_dict['script'].split('\n')
|
||||
logging.debug('finished')
|
||||
rs = RunnerScript.model_validate( # type: ignore
|
||||
{
|
||||
'script': script_list
|
||||
}
|
||||
)
|
||||
return rs
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
path = Path(__file__).parent.parent / 'runner_script.yml'
|
||||
rs = RunnerScript.from_file(path)
|
||||
print(rs)
|
||||
@@ -0,0 +1,31 @@
|
||||
from dotenv import load_dotenv
|
||||
import logging
|
||||
from configparser import ConfigParser
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
# load default values
|
||||
config = ConfigParser()
|
||||
config.read(Path(__file__).parent / 'defaults.ini')
|
||||
LOGGER_LEVEL = config.get('main', 'logger_level')
|
||||
|
||||
|
||||
def setup_logging(
|
||||
logger_level: str = LOGGER_LEVEL
|
||||
):
|
||||
fmt = (
|
||||
'%(asctime)s | '
|
||||
'%(levelname)s | '
|
||||
'%(filename)s | '
|
||||
'%(funcName)s | '
|
||||
'%(message)s'
|
||||
)
|
||||
datefmt = '%Y-%m-%d %H:%M:%S'
|
||||
level = getattr(logging, logger_level.upper())
|
||||
logging.basicConfig(format=fmt, datefmt=datefmt, level=level)
|
||||
logging.debug('finished')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
load_dotenv('dev.env')
|
||||
setup_logging()
|
||||
@@ -0,0 +1,30 @@
|
||||
from pydantic import AnyHttpUrl
|
||||
from pathlib import Path
|
||||
from git import Repo
|
||||
import logging
|
||||
import shutil
|
||||
import os
|
||||
from runner_script import RunnerScript
|
||||
|
||||
|
||||
def clone_repository(repo_url: AnyHttpUrl) -> Path:
|
||||
# extract repo name
|
||||
repo_name = repo_url.split('/')[-1].replace('.git', '')
|
||||
repo_dir = os.getenv('REPO_DIR', default=None)
|
||||
assert repo_dir is not None
|
||||
dst_dir = Path(repo_dir) / repo_name
|
||||
# prepare repo download dir
|
||||
if dst_dir.exists():
|
||||
shutil.rmtree(dst_dir)
|
||||
dst_dir.mkdir()
|
||||
# git clone repo
|
||||
Repo.clone_from(url=repo_url, to_path=dst_dir)
|
||||
logging.debug('finished')
|
||||
return dst_dir
|
||||
|
||||
|
||||
def execute_runner_script(path: Path) -> None:
|
||||
script_list = RunnerScript.from_file(path)
|
||||
for cmd in script_list:
|
||||
os.system(cmd)
|
||||
logging.debug('finished')
|
||||
@@ -0,0 +1,13 @@
|
||||
[mypy]
|
||||
|
||||
[mypy-uvicorn.*]
|
||||
ignore_missing_imports = True
|
||||
|
||||
[mypy-dotenv.*]
|
||||
ignore_missing_imports = True
|
||||
|
||||
[mypy-git.*]
|
||||
ignore_missing_imports = True
|
||||
|
||||
[mypy-fastapi.*]
|
||||
ignore_missing_imports = True
|
||||
@@ -0,0 +1,18 @@
|
||||
annotated-types==0.5.0
|
||||
anyio==3.7.1
|
||||
click==8.1.4
|
||||
exceptiongroup==1.1.2
|
||||
fastapi==0.100.0
|
||||
gitdb==4.0.10
|
||||
GitPython==3.1.32
|
||||
h11==0.14.0
|
||||
idna==3.4
|
||||
pydantic==2.0.2
|
||||
pydantic_core==2.1.2
|
||||
python-dotenv==1.0.0
|
||||
PyYAML==6.0
|
||||
smmap==5.0.0
|
||||
sniffio==1.3.0
|
||||
starlette==0.27.0
|
||||
typing_extensions==4.7.1
|
||||
uvicorn==0.22.0
|
||||
@@ -0,0 +1,3 @@
|
||||
script: |
|
||||
echo "hello world"
|
||||
echo "hello back"
|
||||
Reference in New Issue
Block a user