Compare commits
No commits in common. "0d31493bd375b4c21ddace6aeae6044683813b8c" and "52f91b88abeb4762c7f2ee1dc4647db0480c6d2e" have entirely different histories.
0d31493bd3
...
52f91b88ab
3 changed files with 25 additions and 56 deletions
|
@ -47,6 +47,6 @@ jobs:
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: uv sync
|
run: uv sync
|
||||||
- name: Lint with Ruff
|
- name: Lint with Ruff
|
||||||
run: uv run ruff check $(git ls-files '*.py')
|
run: uv run ruff check
|
||||||
- name: Lint with Pylint
|
- name: Lint with Pylint
|
||||||
run: uv run pylint --rcfile=.forgejo/workflows/config/.pylintrc $(git ls-files '*.py')
|
run: uv run pylint src/
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
[MESSAGES CONTROL]
|
|
||||||
disable=
|
|
||||||
too-many-lines,
|
|
||||||
missing-module-docstring,
|
|
||||||
missing-function-docstring,
|
|
||||||
missing-class-docstring,
|
|
||||||
line-too-long,
|
|
||||||
too-many-arguments,
|
|
||||||
too-many-instance-attributes,
|
|
||||||
import-outside-toplevel,
|
|
67
src/main.py
67
src/main.py
|
@ -10,7 +10,7 @@ from flask import Flask, Response, render_template
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Config:
|
class Config():
|
||||||
base_url: str
|
base_url: str
|
||||||
token: t.Optional[str]
|
token: t.Optional[str]
|
||||||
user: str
|
user: str
|
||||||
|
@ -20,16 +20,15 @@ class Config:
|
||||||
artifact_names: t.List[str]
|
artifact_names: t.List[str]
|
||||||
debug: bool
|
debug: bool
|
||||||
|
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
"base_url": os.environ.get("FORGEJO_BASE_URL"),
|
"base_url": os.environ.get('FORGEJO_BASE_URL'),
|
||||||
"token": os.environ.get("FORGEJO_TOKEN") or None,
|
"token": os.environ.get('FORGEJO_TOKEN') or None,
|
||||||
"user": os.environ.get("FORGEJO_USER"),
|
"user": os.environ.get('FORGEJO_USER'),
|
||||||
"repo": os.environ.get("FORGEJO_REPO"),
|
"repo": os.environ.get('FORGEJO_REPO'),
|
||||||
"task_name": os.environ.get("FORGEJO_TASK_NAME"),
|
"task_name": os.environ.get('FORGEJO_TASK_NAME'),
|
||||||
"page_size": os.environ.get("FORGEJO_PAGE_SIZE") or 10,
|
"page_size": os.environ.get('FORGEJO_PAGE_SIZE') or 10,
|
||||||
"artifact_names": os.environ.get("FORGEJO_ARTIFACT_NAMES", "").split(","),
|
"artifact_names": os.environ.get('FORGEJO_ARTIFACT_NAMES', '').split(','),
|
||||||
"debug": os.environ.get("DEBUG", "") or False,
|
"debug": os.environ.get('DEBUG', '') or False,
|
||||||
}
|
}
|
||||||
config = Config(**config)
|
config = Config(**config)
|
||||||
|
|
||||||
|
@ -44,11 +43,10 @@ if not config.task_name:
|
||||||
if not config.artifact_names:
|
if not config.artifact_names:
|
||||||
raise ValueError("FORGEJO_ARTIFACT_NAMES is required")
|
raise ValueError("FORGEJO_ARTIFACT_NAMES is required")
|
||||||
|
|
||||||
app = Flask(__name__, template_folder="templates")
|
app = Flask(__name__, template_folder='templates')
|
||||||
|
|
||||||
|
|
||||||
async def fetch_tasks():
|
async def fetch_tasks():
|
||||||
url = f"{config.base_url}/api/v1/repos/{config.user}/{config.repo}/actions/tasks"
|
url = f'{config.base_url}/api/v1/repos/{config.user}/{config.repo}/actions/tasks'
|
||||||
headers = {
|
headers = {
|
||||||
"accept": "application/json",
|
"accept": "application/json",
|
||||||
}
|
}
|
||||||
|
@ -61,10 +59,9 @@ async def fetch_tasks():
|
||||||
async with session.get(url, headers=headers, params=params) as response:
|
async with session.get(url, headers=headers, params=params) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
for task in data["workflow_runs"]:
|
for task in data['workflow_runs']:
|
||||||
if task["name"] == config.task_name:
|
if task['name'] == config.task_name:
|
||||||
return task["url"]
|
return task['url']
|
||||||
|
|
||||||
|
|
||||||
async def fetch_artifact(artifact_url):
|
async def fetch_artifact(artifact_url):
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
@ -72,55 +69,37 @@ async def fetch_artifact(artifact_url):
|
||||||
data = await response.read()
|
data = await response.read()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
async def handle_artifact(artifact_name: str):
|
async def handle_artifact(artifact_name: str):
|
||||||
task = await fetch_tasks()
|
task = await fetch_tasks()
|
||||||
artifact_url = f"{task}/artifacts/{artifact_name}"
|
artifact_url = f"{task}/artifacts/{artifact_name}"
|
||||||
artifact_data = await fetch_artifact(artifact_url)
|
artifact_data = await fetch_artifact(artifact_url)
|
||||||
response = Response(artifact_data)
|
response = Response(artifact_data)
|
||||||
response.headers["Content-Disposition"] = (
|
response.headers['Content-Disposition'] = 'attachment; filename="GalacticFactory.zip"'
|
||||||
'attachment; filename="GalacticFactory.zip"'
|
|
||||||
)
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def create_handle_artifact(artifact_name: str):
|
def create_handle_artifact(artifact_name: str):
|
||||||
async def handle_artifact_async():
|
async def handle_artifact_async():
|
||||||
return await handle_artifact(artifact_name)
|
return await handle_artifact(artifact_name)
|
||||||
|
|
||||||
return handle_artifact_async
|
return handle_artifact_async
|
||||||
|
|
||||||
|
for artifact_name in config.artifact_names:
|
||||||
|
route = f'/{artifact_name}/GalacticFactory'
|
||||||
|
endpoint = f'handle_{artifact_name}'
|
||||||
|
app.add_url_rule(route, view_func=create_handle_artifact(artifact_name), endpoint=endpoint)
|
||||||
|
|
||||||
for artifact in config.artifact_names:
|
@app.route('/')
|
||||||
route = f"/{artifact}/GalacticFactory" # pylint: disable=invalid-name
|
|
||||||
endpoint = f"handle_{artifact}" # pylint: disable=invalid-name
|
|
||||||
app.add_url_rule(
|
|
||||||
route, view_func=create_handle_artifact(artifact), endpoint=endpoint
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def index():
|
def index():
|
||||||
return render_template(
|
return render_template('index.html', artifact_urls=[f'/{artifact_name}/GalacticFactory' for artifact_name in config.artifact_names])
|
||||||
"index.html",
|
|
||||||
artifact_urls=[
|
|
||||||
f"/{artifact_name}/GalacticFactory"
|
|
||||||
for artifact_name in config.artifact_names
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
def handle_sigterm(*args):
|
||||||
def handle_sigterm(*args): # pylint: disable=unused-argument
|
|
||||||
print("Received SIGTERM, exiting gracefully...")
|
print("Received SIGTERM, exiting gracefully...")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
signal.signal(signal.SIGTERM, handle_sigterm)
|
signal.signal(signal.SIGTERM, handle_sigterm)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == '__main__':
|
||||||
from hypercorn.asyncio import serve
|
from hypercorn.asyncio import serve
|
||||||
from hypercorn.config import Config as HypercornConfig
|
from hypercorn.config import Config as HypercornConfig
|
||||||
|
|
||||||
h_config = HypercornConfig()
|
h_config = HypercornConfig()
|
||||||
h_config.bind = ["0.0.0.0:80"]
|
h_config.bind = ["0.0.0.0:80"]
|
||||||
asyncio.run(serve(app=app, config=h_config))
|
asyncio.run(serve(app=app, config=h_config))
|
||||||
|
|
Loading…
Add table
Reference in a new issue