Skip to content

Commit 0f28a5c

Browse files
genquan9copybara-github
authored andcommitted
feat: Create PR agent for ADK github repo
Automatically generate reasonable PR descriptions. PiperOrigin-RevId: 775780753
1 parent b04a5ce commit 0f28a5c

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
from . import agent
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
# pylint: disable=g-importing-member
16+
17+
import os
18+
19+
from google.adk import Agent
20+
import requests
21+
22+
23+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "")
24+
if not GITHUB_TOKEN:
25+
raise ValueError("GITHUB_TOKEN environment variable not set")
26+
27+
OWNER = os.getenv("OWNER", "google")
28+
REPO = os.getenv("REPO", "adk-python")
29+
30+
31+
def get_github_pr_info_http(pr_number: int) -> str | None:
32+
"""Fetches information for a GitHub Pull Request by sending direct HTTP requests.
33+
34+
Args:
35+
pr_number (int): The number of the Pull Request.
36+
37+
Returns:
38+
pr_message: A string.
39+
"""
40+
base_url = "https://api.github.com"
41+
42+
headers = {
43+
"Accept": "application/vnd.github+json",
44+
"Authorization": f"Bearer {GITHUB_TOKEN}",
45+
"X-GitHub-Api-Version": "2022-11-28",
46+
}
47+
48+
pr_message = ""
49+
50+
# --- 1. Get main PR details ---
51+
pr_url = f"{base_url}/repos/{OWNER}/{REPO}/pulls/{pr_number}"
52+
print(f"Fetching PR details from: {pr_url}")
53+
try:
54+
response = requests.get(pr_url, headers=headers)
55+
response.raise_for_status()
56+
pr_data = response.json()
57+
pr_message += f"The PR title is: {pr_data.get('title')}\n"
58+
except requests.exceptions.HTTPError as e:
59+
print(
60+
f"HTTP Error fetching PR details: {e.response.status_code} - "
61+
f" {e.response.text}"
62+
)
63+
return None
64+
except requests.exceptions.RequestException as e:
65+
print(f"Network or request error fetching PR details: {e}")
66+
return None
67+
except Exception as e: # pylint: disable=broad-except
68+
print(f"An unexpected error occurred: {e}")
69+
return None
70+
71+
# --- 2. Fetching associated commits (paginated) ---
72+
commits_url = pr_data.get(
73+
"commits_url"
74+
) # This URL is provided in the initial PR response
75+
if commits_url:
76+
print("\n--- Associated Commits in this PR: ---")
77+
page = 1
78+
while True:
79+
# GitHub API often uses 'per_page' and 'page' for pagination
80+
params = {
81+
"per_page": 100,
82+
"page": page,
83+
} # Fetch up to 100 commits per page
84+
try:
85+
response = requests.get(commits_url, headers=headers, params=params)
86+
response.raise_for_status()
87+
commits_data = response.json()
88+
89+
if not commits_data: # No more commits
90+
break
91+
92+
pr_message += "The associated commits are:\n"
93+
for commit in commits_data:
94+
message = commit.get("commit", {}).get("message", "").splitlines()[0]
95+
if message:
96+
pr_message += message + "\n"
97+
98+
# Check for 'Link' header to determine if more pages exist
99+
# This is how GitHub's API indicates pagination
100+
if "Link" in response.headers:
101+
link_header = response.headers["Link"]
102+
if 'rel="next"' in link_header:
103+
page += 1 # Move to the next page
104+
else:
105+
break # No more pages
106+
else:
107+
break # No Link header, so probably only one page
108+
109+
except requests.exceptions.HTTPError as e:
110+
print(
111+
f"HTTP Error fetching PR commits (page {page}):"
112+
f" {e.response.status_code} - {e.response.text}"
113+
)
114+
break
115+
except requests.exceptions.RequestException as e:
116+
print(
117+
f"Network or request error fetching PR commits (page {page}): {e}"
118+
)
119+
break
120+
else:
121+
print("Commits URL not found in PR data.")
122+
123+
return pr_message
124+
125+
126+
system_prompt = """
127+
You are a helpful assistant to generate reasonable descriptions for pull requests for software engineers.
128+
129+
The descritions should not be too short (e.g.: less than 3 words), or too long (e.g.: more than 30 words).
130+
131+
The generated description should start with `chore`, `docs`, `feat`, `fix`, `test`, or `refactor`.
132+
`feat` stands for a new feature.
133+
`fix` stands for a bug fix.
134+
`chore`, `docs`, `test`, and `refactor` stand for improvements.
135+
136+
Some good descriptions are:
137+
1. feat: Added implementation for `get_eval_case`, `update_eval_case` and `delete_eval_case` for the local eval sets manager.
138+
2. feat: Provide inject_session_state as public util method.
139+
140+
Some bad descriptions are:
141+
1. fix: This fixes bugs.
142+
2. feat: This is a new feature.
143+
144+
"""
145+
146+
root_agent = Agent(
147+
model="gemini-2.0-flash",
148+
name="github_pr_agent",
149+
description="Generate pull request descriptions for ADK.",
150+
instruction=system_prompt,
151+
)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
# pylint: disable=g-importing-member
16+
17+
import asyncio
18+
import time
19+
20+
import agent
21+
from google.adk.agents.run_config import RunConfig
22+
from google.adk.runners import InMemoryRunner
23+
from google.adk.sessions import Session
24+
from google.genai import types
25+
26+
27+
async def main():
28+
app_name = "adk_pr_app"
29+
user_id_1 = "adk_pr_user"
30+
runner = InMemoryRunner(
31+
agent=agent.root_agent,
32+
app_name=app_name,
33+
)
34+
session_11 = await runner.session_service.create_session(
35+
app_name=app_name, user_id=user_id_1
36+
)
37+
38+
async def run_agent_prompt(session: Session, prompt_text: str):
39+
content = types.Content(
40+
role="user", parts=[types.Part.from_text(text=prompt_text)]
41+
)
42+
final_agent_response_parts = []
43+
async for event in runner.run_async(
44+
user_id=user_id_1,
45+
session_id=session.id,
46+
new_message=content,
47+
run_config=RunConfig(save_input_blobs_as_artifacts=False),
48+
):
49+
if event.content.parts and event.content.parts[0].text:
50+
if event.author == agent.root_agent.name:
51+
final_agent_response_parts.append(event.content.parts[0].text)
52+
print(f"<<<< Agent Final Output: {''.join(final_agent_response_parts)}\n")
53+
54+
pr_message = agent.get_github_pr_info_http(pr_number=1422)
55+
query = "Generate pull request description for " + pr_message
56+
await run_agent_prompt(session_11, query)
57+
58+
59+
if __name__ == "__main__":
60+
start_time = time.time()
61+
print(
62+
"Script start time:",
63+
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(start_time)),
64+
)
65+
print("------------------------------------")
66+
asyncio.run(main())
67+
end_time = time.time()
68+
print("------------------------------------")
69+
print(
70+
"Script end time:",
71+
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(end_time)),
72+
)
73+
print("Total script execution time:", f"{end_time - start_time:.2f} seconds")

0 commit comments

Comments
 (0)