Skip to content

Commit de6af2a

Browse files
committed
feat: gemini-cli GitHub Actions
1 parent 28e4c10 commit de6af2a

4 files changed

Lines changed: 1031 additions & 0 deletions

File tree

.github/workflows/gemini-cli.yml

Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
1+
name: '💬 Gemini CLI'
2+
3+
on:
4+
pull_request_review_comment:
5+
types:
6+
- 'created'
7+
pull_request_review:
8+
types:
9+
- 'submitted'
10+
issue_comment:
11+
types:
12+
- 'created'
13+
14+
concurrency:
15+
group: '${{ github.workflow }}-${{ github.event.issue.number }}'
16+
cancel-in-progress: |-
17+
${{ github.event.sender.type == 'User' && ( github.event.issue.author_association == 'OWNER' || github.event.issue.author_association == 'MEMBER' || github.event.issue.author_association == 'COLLABORATOR') }}
18+
19+
defaults:
20+
run:
21+
shell: 'bash'
22+
23+
permissions:
24+
contents: 'write'
25+
id-token: 'write'
26+
pull-requests: 'write'
27+
issues: 'write'
28+
29+
jobs:
30+
gemini-cli:
31+
# This condition is complex to ensure we only run when explicitly invoked.
32+
if: |-
33+
github.event_name == 'workflow_dispatch' ||
34+
(
35+
github.event_name == 'issues' && github.event.action == 'opened' &&
36+
contains(github.event.issue.body, '@gemini-cli') &&
37+
!contains(github.event.issue.body, '/review') &&
38+
!contains(github.event.issue.body, '/triage') &&
39+
(
40+
github.event.sender.type == 'User' && (
41+
github.event.issue.author_association == 'OWNER' ||
42+
github.event.issue.author_association == 'MEMBER' ||
43+
github.event.issue.author_association == 'COLLABORATOR'
44+
)
45+
)
46+
) ||
47+
(
48+
github.event_name == 'issue_comment' &&
49+
contains(github.event.comment.body, '@gemini-cli') &&
50+
!contains(github.event.comment.body, '/review') &&
51+
!contains(github.event.comment.body, '/triage') &&
52+
(
53+
github.event.sender.type == 'User' && (
54+
github.event.comment.author_association == 'OWNER' ||
55+
github.event.comment.author_association == 'MEMBER' ||
56+
github.event.comment.author_association == 'COLLABORATOR'
57+
)
58+
)
59+
) ||
60+
(
61+
github.event_name == 'pull_request_review' &&
62+
contains(github.event.review.body, '@gemini-cli') &&
63+
!contains(github.event.review.body, '/review') &&
64+
!contains(github.event.review.body, '/triage') &&
65+
(
66+
github.event.sender.type == 'User' && (
67+
github.event.review.author_association == 'OWNER' ||
68+
github.event.review.author_association == 'MEMBER' ||
69+
github.event.review.author_association == 'COLLABORATOR'
70+
)
71+
)
72+
) ||
73+
(
74+
github.event_name == 'pull_request_review_comment' &&
75+
contains(github.event.comment.body, '@gemini-cli') &&
76+
!contains(github.event.comment.body, '/review') &&
77+
!contains(github.event.comment.body, '/triage') &&
78+
(
79+
github.event.sender.type == 'User' && (
80+
github.event.comment.author_association == 'OWNER' ||
81+
github.event.comment.author_association == 'MEMBER' ||
82+
github.event.comment.author_association == 'COLLABORATOR'
83+
)
84+
)
85+
)
86+
timeout-minutes: 10
87+
runs-on: 'ubuntu-latest'
88+
89+
steps:
90+
- name: 'Generate GitHub App Token'
91+
id: 'generate_token'
92+
if: |-
93+
${{ vars.APP_ID }}
94+
uses: 'actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e' # ratchet:actions/create-github-app-token@v2
95+
with:
96+
app-id: '${{ vars.APP_ID }}'
97+
private-key: '${{ secrets.APP_PRIVATE_KEY }}'
98+
99+
- name: 'Get context from event'
100+
id: 'get_context'
101+
env:
102+
EVENT_NAME: '${{ github.event_name }}'
103+
EVENT_PAYLOAD: '${{ toJSON(github.event) }}'
104+
run: |-
105+
set -euo pipefail
106+
107+
USER_REQUEST=""
108+
ISSUE_NUMBER=""
109+
IS_PR="false"
110+
111+
if [[ "${EVENT_NAME}" == "issues" ]]; then
112+
USER_REQUEST=$(echo "${EVENT_PAYLOAD}" | jq -r .issue.body)
113+
ISSUE_NUMBER=$(echo "${EVENT_PAYLOAD}" | jq -r .issue.number)
114+
elif [[ "${EVENT_NAME}" == "issue_comment" ]]; then
115+
USER_REQUEST=$(echo "${EVENT_PAYLOAD}" | jq -r .comment.body)
116+
ISSUE_NUMBER=$(echo "${EVENT_PAYLOAD}" | jq -r .issue.number)
117+
if [[ $(echo "${EVENT_PAYLOAD}" | jq -r .issue.pull_request) != "null" ]]; then
118+
IS_PR="true"
119+
fi
120+
elif [[ "${EVENT_NAME}" == "pull_request_review" ]]; then
121+
USER_REQUEST=$(echo "${EVENT_PAYLOAD}" | jq -r .review.body)
122+
ISSUE_NUMBER=$(echo "${EVENT_PAYLOAD}" | jq -r .pull_request.number)
123+
IS_PR="true"
124+
elif [[ "${EVENT_NAME}" == "pull_request_review_comment" ]]; then
125+
USER_REQUEST=$(echo "${EVENT_PAYLOAD}" | jq -r .comment.body)
126+
ISSUE_NUMBER=$(echo "${EVENT_PAYLOAD}" | jq -r .pull_request.number)
127+
IS_PR="true"
128+
fi
129+
130+
# Clean up user request
131+
USER_REQUEST=$(echo "${USER_REQUEST}" | sed 's/.*@gemini-cli//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
132+
133+
{
134+
echo "user_request=${USER_REQUEST}"
135+
echo "issue_number=${ISSUE_NUMBER}"
136+
echo "is_pr=${IS_PR}"
137+
} >> "${GITHUB_OUTPUT}"
138+
139+
- name: 'Set up git user for commits'
140+
run: |-
141+
git config --global user.name 'gemini-cli[bot]'
142+
git config --global user.email 'gemini-cli[bot]@users.noreply.github.com'
143+
144+
- name: 'Checkout PR branch'
145+
if: |-
146+
${{ steps.get_context.outputs.is_pr == 'true' }}
147+
uses: 'actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683' # ratchet:actions/checkout@v4
148+
with:
149+
token: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN }}'
150+
repository: '${{ github.repository }}'
151+
ref: 'refs/pull/${{ steps.get_context.outputs.issue_number }}/head'
152+
fetch-depth: 0
153+
154+
- name: 'Checkout main branch'
155+
if: |-
156+
${{ steps.get_context.outputs.is_pr == 'false' }}
157+
uses: 'actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683' # ratchet:actions/checkout@v4
158+
with:
159+
token: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN }}'
160+
repository: '${{ github.repository }}'
161+
fetch-depth: 0
162+
163+
- name: 'Acknowledge request'
164+
env:
165+
GITHUB_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN }}'
166+
ISSUE_NUMBER: '${{ steps.get_context.outputs.issue_number }}'
167+
REPOSITORY: '${{ github.repository }}'
168+
REQUEST_TYPE: '${{ steps.get_context.outputs.request_type }}'
169+
run: |-
170+
set -euo pipefail
171+
MESSAGE="I've received your request and I'm working on it now! 🤖"
172+
if [[ -n "${MESSAGE}" ]]; then
173+
gh issue comment "${ISSUE_NUMBER}" \
174+
--body "${MESSAGE}" \
175+
--repo "${REPOSITORY}"
176+
fi
177+
178+
- name: 'Get description'
179+
id: 'get_description'
180+
env:
181+
GITHUB_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN }}'
182+
IS_PR: '${{ steps.get_context.outputs.is_pr }}'
183+
ISSUE_NUMBER: '${{ steps.get_context.outputs.issue_number }}'
184+
run: |-
185+
set -euo pipefail
186+
if [[ "${IS_PR}" == "true" ]]; then
187+
DESCRIPTION=$(gh pr view "${ISSUE_NUMBER}" --json body --template '{{.body}}')
188+
else
189+
DESCRIPTION=$(gh issue view "${ISSUE_NUMBER}" --json body --template '{{.body}}')
190+
fi
191+
{
192+
echo "description<<EOF"
193+
echo "${DESCRIPTION}"
194+
echo "EOF"
195+
} >> "${GITHUB_OUTPUT}"
196+
197+
- name: 'Get comments'
198+
id: 'get_comments'
199+
env:
200+
GITHUB_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN }}'
201+
IS_PR: '${{ steps.get_context.outputs.is_pr }}'
202+
ISSUE_NUMBER: '${{ steps.get_context.outputs.issue_number }}'
203+
run: |-
204+
set -euo pipefail
205+
if [[ "${IS_PR}" == "true" ]]; then
206+
COMMENTS=$(gh pr view "${ISSUE_NUMBER}" --json comments --template '{{range .comments}}{{.author.login}}: {{.body}}{{"\n"}}{{end}}')
207+
else
208+
COMMENTS=$(gh issue view "${ISSUE_NUMBER}" --json comments --template '{{range .comments}}{{.author.login}}: {{.body}}{{"\n"}}{{end}}')
209+
fi
210+
{
211+
echo "comments<<EOF"
212+
echo "${COMMENTS}"
213+
echo "EOF"
214+
} >> "${GITHUB_OUTPUT}"
215+
216+
- name: 'Run Gemini'
217+
id: 'run_gemini'
218+
uses: 'google-github-actions/run-gemini-cli@v0'
219+
env:
220+
GITHUB_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN }}'
221+
REPOSITORY: '${{ github.repository }}'
222+
USER_REQUEST: '${{ steps.get_context.outputs.user_request }}'
223+
ISSUE_NUMBER: '${{ steps.get_context.outputs.issue_number }}'
224+
IS_PR: '${{ steps.get_context.outputs.is_pr }}'
225+
with:
226+
gemini_api_key: '${{ secrets.GEMINI_API_KEY }}'
227+
gcp_workload_identity_provider: '${{ vars.GCP_WIF_PROVIDER }}'
228+
gcp_project_id: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
229+
gcp_location: '${{ vars.GOOGLE_CLOUD_LOCATION }}'
230+
gcp_service_account: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'
231+
use_vertex_ai: '${{ vars.GOOGLE_GENAI_USE_VERTEXAI }}'
232+
use_gemini_code_assist: '${{ vars.GOOGLE_GENAI_USE_GCA }}'
233+
settings: |-
234+
{
235+
"maxSessionTurns": 50,
236+
"telemetry": {
237+
"enabled": false,
238+
"target": "gcp"
239+
}
240+
}
241+
prompt: |-
242+
## Role
243+
244+
You are a helpful AI assistant invoked via a CLI interface in a GitHub workflow. You have access to tools to interact with the repository and respond to the user.
245+
246+
## Context
247+
248+
- **Repository**: `${{ github.repository }}`
249+
- **Triggering Event**: `${{ github.event_name }}`
250+
- **Issue/PR Number**: `${{ steps.get_context.outputs.issue_number }}`
251+
- **Is this a PR?**: `${{ steps.get_context.outputs.is_pr }}`
252+
- **Issue/PR Description**:
253+
`${{ steps.get_description.outputs.description }}`
254+
- **Comments**:
255+
`${{ steps.get_comments.outputs.comments }}`
256+
257+
## User Request
258+
259+
The user has sent the following request:
260+
`${{ steps.get_context.outputs.user_request }}`
261+
262+
## How to Respond to Issues, PR Comments, and Questions
263+
264+
This workflow supports three main scenarios:
265+
266+
1. **Creating a Fix for an Issue**
267+
- Carefully read the user request and the related issue or PR description.
268+
- Use available tools to gather all relevant context (e.g., `gh issue view`, `gh pr view`, `gh pr diff`, `cat`, `head`, `tail`).
269+
- Identify the root cause of the problem before proceeding.
270+
- **Show and maintain a plan as a checklist**:
271+
- At the very beginning, outline the steps needed to resolve the issue or address the request and post them as a checklist comment on the issue or PR (use GitHub markdown checkboxes: `- [ ] Task`).
272+
- Example:
273+
```
274+
### Plan
275+
- [ ] Investigate the root cause
276+
- [ ] Implement the fix in `file.py`
277+
- [ ] Add/modify tests
278+
- [ ] Update documentation
279+
- [ ] Verify the fix and close the issue
280+
```
281+
- Use: `gh pr comment "${ISSUE_NUMBER}" --body "<plan>"` or `gh issue comment "${ISSUE_NUMBER}" --body "<plan>"` to post the initial plan.
282+
- As you make progress, keep the checklist visible and up to date by editing the same comment (check off completed tasks with `- [x]`).
283+
- To update the checklist:
284+
1. Find the comment ID for the checklist (use `gh pr comment list "${ISSUE_NUMBER}"` or `gh issue comment list "${ISSUE_NUMBER}"`).
285+
2. Edit the comment with the updated checklist:
286+
- For PRs: `gh pr comment --edit <comment-id> --body "<updated plan>"`
287+
- For Issues: `gh issue comment --edit <comment-id> --body "<updated plan>"`
288+
3. The checklist should only be maintained as a comment on the issue or PR. Do not track or update the checklist in code files.
289+
- If the fix requires code changes, determine which files and lines are affected. If clarification is needed, note any questions for the user.
290+
- Make the necessary code or documentation changes using the available tools (e.g., `write_file`). Ensure all changes follow project conventions and best practices. Reference all shell variables as `"${VAR}"` (with quotes and braces) to prevent errors.
291+
- Run any relevant tests or checks to verify the fix works as intended. If possible, provide evidence (test output, screenshots, etc.) that the issue is resolved.
292+
- **Branching and Committing**:
293+
- **NEVER commit directly to the `main` branch.**
294+
- If you are working on a **pull request** (`IS_PR` is `true`), the correct branch is already checked out. Simply commit and push to it.
295+
- `git add .`
296+
- `git commit -m "feat: <describe the change>"`
297+
- `git push`
298+
- If you are working on an **issue** (`IS_PR` is `false`), create a new branch for your changes. A good branch name would be `issue/${ISSUE_NUMBER}/<short-description>`.
299+
- `git checkout -b issue/${ISSUE_NUMBER}/my-fix`
300+
- `git add .`
301+
- `git commit -m "feat: <describe the fix>"`
302+
- `git push origin issue/${ISSUE_NUMBER}/my-fix`
303+
- After pushing, you can create a pull request: `gh pr create --title "Fixes #${ISSUE_NUMBER}: <short title>" --body "This PR addresses issue #${ISSUE_NUMBER}."`
304+
- Summarize what was changed and why in a markdown file: `write_file("response.md", "<your response here>")`
305+
- Post the response as a comment:
306+
- For PRs: `gh pr comment "${ISSUE_NUMBER}" --body-file response.md`
307+
- For Issues: `gh issue comment "${ISSUE_NUMBER}" --body-file response.md`
308+
309+
2. **Addressing Comments on a Pull Request**
310+
- Read the specific comment and the context of the PR.
311+
- Use tools like `gh pr view`, `gh pr diff`, and `cat` to understand the code and discussion.
312+
- If the comment requests a change or clarification, follow the same process as for fixing an issue: create a checklist plan, implement, test, and commit any required changes, updating the checklist as you go.
313+
- **Committing Changes**: The correct PR branch is already checked out. Simply add, commit, and push your changes.
314+
- `git add .`
315+
- `git commit -m "fix: address review comments"`
316+
- `git push`
317+
- If the comment is a question, answer it directly and clearly, referencing code or documentation as needed.
318+
- Document your response in `response.md` and post it as a PR comment: `gh pr comment "${ISSUE_NUMBER}" --body-file response.md`
319+
320+
3. **Answering Any Question on an Issue**
321+
- Read the question and the full issue context using `gh issue view` and related tools.
322+
- Research or analyze the codebase as needed to provide an accurate answer.
323+
- If the question requires code or documentation changes, follow the fix process above, including creating and updating a checklist plan and **creating a new branch for your changes as described in section 1.**
324+
- Write a clear, concise answer in `response.md` and post it as an issue comment: `gh issue comment "${ISSUE_NUMBER}" --body-file response.md`
325+
326+
## Guidelines
327+
328+
- **Be concise and actionable.** Focus on solving the user's problem efficiently.
329+
- **Always commit and push your changes if you modify code or documentation.**
330+
- **If you are unsure about the fix or answer, explain your reasoning and ask clarifying questions.**
331+
- **Follow project conventions and best practices.**

0 commit comments

Comments
 (0)