Make GitHub calls async

This commit is contained in:
Stéphane Bidoul 2021-11-05 09:58:48 +01:00
parent 68929b6ee7
commit 3c1b4b3fb2
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
5 changed files with 26 additions and 24 deletions

View file

@ -12,9 +12,9 @@ classifiers = [
dependencies = [ dependencies = [
"fastapi", "fastapi",
"gunicorn", "gunicorn",
"httpx",
"jinja2", "jinja2",
"kubernetes", "kubernetes",
"requests", # TODO for github, to replace by aiohttp or httpx
"rich", "rich",
"uvicorn", "uvicorn",
] ]

View file

@ -11,6 +11,8 @@ fastapi==0.70.0
google-auth==2.3.3 google-auth==2.3.3
gunicorn==20.1.0 gunicorn==20.1.0
h11==0.12.0 h11==0.12.0
httpcore==0.13.7
httpx==0.20.0
idna==3.3 idna==3.3
Jinja2==3.0.2 Jinja2==3.0.2
kubernetes==19.15.0 kubernetes==19.15.0
@ -24,6 +26,7 @@ python-dateutil==2.8.2
PyYAML==6.0 PyYAML==6.0
requests==2.26.0 requests==2.26.0
requests-oauthlib==1.3.0 requests-oauthlib==1.3.0
rfc3986==1.5.0
rich==10.12.0 rich==10.12.0
rsa==4.7.2 rsa==4.7.2
six==1.16.0 six==1.16.0

View file

@ -79,8 +79,7 @@ async def builds(repo: Optional[str] = None):
) )
async def trigger_branch(repo: str, branch: str): async def trigger_branch(repo: str, branch: str):
"""Trigger build for a branch.""" """Trigger build for a branch."""
# TODO async github call branch_info = await github.get_branch_info(repo, branch)
branch_info = github.get_branch_info(repo, branch)
await controller.deploy_or_delay_start( await controller.deploy_or_delay_start(
repo=branch_info.repo, repo=branch_info.repo,
target_branch=branch_info.name, target_branch=branch_info.name,
@ -95,8 +94,7 @@ async def trigger_branch(repo: str, branch: str):
) )
async def trigger_pull(repo: str, pr: int): async def trigger_pull(repo: str, pr: int):
"""Trigger build for a pull request.""" """Trigger build for a pull request."""
# TODO async github call pull_info = await github.get_pull_info(repo, pr)
pull_info = github.get_pull_info(repo, pr)
await controller.deploy_or_delay_start( await controller.deploy_or_delay_start(
repo=pull_info.repo, repo=pull_info.repo,
target_branch=pull_info.target_branch, target_branch=pull_info.target_branch,

View file

@ -10,7 +10,7 @@ class BranchNotFound(ClientError):
pass pass
class NotFoundOnGithub(ClientError): class NotFoundOnGitHub(ClientError):
pass pass

View file

@ -1,22 +1,23 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any
import requests import httpx
from .exceptions import NotFoundOnGithub from .exceptions import NotFoundOnGitHub
from .settings import settings from .settings import settings
def _github_get(url: str) -> Any: async def _github_get(url: str) -> Any:
async with httpx.AsyncClient() as client:
full_url = f"https://api.github.com{url}" full_url = f"https://api.github.com{url}"
headers = { headers = {
"Accept": "application/vnd.github.v3+json", "Accept": "application/vnd.github.v3+json",
} }
if settings.github_token: if settings.github_token:
headers["Authorization"] = f"token {settings.github_token}" headers["Authorization"] = f"token {settings.github_token}"
response = requests.get(full_url, headers=headers) response = await client.get(full_url, headers=headers)
if response.status_code == 404: if response.status_code == 404:
raise NotFoundOnGithub(f"GitHub URL not found: {full_url}.") raise NotFoundOnGitHub(f"GitHub URL not found: {full_url}.")
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
@ -28,8 +29,8 @@ class BranchInfo:
head_sha: str head_sha: str
def get_branch_info(repo: str, branch: str) -> BranchInfo: async def get_branch_info(repo: str, branch: str) -> BranchInfo:
branch_data = _github_get(f"/repos/{repo}/git/ref/heads/{branch}") branch_data = await _github_get(f"/repos/{repo}/git/ref/heads/{branch}")
return BranchInfo( return BranchInfo(
repo=repo, repo=repo,
name=branch, name=branch,
@ -45,8 +46,8 @@ class PullInfo:
target_branch: str target_branch: str
def get_pull_info(repo: str, pr: int) -> PullInfo: async def get_pull_info(repo: str, pr: int) -> PullInfo:
pr_data = _github_get(f"/repos/{repo}/pulls/{pr}") pr_data = await _github_get(f"/repos/{repo}/pulls/{pr}")
return PullInfo( return PullInfo(
repo=repo, repo=repo,
number=pr, number=pr,