gitea-actions-badges/api.py

58 lines
1.7 KiB
Python
Raw Normal View History

2023-09-15 18:12:06 +01:00
from fastapi import FastAPI, HTTPException, Security, status
from fastapi.security import APIKeyHeader
from fastapi.responses import RedirectResponse
import yaml
app = FastAPI()
api_key_header = APIKeyHeader(name="X-API-Key")
with open("keys.yaml") as f:
api_keys = yaml.safe_load(f)
try:
with open("badges.yaml") as f:
badge_dict = yaml.safe_load(f)
except FileNotFoundError:
badge_dict = dict()
def get_api_key(api_key_header: str = Security(api_key_header)) -> str:
if api_key_header in api_keys:
return api_key_header
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or missing API Key",
)
@app.get("/")
async def root():
return
@app.get("/set_badge/")
2023-09-25 20:39:51 +01:00
async def set_badge(repo: str, new_badge: str, branch: str=None, action: str=None, api_key: str = Security(get_api_key)):
2023-09-15 18:12:06 +01:00
if api_key.rsplit("-", 1)[0] != repo:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="No permissions for repo",
)
2023-09-25 20:39:51 +01:00
if branch is not None or action is not None:
badge_dict[str((repo, branch, action))] = new_badge
2023-09-16 16:38:48 +01:00
else:
badge_dict[repo] = new_badge
2023-09-15 18:12:06 +01:00
with open("badges.yaml", "w") as f:
yaml.safe_dump(badge_dict, f)
@app.get("/get_badge/")
2023-09-25 20:39:51 +01:00
async def get_badge(repo: str, branch: str=None, action: str=None):
if str((repo, branch, action)) not in badge_dict.keys()) and repo not in badge_dict.keys():
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
2023-09-25 20:57:21 +01:00
detail="Badge not found",
2023-09-15 18:12:06 +01:00
)
return RedirectResponse("https://img.shields.io/badge/" + (badge_dict[str((repo, branch, action))] if branch is not None or action is not None else badge_dict[repo]))