Skip to content

Commit c2ab498

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 c2ab498

File tree

3 files changed

+238
-0
lines changed

3 files changed

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