From 233a32d8997737ec4ef1f9c22de1e63159aab67b Mon Sep 17 00:00:00 2001 From: Robert Brennan Date: Mon, 11 Nov 2024 17:37:06 -0500 Subject: [PATCH] update to use github client lib --- openhands/server/github.py | 31 +++++++++---------------- poetry.lock | 47 +++++++++++++++++++++++++++++++++++++- pyproject.toml | 1 + 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/openhands/server/github.py b/openhands/server/github.py index ef586e05af4c..ed025f13423e 100644 --- a/openhands/server/github.py +++ b/openhands/server/github.py @@ -1,10 +1,12 @@ import os -import httpx +from github import Github +from github.GithubException import GithubException from tenacity import retry, stop_after_attempt, wait_exponential from openhands.core.logger import openhands_logger as logger from openhands.server.sheets_client import GoogleSheetsClient +from openhands.utils.async_utils import call_sync_from_async GITHUB_CLIENT_ID = os.getenv('GITHUB_CLIENT_ID', '').strip() GITHUB_CLIENT_SECRET = os.getenv('GITHUB_CLIENT_SECRET', '').strip() @@ -113,24 +115,13 @@ async def get_github_user(token: str) -> str: github handle of the user """ logger.info('Fetching GitHub user info from token') - headers = { - 'Accept': 'application/vnd.github+json', - 'Authorization': f'Bearer {token}', - } - async with httpx.AsyncClient( - timeout=httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=5.0) - ) as client: - try: - response = await client.get('https://api.github.com/user', headers=headers) - except httpx.RequestError as e: - logger.error(f'Error making request to GitHub API: {str(e)}') - logger.error(e) - raise - - logger.info('Received response from GitHub API') - logger.debug(f'Response status code: {response.status_code}') - response.raise_for_status() - user_data = response.json() - login = user_data.get('login') + try: + g = Github(token) + user = await call_sync_from_async(g.get_user) + login = user.login logger.info(f'Successfully retrieved GitHub user: {login}') return login + except GithubException as e: + logger.error(f'Error making request to GitHub API: {str(e)}') + logger.error(e) + raise diff --git a/poetry.lock b/poetry.lock index 4f2fb82488b9..60be51e9cae6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -6807,6 +6807,25 @@ files = [ {file = "pyflakes-3.2.0.tar.gz", hash = "sha256:1c61603ff154621fb2a9172037d84dca3500def8c8b630657d1701f026f8af3f"}, ] +[[package]] +name = "pygithub" +version = "2.5.0" +description = "Use the full Github API v3" +optional = false +python-versions = ">=3.8" +files = [ + {file = "PyGithub-2.5.0-py3-none-any.whl", hash = "sha256:b0b635999a658ab8e08720bdd3318893ff20e2275f6446fcf35bf3f44f2c0fd2"}, + {file = "pygithub-2.5.0.tar.gz", hash = "sha256:e1613ac508a9be710920d26eb18b1905ebd9926aa49398e88151c1b526aad3cf"}, +] + +[package.dependencies] +Deprecated = "*" +pyjwt = {version = ">=2.4.0", extras = ["crypto"]} +pynacl = ">=1.4.0" +requests = ">=2.14.0" +typing-extensions = ">=4.0.0" +urllib3 = ">=1.26.0" + [[package]] name = "pygments" version = "2.18.0" @@ -6871,6 +6890,32 @@ files = [ [package.dependencies] pybind11 = ">=2.2" +[[package]] +name = "pynacl" +version = "1.5.0" +description = "Python binding to the Networking and Cryptography (NaCl) library" +optional = false +python-versions = ">=3.6" +files = [ + {file = "PyNaCl-1.5.0-cp36-abi3-macosx_10_10_universal2.whl", hash = "sha256:401002a4aaa07c9414132aaed7f6836ff98f59277a234704ff66878c2ee4a0d1"}, + {file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_24_aarch64.whl", hash = "sha256:52cb72a79269189d4e0dc537556f4740f7f0a9ec41c1322598799b0bdad4ef92"}, + {file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a36d4a9dda1f19ce6e03c9a784a2921a4b726b02e1c736600ca9c22029474394"}, + {file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl", hash = "sha256:0c84947a22519e013607c9be43706dd42513f9e6ae5d39d3613ca1e142fba44d"}, + {file = "PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:06b8f6fa7f5de8d5d2f7573fe8c863c051225a27b61e6860fd047b1775807858"}, + {file = "PyNaCl-1.5.0-cp36-abi3-musllinux_1_1_aarch64.whl", hash = "sha256:a422368fc821589c228f4c49438a368831cb5bbc0eab5ebe1d7fac9dded6567b"}, + {file = "PyNaCl-1.5.0-cp36-abi3-musllinux_1_1_x86_64.whl", hash = "sha256:61f642bf2378713e2c2e1de73444a3778e5f0a38be6fee0fe532fe30060282ff"}, + {file = "PyNaCl-1.5.0-cp36-abi3-win32.whl", hash = "sha256:e46dae94e34b085175f8abb3b0aaa7da40767865ac82c928eeb9e57e1ea8a543"}, + {file = "PyNaCl-1.5.0-cp36-abi3-win_amd64.whl", hash = "sha256:20f42270d27e1b6a29f54032090b972d97f0a1b0948cc52392041ef7831fee93"}, + {file = "PyNaCl-1.5.0.tar.gz", hash = "sha256:8ac7448f09ab85811607bdd21ec2464495ac8b7c66d146bf545b0f08fb9220ba"}, +] + +[package.dependencies] +cffi = ">=1.4.1" + +[package.extras] +docs = ["sphinx (>=1.6.5)", "sphinx-rtd-theme"] +tests = ["hypothesis (>=3.27.0)", "pytest (>=3.2.1,!=3.3.0)"] + [[package]] name = "pyparsing" version = "3.2.0" @@ -10179,4 +10224,4 @@ testing = ["coverage[toml]", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "70158478f99a3e3e3356b9cfcd3bc5a156257ce7adebf6d2f5ffe8c9eff5d4a7" +content-hash = "2c52184376e4637e8033f27d6c8be006deb7b041f3aacf921a2205e00ec9464a" diff --git a/pyproject.toml b/pyproject.toml index 22360528bc69..b1182771f5a3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,6 +63,7 @@ opentelemetry-exporter-otlp-proto-grpc = "1.25.0" modal = "^0.64.145" runloop-api-client = "0.7.0" slowapi = "^0.1.9" +pygithub = "^2.5.0" [tool.poetry.group.llama-index.dependencies] llama-index = "*"