Skip to content

Commit 904c6f9

Browse files
committed
chore: Enable Gemini CLI Github Actions
1 parent c34e384 commit 904c6f9

4 files changed

Lines changed: 947 additions & 0 deletions

File tree

.github/workflows/gemini-cli.yml

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

0 commit comments

Comments
 (0)