FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
android-maps-compose/.github/scripts/create_pr.py at main · googlemaps/android-maps-compose · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
googlemaps
/
android-maps-compose
Public
Notifications
You must be signed in to change notification settings
Fork
181
Star
1.3k
Code
Issues
92
Pull requests
20
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
android-maps-compose
/
.github
/
scripts
/
create_pr.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
254 lines (220 loc) · 9.33 KB
Breadcrumbs
android-maps-compose
/
.github
/
scripts
/
create_pr.py
Copy path
File metadata and controls
254 lines (220 loc) · 9.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
json
import
urllib
.
request
import
sys
import
glob
def
get_gemini_response
(
api_key
,
prompt
,
json_mode
=
False
):
url
=
f"https://generativelanguage.googleapis.com/v1beta/models/gemini-3.5-flash:generateContent?key=
{
api_key
}
"
headers
=
{
'Content-Type'
:
'application/json'
}
data
=
{
"contents"
: [{
"parts"
: [{
"text"
:
prompt
}]
}]
}
if
json_mode
:
data
[
"generationConfig"
]
=
{
"response_mime_type"
:
"application/json"
}
req
=
urllib
.
request
.
Request
(
url
,
data
=
json
.
dumps
(
data
).
encode
(
'utf-8'
),
headers
=
headers
)
try
:
with
urllib
.
request
.
urlopen
(
req
)
as
response
:
res_data
=
json
.
loads
(
response
.
read
().
decode
(
'utf-8'
))
return
res_data
[
'candidates'
][
0
][
'content'
][
'parts'
][
0
][
'text'
]
except
urllib
.
error
.
HTTPError
as
e
:
print
(
f"Gemini API Error (
{
e
.
code
}
):
{
e
.
reason
}
"
,
file
=
sys
.
stderr
)
try
:
error_body
=
e
.
read
().
decode
(
'utf-8'
)
print
(
f"Error details:
{
error_body
}
"
,
file
=
sys
.
stderr
)
except
:
pass
return
None
except
Exception
as
e
:
print
(
f"Error calling Gemini API:
{
e
}
"
,
file
=
sys
.
stderr
)
return
None
def
list_project_files
():
"""Lists relevant source and configuration files in the project."""
files
=
[]
# Include Kotlin files, build scripts, Manifests, and properties
patterns
=
[
"**/*.kt"
,
"**/*.kts"
,
"**/AndroidManifest.xml"
,
"**/*.properties"
,
"**/*.toml"
]
for
pattern
in
patterns
:
for
filepath
in
glob
.
glob
(
pattern
,
recursive
=
True
):
# Skip build/bin/gradle-related directories
if
any
(
part
in
filepath
.
split
(
os
.
sep
)
for
part
in
[
"build"
,
"bin"
,
".gradle"
,
".git"
,
".antigravitycli"
]):
continue
files
.
append
(
filepath
)
return
files
def
main
():
api_key
=
os
.
getenv
(
"GEMINI_API_KEY"
)
issue_title
=
os
.
getenv
(
"ISSUE_TITLE"
)
issue_body
=
os
.
getenv
(
"ISSUE_BODY"
)
issue_number
=
os
.
getenv
(
"ISSUE_NUMBER"
,
"unknown"
)
if
not
api_key
:
print
(
"GEMINI_API_KEY not found"
,
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
)
sys
.
exit
(
1
)
print
(
"Scanning project structure..."
,
file
=
sys
.
stderr
)
project_files
=
list_project_files
()
files_list_str
=
"
\n
"
.
join
(
project_files
)
# 1. Ask Gemini which files need to be modified or created
selection_prompt
=
f"""
You are an expert software engineer maintaining the `android-maps-compose` codebase.
A user has submitted the following issue:
- **Title**:
{
issue_title
}
- **Body**:
{
issue_body
}
Here is the list of files in the project:
```
{
files_list_str
}
```
Identify which files need to be modified or created to solve this issue.
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.
- `explanation`: A brief explanation of the proposed changes.
Your response must be valid JSON matching the format:
{{
"files_to_modify": ["path/to/file1.kt"],
"files_to_create": ["path/to/newfile.kt"],
"explanation": "Brief explanation of what needs to be changed."
}}
"""
print
(
"Determining which files to edit..."
,
file
=
sys
.
stderr
)
selection_response
=
get_gemini_response
(
api_key
,
selection_prompt
,
json_mode
=
True
)
if
not
selection_response
:
print
(
"Failed to get file selection from Gemini."
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
try
:
selection_data
=
json
.
loads
(
selection_response
)
except
Exception
as
e
:
print
(
f"Failed to parse selection JSON:
{
e
}
\n
Raw response:
{
selection_response
}
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
files_to_modify
=
selection_data
.
get
(
"files_to_modify"
, [])
files_to_create
=
selection_data
.
get
(
"files_to_create"
, [])
explanation
=
selection_data
.
get
(
"explanation"
,
""
)
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
)
# 2. Modify existing files
for
filepath
in
files_to_modify
:
if
not
os
.
path
.
exists
(
filepath
):
print
(
f"File
{
filepath
}
not found, skipping modification."
,
file
=
sys
.
stderr
)
continue
with
open
(
filepath
,
"r"
)
as
f
:
original_content
=
f
.
read
()
edit_prompt
=
f"""
You are an expert software engineer. You are fixing the following issue in the `android-maps-compose` codebase:
- **Issue Title**:
{
issue_title
}
- **Issue Body**:
{
issue_body
}
We need to modify the file `
{
filepath
}
` to resolve this issue.
Here is the current content of `
{
filepath
}
`:
```
{
original_content
}
```
Please generate the COMPLETE updated content of this file, including all necessary imports and logic, ensuring code style, safety, and correctness.
Return ONLY the updated file contents. Do not wrap the response in any markdown code blocks.
"""
print
(
f"Generating changes for
{
filepath
}
..."
,
file
=
sys
.
stderr
)
updated_content
=
get_gemini_response
(
api_key
,
edit_prompt
)
if
updated_content
:
# Clean up wrap
if
updated_content
.
startswith
(
"```"
):
lines
=
updated_content
.
splitlines
()
if
lines
[
0
].
startswith
(
"```"
):
lines
=
lines
[
1
:]
if
lines
and
lines
[
-
1
].
strip
()
==
"```"
:
lines
=
lines
[:
-
1
]
updated_content
=
"
\n
"
.
join
(
lines
)
updated_content
=
updated_content
.
strip
()
+
"
\n
"
with
open
(
filepath
,
"w"
)
as
f
:
f
.
write
(
updated_content
)
print
(
f"Successfully updated
{
filepath
}
"
,
file
=
sys
.
stderr
)
else
:
print
(
f"Failed to generate changes for
{
filepath
}
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
# 3. Create new files
for
filepath
in
files_to_create
:
create_prompt
=
f"""
You are an expert software engineer. We are resolving this issue:
- **Issue Title**:
{
issue_title
}
- **Issue Body**:
{
issue_body
}
We need to create a new file at `
{
filepath
}
`.
Please generate the COMPLETE content of this new file.
Return ONLY the file contents. Do not wrap the response in any markdown code blocks.
"""
print
(
f"Generating content for new file
{
filepath
}
..."
,
file
=
sys
.
stderr
)
new_content
=
get_gemini_response
(
api_key
,
create_prompt
)
if
new_content
:
# Clean up wrap
if
new_content
.
startswith
(
"```"
):
lines
=
new_content
.
splitlines
()
if
lines
[
0
].
startswith
(
"```"
):
lines
=
lines
[
1
:]
if
lines
and
lines
[
-
1
].
strip
()
==
"```"
:
lines
=
lines
[:
-
1
]
new_content
=
"
\n
"
.
join
(
lines
)
new_content
=
new_content
.
strip
()
+
"
\n
"
# Create parent directories if they don't exist
os
.
makedirs
(
os
.
path
.
dirname
(
filepath
),
exist_ok
=
True
)
with
open
(
filepath
,
"w"
)
as
f
:
f
.
write
(
new_content
)
print
(
f"Successfully created
{
filepath
}
"
,
file
=
sys
.
stderr
)
else
:
print
(
f"Failed to generate content for
{
filepath
}
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
# 4. Generate Branch Name, Commit Message, and Pull Request metadata
metadata_prompt
=
f"""
Based on the issue:
- **Title**:
{
issue_title
}
- **Number**:
{
issue_number
}
- **Explanation**:
{
explanation
}
Generate metadata for a Git branch and a Pull Request.
Return a JSON object with:
- `branch_name`: A short, descriptive branch name (e.g. "fix/issue-123-marker-crash").
- `commit_message`: A descriptive commit message following conventional commits (e.g. "fix: resolve crash in marker state calculation").
- `pr_title`: A clear, professional title for the PR.
- `pr_body`: A detailed Markdown description explaining the changes and linking to the issue (e.g. "Fixes #
{
issue_number
}
").
Return valid JSON format:
{{
"branch_name": "fix/...",
"commit_message": "...",
"pr_title": "...",
"pr_body": "..."
}}
"""
print
(
"Generating Git metadata..."
,
file
=
sys
.
stderr
)
metadata_response
=
get_gemini_response
(
api_key
,
metadata_prompt
,
json_mode
=
True
)
if
metadata_response
:
try
:
metadata_data
=
json
.
loads
(
metadata_response
)
# 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
)
print
(
"Successfully wrote pr_metadata.json"
,
file
=
sys
.
stderr
)
except
Exception
as
e
:
print
(
f"Failed to parse metadata JSON:
{
e
}
\n
Raw response:
{
metadata_response
}
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
else
:
sys
.
exit
(
1
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL