Skip to content

Commit

Permalink
include user id if available
Browse files Browse the repository at this point in the history
  • Loading branch information
malhotra5 committed Jan 31, 2025
1 parent 1675328 commit 4b60c4a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
19 changes: 13 additions & 6 deletions openhands/server/routes/github.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi.responses import JSONResponse

from openhands.server.auth import get_github_token
from openhands.server.auth import get_github_token, get_user_id
from openhands.server.services.github_service import GitHubService
from openhands.server.shared import server_config
from openhands.utils.import_utils import get_impl
Expand Down Expand Up @@ -30,24 +30,30 @@ async def get_github_repositories(
sort: str = 'pushed',
installation_id: int | None = None,
github_token: str = Depends(require_github_token),
github_user_id: str | None = Depends(get_user_id),
):
client = GithubServiceImpl(github_token)
print('got user id ', github_user_id)
client = GithubServiceImpl(github_token, github_user_id)
return await client.fetch_response(
'get_repositories', page, per_page, sort, installation_id
)


@app.get('/user')
async def get_github_user(github_token: str = Depends(require_github_token)):
client = GithubServiceImpl(github_token)
async def get_github_user(
github_token: str = Depends(require_github_token),
github_user_id: str | None = Depends(get_user_id),
):
client = GithubServiceImpl(github_token, github_user_id)
return await client.fetch_response('get_user')


@app.get('/installations')
async def get_github_installation_ids(
github_token: str = Depends(require_github_token),
github_user_id: str | None = Depends(get_user_id),
):
client = GithubServiceImpl(github_token)
client = GithubServiceImpl(github_token, github_user_id)
installations = await client.get_installation_ids()
return JSONResponse(content=[i['id'] for i in installations])

Expand All @@ -59,8 +65,9 @@ async def search_github_repositories(
sort: str = 'stars',
order: str = 'desc',
github_token: str = Depends(require_github_token),
github_user_id: str | None = Depends(get_user_id),
):
client = GithubServiceImpl(github_token)
client = GithubServiceImpl(github_token, github_user_id)
response = await client.search_repositories(query, per_page, sort, order)
json_response = JSONResponse(content=response.json())
response.close()
Expand Down
2 changes: 1 addition & 1 deletion openhands/server/routes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def store_settings(
try:
# We check if the token is valid by getting the user
# If the token is invalid, this will raise an exception
github = GitHubService(settings.github_token)
github = GitHubService(settings.github_token, None)
response = await github.get_user()
if response.status_code != status.HTTP_200_OK:
raise Exception('Invalid Github Token')
Expand Down
7 changes: 4 additions & 3 deletions openhands/server/services/github_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
class GitHubService:
BASE_URL = 'https://api.github.com'

def __init__(self, token: str):
def __init__(self, token: str, user_id: str | None):
self.token = token
self.user_id = user_id
self.headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/vnd.github.v3+json',
Expand All @@ -19,7 +20,7 @@ def __init__(self, token: str):
def _has_token_expired(self, status_code: int):
return status_code == 401

async def _refresh_token(self):
async def _get_latest_token(self):
pass

async def _fetch_data(self, url: str, params: dict | None = None):
Expand All @@ -29,7 +30,7 @@ async def _fetch_data(self, url: str, params: dict | None = None):
if server_config.app_mode == 'SAAS' and self._has_token_expired(
response.status_code
):
await self._refresh_token()
await self._get_latest_token()
response = await client.get(
url, headers=self.headers, params=params
)
Expand Down

0 comments on commit 4b60c4a

Please sign in to comment.