2021-12-02 10:21:39 +00:00
|
|
|
import logging
|
|
|
|
from typing import Optional
|
|
|
|
import giteapy
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Header, Query, HTTPException
|
|
|
|
|
2022-03-13 04:50:33 +00:00
|
|
|
from core.storage import Storage
|
|
|
|
|
|
|
|
storage = Storage("gitea")
|
2021-12-02 10:21:39 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger("gitea")
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
@router.get("/gitea/latest")
|
|
|
|
def get_latest_version(base_url: Optional[str] = Header(None), author: str = Query(None), repo: str = Query(None), pre_releases: bool = Query(False)):
|
2022-03-13 04:50:33 +00:00
|
|
|
data = storage.read_file("keys.json")
|
2021-12-02 10:21:39 +00:00
|
|
|
if base_url in data.keys():
|
|
|
|
config = giteapy.Configuration()
|
|
|
|
config.api_key['access_token'] = data[base_url]
|
|
|
|
config.host = base_url + "/api/v1"
|
|
|
|
api = giteapy.RepositoryApi(giteapy.ApiClient(config))
|
|
|
|
response = api.repo_list_releases(author, repo)
|
|
|
|
if len(response) == 0:
|
|
|
|
raise HTTPException(404, "No releases for given repository.")
|
|
|
|
|
|
|
|
for release in response:
|
|
|
|
release: giteapy.Release
|
|
|
|
if pre_releases:
|
|
|
|
return release.name
|
|
|
|
elif not pre_releases and release.prerelease:
|
|
|
|
continue
|
|
|
|
elif not pre_releases and not release.prerelease:
|
|
|
|
return release.name
|
|
|
|
else:
|
|
|
|
raise HTTPException(400, "API cannot access this Gitea instance.")
|