Skip to content

Commit

Permalink
hotfix(backend): Remove GH header token middleware (#6269)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanape authored Jan 14, 2025
1 parent 4da812c commit 04382b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
2 changes: 0 additions & 2 deletions openhands/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import openhands.agenthub # noqa F401 (we import this to get the agents registered)
from openhands.server.middleware import (
AttachConversationMiddleware,
GitHubTokenMiddleware,
InMemoryRateLimiter,
LocalhostCORSMiddleware,
NoCacheMiddleware,
Expand Down Expand Up @@ -45,7 +44,6 @@ async def _lifespan(app: FastAPI):
allow_headers=['*'],
)

app.add_middleware(GitHubTokenMiddleware)
app.add_middleware(NoCacheMiddleware)
app.add_middleware(
RateLimitMiddleware, rate_limiter=InMemoryRateLimiter(requests=10, seconds=1)
Expand Down
13 changes: 0 additions & 13 deletions openhands/server/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,3 @@ async def __call__(self, request: Request, call_next: Callable):
await self._detach_session(request)

return response


class GitHubTokenMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
if request.url.path.startswith('/api/github'):
github_token = request.headers.get('X-GitHub-Token')
if not github_token:
return JSONResponse(
status_code=400,
content={'error': 'Missing X-GitHub-Token header'},
)
request.state.github_token = github_token
return await call_next(request)
30 changes: 21 additions & 9 deletions openhands/server/routes/github.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import requests
from fastapi import APIRouter, HTTPException, Request
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import JSONResponse

from openhands.server.shared import openhands_config
Expand All @@ -8,13 +8,23 @@
app = APIRouter(prefix='/api/github')


def require_github_token(request: Request):
github_token = request.headers.get('X-GitHub-Token')
if not github_token:
raise HTTPException(
status_code=400,
detail='Missing X-GitHub-Token header',
)
return github_token


@app.get('/repositories')
async def get_github_repositories(
request: Request,
page: int = 1,
per_page: int = 10,
sort: str = 'pushed',
installation_id: int | None = None,
github_token: str = Depends(require_github_token),
):
openhands_config.verify_github_repo_list(installation_id)

Expand All @@ -33,7 +43,7 @@ async def get_github_repositories(
params['sort'] = sort

# Set the authorization header with the GitHub token
headers = generate_github_headers(request.state.github_token)
headers = generate_github_headers(github_token)

# Fetch repositories from GitHub
try:
Expand All @@ -59,8 +69,8 @@ async def get_github_repositories(


@app.get('/user')
async def get_github_user(request: Request):
headers = generate_github_headers(request.state.github_token)
async def get_github_user(github_token: str = Depends(require_github_token)):
headers = generate_github_headers(github_token)
try:
response = await call_sync_from_async(
requests.get, 'https://api.github.com/user', headers=headers
Expand All @@ -79,8 +89,10 @@ async def get_github_user(request: Request):


@app.get('/installations')
async def get_github_installation_ids(request: Request):
headers = generate_github_headers(request.state.github_token)
async def get_github_installation_ids(
github_token: str = Depends(require_github_token),
):
headers = generate_github_headers(github_token)
try:
response = await call_sync_from_async(
requests.get, 'https://api.github.com/user/installations', headers=headers
Expand All @@ -102,13 +114,13 @@ async def get_github_installation_ids(request: Request):

@app.get('/search/repositories')
async def search_github_repositories(
request: Request,
query: str,
per_page: int = 5,
sort: str = 'stars',
order: str = 'desc',
github_token: str = Depends(require_github_token),
):
headers = generate_github_headers(request.state.github_token)
headers = generate_github_headers(github_token)
params = {
'q': query,
'per_page': per_page,
Expand Down

0 comments on commit 04382b2

Please sign in to comment.