ci: Load discussion data from event content to avoid additional GraphQL API call

PiperOrigin-RevId: 802308383
This commit is contained in:
Xiang (Sean) Zhou
2025-09-02 15:22:17 -07:00
committed by Copybara-Service
parent 408d3dfeb1
commit a503a0c807
3 changed files with 61 additions and 8 deletions
+1 -1
View File
@@ -42,4 +42,4 @@ jobs:
REPO: 'adk-python'
INTERACTIVE: 0
PYTHONPATH: contributing/samples
run: python -m adk_answering_agent.main --discussion_number ${{ github.event.discussion.number }}
run: python -m adk_answering_agent.main --discussion '${{ toJson(github.event.discussion) }}'
@@ -36,6 +36,7 @@ The `main.py` script supports batch processing for ADK oncall team to process di
### Features
* **Single Discussion**: Process a specific discussion by providing its number.
* **Batch Process**: Process the N most recently updated discussions.
* **Direct Discussion Data**: Process a discussion using JSON data directly (optimized for GitHub Actions).
### Running in Batch Script Mode
To run the agent in batch script mode, first set the required environment variables. Then, execute one of the following commands:
@@ -48,13 +49,19 @@ python -m adk_answering_agent.main --discussion_number 27
# Answer the 10 most recent updated discussions
python -m adk_answering_agent.main --recent 10
# Answer a discussion using direct JSON data (saves API calls)
python -m adk_answering_agent.main --discussion '{"number": 27, "title": "How to...", "body": "I need help with...", "author": {"login": "username"}}'
```
---
## GitHub Workflow Mode
The `main.py` script is automatically triggered by GitHub Actions when new discussions are created in the Q&A category. The workflow is configured in `.github/workflows/discussion_answering.yml` and automatically processes discussions using the `--discussion_number` flag.
The `main.py` script is automatically triggered by GitHub Actions when new discussions are created in the Q&A category. The workflow is configured in `.github/workflows/discussion_answering.yml` and automatically processes discussions using the `--discussion` flag with JSON data from the GitHub event payload.
### Optimization
The GitHub Actions workflow passes discussion data directly from `github.event.discussion` using `toJson()`, eliminating the need for additional API calls to fetch discussion information that's already available in the event payload. This makes the workflow faster and more reliable.
---
@@ -1,3 +1,5 @@
"""ADK Answering Agent main script."""
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,10 +16,11 @@
import argparse
import asyncio
import json
import logging
import os
import sys
import time
from typing import Union
from adk_answering_agent import agent
from adk_answering_agent.settings import OWNER
@@ -35,7 +38,9 @@ USER_ID = "adk_answering_user"
logs.setup_adk_logger(level=logging.DEBUG)
async def list_most_recent_discussions(count: int = 1) -> list[int] | None:
async def list_most_recent_discussions(
count: int = 1,
) -> Union[list[int], None]:
"""Fetches a specified number of the most recently updated discussions.
Args:
@@ -98,6 +103,8 @@ def process_arguments():
"Example usage: \n"
"\tpython -m adk_answering_agent.main --recent 10\n"
"\tpython -m adk_answering_agent.main --discussion_number 21\n"
"\tpython -m adk_answering_agent.main --discussion "
'\'{"number": 21, "title": "...", "body": "..."}\'\n'
),
formatter_class=argparse.RawTextHelpFormatter,
)
@@ -118,12 +125,20 @@ def process_arguments():
help="Answer a specific discussion number.",
)
group.add_argument(
"--discussion",
type=str,
metavar="JSON",
help="Answer a discussion using provided JSON data from GitHub event.",
)
return parser.parse_args()
async def main():
args = process_arguments()
discussion_numbers = []
discussion_json_data = None
if args.recent:
fetched_numbers = await list_most_recent_discussions(count=args.recent)
@@ -140,6 +155,21 @@ async def main():
)
return
discussion_numbers = [discussion_number]
elif args.discussion:
try:
discussion_data = json.loads(args.discussion)
discussion_number = discussion_data.get("number")
if not discussion_number:
print("Error: Discussion JSON missing 'number' field.", file=sys.stderr)
return
discussion_numbers = [discussion_number]
# Store the discussion data for later use
discussion_json_data = discussion_data
except json.JSONDecodeError as e:
print(
f"Error: Invalid JSON in --discussion argument: {e}", file=sys.stderr
)
return
print(f"Will try to answer discussions: {discussion_numbers}...")
@@ -156,10 +186,26 @@ async def main():
session = await runner.session_service.create_session(
app_name=APP_NAME, user_id=USER_ID
)
prompt = (
f"Please check discussion #{discussion_number} see if you can help"
" answer the question or provide some information!"
)
# If we have discussion JSON data, include it in the prompt
# to avoid API call
if args.discussion and discussion_json_data:
title = discussion_json_data.get("title", "No title")
body = discussion_json_data.get("body", "No body")
author = discussion_json_data.get("author", {}).get("login", "Unknown")
prompt = (
f"Please help answer this GitHub discussion #{discussion_number}:\n\n"
f"Title: {title}\n\n"
f"Author: {author}\n\n"
f"Body: {body}\n\n"
"Please provide a helpful response based on your knowledge of ADK."
)
else:
prompt = (
f"Please check discussion #{discussion_number} see if you can help"
" answer the question or provide some information!"
)
response = await call_agent_async(runner, USER_ID, session.id, prompt)
print(f"<<<< Agent Final Output: {response}\n")