Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
46 changes: 46 additions & 0 deletions .github/scripts/create_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@
import urllib.request
import sys
import glob
import re

# The agent may only touch Kotlin sources in these directories. Everything else
# (build scripts, Gradle wrapper, workflows, git metadata) stays out of reach,
# because the workflow compiles the result right after this script runs.
EDITABLE_SOURCE_DIRS = (
"maps-app/src/",
"maps-compose/src/",
"maps-compose-utils/src/",
"maps-compose-widgets/src/",
)

def validate_source_path(filepath, repo_root):
"""Returns filepath relative to repo_root, or exits if the agent may not edit it."""
if not isinstance(filepath, str):
print(f"Refusing non-string path: {filepath!r}", file=sys.stderr)
sys.exit(1)
resolved = os.path.realpath(os.path.join(repo_root, filepath))
relative = os.path.relpath(resolved, repo_root).replace(os.sep, "/")
if not relative.endswith(".kt") or not relative.startswith(EDITABLE_SOURCE_DIRS):
print(f"Refusing to edit {filepath!r}: only .kt files under {', '.join(EDITABLE_SOURCE_DIRS)} are allowed.", file=sys.stderr)
sys.exit(1)
return relative

def get_gemini_response(api_key, prompt, json_mode=False):
model = os.getenv("GEMINI_MODEL", "gemini-3.8-flash")
Expand Down Expand Up @@ -77,6 +100,10 @@ def main():
if not api_key:
print("GEMINI_API_KEY not found", file=sys.stderr)
sys.exit(1)

if not re.fullmatch(r"\d+", issue_number):
print(f"Invalid ISSUE_NUMBER: {issue_number!r}", file=sys.stderr)
sys.exit(1)

if not issue_title and not issue_body:
print("Error: ISSUE_TITLE and ISSUE_BODY are empty.", file=sys.stderr)
Expand All @@ -100,6 +127,7 @@ def main():
```

Identify which files need to be modified or created to solve this issue.
Only Kotlin (`.kt`) files under {", ".join(EDITABLE_SOURCE_DIRS)} can be modified or created.
Return a JSON object containing:
- `files_to_modify`: A list of file paths from the provided project files list that must be changed.
- `files_to_create`: A list of new file paths that must be created.
Expand Down Expand Up @@ -129,6 +157,16 @@ def main():
files_to_create = selection_data.get("files_to_create", [])
explanation = selection_data.get("explanation", "")

if not isinstance(files_to_modify, list) or not isinstance(files_to_create, list):
print("files_to_modify and files_to_create must be lists.", file=sys.stderr)
sys.exit(1)

# Validate every path before writing anything, so a bad entry leaves the tree untouched.
repo_root = os.path.realpath(os.getcwd())
files_to_modify = [validate_source_path(p, repo_root) for p in files_to_modify]
files_to_create = [validate_source_path(p, repo_root) for p in files_to_create]
changed_files = []

print(f"Explanation: {explanation}", file=sys.stderr)
print(f"Modifying: {files_to_modify}", file=sys.stderr)
print(f"Creating: {files_to_create}", file=sys.stderr)
Expand Down Expand Up @@ -173,6 +211,7 @@ def main():

with open(filepath, "w") as f:
f.write(updated_content)
changed_files.append(filepath)
print(f"Successfully updated {filepath}", file=sys.stderr)
else:
print(f"Failed to generate changes for {filepath}", file=sys.stderr)
Expand Down Expand Up @@ -208,6 +247,7 @@ def main():
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, "w") as f:
f.write(new_content)
changed_files.append(filepath)
print(f"Successfully created {filepath}", file=sys.stderr)
else:
print(f"Failed to generate content for {filepath}", file=sys.stderr)
Expand Down Expand Up @@ -241,6 +281,12 @@ def main():
if metadata_response:
try:
metadata_data = json.loads(metadata_response)
if not isinstance(metadata_data, dict):
raise ValueError("metadata is not a JSON object")
# The branch is always derived from the issue number, never chosen by the model,
# and the workflow commits only the files validated above.
metadata_data["branch_name"] = f"auto-fix/issue-{issue_number}"
metadata_data["files"] = changed_files
# Write metadata to a file for GitHub Actions to read
with open("pr_metadata.json", "w") as f:
json.dump(metadata_data, f, indent=2)
Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/auto-fix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ jobs:
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
token: ${{ secrets.SYNCED_GITHUB_TOKEN_REPO || secrets.GITHUB_TOKEN }}
# Keep credentials out of .git/config so the generated code compiled below cannot read them.
# Only the push step authenticates.
persist-credentials: false

- name: Set up JDK 17
uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0
Expand Down Expand Up @@ -98,13 +100,15 @@ jobs:
git config --global user.name "googlemaps-bot"
git config --global user.email "googlemaps-bot@google.com"

# Create branch and commit
# Create branch and commit only the files create_pr.py validated
mapfile -t CHANGED_FILES < <(jq -r '.files[]' pr_metadata.json)
git checkout -b "$BRANCH_NAME"
git add .
git add -- "${CHANGED_FILES[@]}"
git commit -m "$COMMIT_MESSAGE"

# Push branch
git push origin "$BRANCH_NAME" --force
gh auth setup-git
git push origin "$BRANCH_NAME"

# Create Pull Request
gh pr create \
Expand Down
Loading