Skip to content

Commit 646eb42

Browse files
xuanyang15copybara-github
authored andcommitted
chore: add Github workflow config for the ADK PR triaging agent
PiperOrigin-RevId: 788519884
1 parent bf72426 commit 646eb42

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

.github/workflows/pr-triage.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: ADK Pull Request Triaging Agent
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, edited]
6+
7+
jobs:
8+
agent-triage-pull-request:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
pull-requests: write
12+
contents: read
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: '3.11'
22+
23+
- name: Install dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install requests google-adk
27+
28+
- name: Run Triaging Script
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.ADK_TRIAGE_AGENT }}
31+
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
32+
GOOGLE_GENAI_USE_VERTEXAI: 0
33+
OWNER: 'google'
34+
REPO: 'adk-python'
35+
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
36+
INTERACTIVE: ${{ secrets.PR_TRIAGE_INTERACTIVE }}
37+
PYTHONPATH: contributing/samples
38+
run: python -m adk_pr_triaging_agent.main
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ADK Pull Request Triaging Assistant
2+
3+
The ADK Pull Request (PR) Triaging Assistant is a Python-based agent designed to help manage and triage GitHub pull requests for the `google/adk-python` repository. It uses a large language model to analyze new and unlabelled pull requests, recommend appropriate labels, assign a reviewer, and check contribution guides based on a predefined set of rules.
4+
5+
This agent can be operated in two distinct modes:
6+
7+
* an interactive mode for local use
8+
* a fully automated GitHub Actions workflow.
9+
10+
---
11+
12+
## Interactive Mode
13+
14+
This mode allows you to run the agent locally to review its recommendations in real-time before any changes are made to your repository's pull requests.
15+
16+
### Features
17+
* **Web Interface**: The agent's interactive mode can be rendered in a web browser using the ADK's `adk web` command.
18+
* **User Approval**: In interactive mode, the agent is instructed to ask for your confirmation before applying a label or posting a comment to a GitHub pull request.
19+
20+
### Running in Interactive Mode
21+
To run the agent in interactive mode, first set the required environment variables. Then, execute the following command in your terminal:
22+
23+
```bash
24+
adk web
25+
```
26+
This will start a local server and provide a URL to access the agent's web interface in your browser.
27+
28+
---
29+
30+
## GitHub Workflow Mode
31+
32+
For automated, hands-off PR triaging, the agent can be integrated directly into your repository's CI/CD pipeline using a GitHub Actions workflow.
33+
34+
### Workflow Triggers
35+
The GitHub workflow is configured to run on specific triggers:
36+
37+
* **Pull Request Events**: The workflow executes automatically whenever a new PR is `opened` or an existing one is `reopened` or `edited`.
38+
39+
### Automated Labeling
40+
When running as part of the GitHub workflow, the agent operates non-interactively. It identifies and applies the best label or posts a comment directly without requiring user approval. This behavior is configured by setting the `INTERACTIVE` environment variable to `0` in the workflow file.
41+
42+
### Workflow Configuration
43+
The workflow is defined in a YAML file (`.github/workflows/pr-triage.yml`). This file contains the steps to check out the code, set up the Python environment, install dependencies, and run the triaging script with the necessary environment variables and secrets.
44+
45+
---
46+
47+
## Setup and Configuration
48+
49+
Whether running in interactive or workflow mode, the agent requires the following setup.
50+
51+
### Dependencies
52+
The agent requires the following Python libraries.
53+
54+
```bash
55+
pip install --upgrade pip
56+
pip install google-adk
57+
```
58+
59+
### Environment Variables
60+
The following environment variables are required for the agent to connect to the necessary services.
61+
62+
* `GITHUB_TOKEN`: **(Required)** A GitHub Personal Access Token with `pull_requests:write` permissions. Needed for both interactive and workflow modes.
63+
* `GOOGLE_API_KEY`: **(Required)** Your API key for the Gemini API. Needed for both interactive and workflow modes.
64+
* `OWNER`: The GitHub organization or username that owns the repository (e.g., `google`). Needed for both modes.
65+
* `REPO`: The name of the GitHub repository (e.g., `adk-python`). Needed for both modes.
66+
* `INTERACTIVE`: Controls the agent's interaction mode. For the automated workflow, this is set to `0`. For interactive mode, it should be set to `1` or left unset.
67+
68+
For local execution in interactive mode, you can place these variables in a `.env` file in the project's root directory. For the GitHub workflow, they should be configured as repository secrets.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import asyncio
16+
import time
17+
18+
from adk_pr_triaging_agent import agent
19+
from adk_pr_triaging_agent.settings import OWNER
20+
from adk_pr_triaging_agent.settings import PULL_REQUEST_NUMBER
21+
from adk_pr_triaging_agent.settings import REPO
22+
from adk_pr_triaging_agent.utils import call_agent_async
23+
from adk_pr_triaging_agent.utils import parse_number_string
24+
from google.adk.runners import InMemoryRunner
25+
26+
APP_NAME = "adk_pr_triaging_app"
27+
USER_ID = "adk_pr_triaging_user"
28+
29+
30+
async def main():
31+
runner = InMemoryRunner(
32+
agent=agent.root_agent,
33+
app_name=APP_NAME,
34+
)
35+
session = await runner.session_service.create_session(
36+
app_name=APP_NAME, user_id=USER_ID
37+
)
38+
39+
pr_number = parse_number_string(PULL_REQUEST_NUMBER)
40+
if not pr_number:
41+
print(
42+
f"Error: Invalid pull request number received: {PULL_REQUEST_NUMBER}."
43+
)
44+
return
45+
46+
prompt = f"Please triage pull request #{pr_number}!"
47+
response = await call_agent_async(runner, USER_ID, session.id, prompt)
48+
print(f"<<<< Agent Final Output: {response}\n")
49+
50+
51+
if __name__ == "__main__":
52+
start_time = time.time()
53+
print(
54+
f"Start triaging {OWNER}/{REPO} pull request #{PULL_REQUEST_NUMBER} at"
55+
f" {time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(start_time))}"
56+
)
57+
print("-" * 80)
58+
asyncio.run(main())
59+
print("-" * 80)
60+
end_time = time.time()
61+
print(
62+
"Triaging finished at"
63+
f" {time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(end_time))}",
64+
)
65+
print("Total script execution time:", f"{end_time - start_time:.2f} seconds")

contributing/samples/adk_pr_triaging_agent/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
OWNER = os.getenv("OWNER", "google")
2929
REPO = os.getenv("REPO", "adk-python")
3030
BOT_LABEL = os.getenv("BOT_LABEL", "bot triaged")
31+
PULL_REQUEST_NUMBER = os.getenv("PULL_REQUEST_NUMBER")
3132

3233
IS_INTERACTIVE = os.environ.get("INTERACTIVE", "1").lower() in ["true", "1"]

contributing/samples/adk_pr_triaging_agent/utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import sys
1516
from typing import Any
1617

1718
from adk_pr_triaging_agent.settings import GITHUB_GRAPHQL_URL
1819
from adk_pr_triaging_agent.settings import GITHUB_TOKEN
20+
from google.adk.agents.run_config import RunConfig
21+
from google.adk.runners import Runner
22+
from google.genai import types
1923
import requests
2024

2125
headers = {
@@ -75,3 +79,42 @@ def read_file(file_path: str) -> str:
7579
except FileNotFoundError:
7680
print(f"Error: File not found: {file_path}.")
7781
return ""
82+
83+
84+
def parse_number_string(number_str: str | None, default_value: int = 0) -> int:
85+
"""Parse a number from the given string."""
86+
if not number_str:
87+
return default_value
88+
89+
try:
90+
return int(number_str)
91+
except ValueError:
92+
print(
93+
f"Warning: Invalid number string: {number_str}. Defaulting to"
94+
f" {default_value}.",
95+
file=sys.stderr,
96+
)
97+
return default_value
98+
99+
100+
async def call_agent_async(
101+
runner: Runner, user_id: str, session_id: str, prompt: str
102+
) -> str:
103+
"""Call the agent asynchronously with the user's prompt."""
104+
content = types.Content(
105+
role="user", parts=[types.Part.from_text(text=prompt)]
106+
)
107+
108+
final_response_text = ""
109+
async for event in runner.run_async(
110+
user_id=user_id,
111+
session_id=session_id,
112+
new_message=content,
113+
run_config=RunConfig(save_input_blobs_as_artifacts=False),
114+
):
115+
if event.content and event.content.parts:
116+
if text := "".join(part.text or "" for part in event.content.parts):
117+
if event.author != "user":
118+
final_response_text += text
119+
120+
return final_response_text

0 commit comments

Comments
 (0)