-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Google Sheets integration for GitHub user verification (#4671)
Co-authored-by: openhands <[email protected]> Co-authored-by: Graham Neubig <[email protected]>
- Loading branch information
1 parent
adf7ab5
commit b27fabe
Showing
4 changed files
with
172 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import List | ||
|
||
from google.auth import default | ||
from googleapiclient.discovery import build | ||
from googleapiclient.errors import HttpError | ||
|
||
from openhands.core.logger import openhands_logger as logger | ||
|
||
|
||
class GoogleSheetsClient: | ||
def __init__(self): | ||
"""Initialize Google Sheets client using workload identity. | ||
Uses application default credentials which supports workload identity when running in GCP. | ||
""" | ||
logger.info('Initializing Google Sheets client with workload identity') | ||
try: | ||
credentials, project = default( | ||
scopes=['https://www.googleapis.com/auth/spreadsheets.readonly'] | ||
) | ||
logger.info(f'Successfully obtained credentials for project: {project}') | ||
self.service = build('sheets', 'v4', credentials=credentials) | ||
logger.info('Successfully initialized Google Sheets API service') | ||
except Exception as e: | ||
logger.error(f'Failed to initialize Google Sheets client: {str(e)}') | ||
self.service = None | ||
|
||
def get_usernames(self, spreadsheet_id: str, range_name: str = 'A:A') -> List[str]: | ||
"""Get list of usernames from specified Google Sheet. | ||
Args: | ||
spreadsheet_id: The ID of the Google Sheet | ||
range_name: The A1 notation of the range to fetch | ||
Returns: | ||
List of usernames from the sheet | ||
""" | ||
if not self.service: | ||
logger.error('Google Sheets service not initialized') | ||
return [] | ||
|
||
try: | ||
logger.info( | ||
f'Fetching usernames from sheet {spreadsheet_id}, range {range_name}' | ||
) | ||
result = ( | ||
self.service.spreadsheets() | ||
.values() | ||
.get(spreadsheetId=spreadsheet_id, range=range_name) | ||
.execute() | ||
) | ||
|
||
values = result.get('values', []) | ||
usernames = [ | ||
str(cell[0]).strip() for cell in values if cell and cell[0].strip() | ||
] | ||
logger.info( | ||
f'Successfully fetched {len(usernames)} usernames from Google Sheet' | ||
) | ||
return usernames | ||
|
||
except HttpError as err: | ||
logger.error(f'Error accessing Google Sheet {spreadsheet_id}: {err}') | ||
return [] | ||
except Exception as e: | ||
logger.error( | ||
f'Unexpected error accessing Google Sheet {spreadsheet_id}: {str(e)}' | ||
) | ||
return [] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters