|
| 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 | +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 | +) |
0 commit comments