-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathaction.yml
More file actions
416 lines (379 loc) · 15 KB
/
action.yml
File metadata and controls
416 lines (379 loc) · 15 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
name: 'Loki Mode Code Review'
description: 'Multi-agent autonomous code review using loki-mode. Supports Claude, Codex, and Gemini providers.'
author: 'asklokesh'
branding:
icon: 'eye'
color: 'purple'
inputs:
mode:
description: 'Review mode: review, fix, or test'
required: false
default: 'review'
provider:
description: 'AI provider: claude, codex, or gemini'
required: false
default: 'claude'
budget_limit:
description: 'Max cost in USD before stopping (maps to --budget CLI flag)'
required: false
default: '5.00'
budget:
description: 'Alias for budget_limit (max cost in USD before stopping)'
required: false
default: ''
github_token:
description: 'GitHub token for PR comments'
required: true
max_iterations:
description: 'Maximum loki iterations (sets LOKI_MAX_ITERATIONS env var)'
required: false
default: '3'
node_version:
description: 'Node.js version to use'
required: false
default: '20'
prd_file:
description: 'Path to PRD file relative to repo root (optional, used as positional arg to loki start)'
required: false
default: ''
auto_confirm:
description: 'Skip confirmation prompts (auto-enabled in CI environments)'
required: false
default: 'true'
install_cli:
description: 'Automatically install the provider CLI if not present'
required: false
default: 'true'
install_claude:
description: 'Deprecated: use install_cli instead. Kept for backward compatibility.'
required: false
default: ''
runs:
using: 'composite'
steps:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node_version }}
- name: Install loki-mode
shell: bash
run: |
set -eo pipefail
echo "--- Installing loki-mode from npm ---"
npm install -g loki-mode
echo "Installed version: $(loki version 2>/dev/null || echo 'unknown')"
- name: Install provider CLI
if: inputs.install_cli == 'true' || (inputs.install_cli == '' && inputs.install_claude != 'false')
shell: bash
env:
ACTION_PROVIDER: ${{ inputs.provider }}
run: |
set -eo pipefail
case "$ACTION_PROVIDER" in
claude)
if command -v claude &>/dev/null; then
echo "Claude Code CLI already installed: $(claude --version 2>/dev/null || echo 'unknown')"
else
echo "--- Installing Claude Code CLI ---"
npm install -g @anthropic-ai/claude-code
echo "Installed: $(claude --version 2>/dev/null || echo 'unknown')"
fi
;;
codex)
if command -v codex &>/dev/null; then
echo "OpenAI Codex CLI already installed: $(codex --version 2>/dev/null || echo 'unknown')"
else
echo "--- Installing OpenAI Codex CLI ---"
npm install -g @openai/codex
echo "Installed: $(codex --version 2>/dev/null || echo 'unknown')"
fi
;;
gemini)
if command -v gemini &>/dev/null; then
echo "Google Gemini CLI already installed: $(gemini --version 2>/dev/null || echo 'unknown')"
else
echo "--- Installing Google Gemini CLI ---"
npm install -g @google/gemini-cli
echo "Installed: $(gemini --version 2>/dev/null || echo 'unknown')"
fi
;;
*)
echo "::error::Unknown provider '$ACTION_PROVIDER'. Supported: claude, codex, gemini"
exit 1
;;
esac
- name: Verify provider credentials
shell: bash
env:
ACTION_PROVIDER: ${{ inputs.provider }}
run: |
case "$ACTION_PROVIDER" in
claude)
# Claude supports API key or OAuth token (Max subscription)
if [ -n "${ANTHROPIC_API_KEY:-}" ]; then
echo "ANTHROPIC_API_KEY is set"
elif [ -n "${CLAUDE_CODE_OAUTH_TOKEN:-}" ]; then
echo "CLAUDE_CODE_OAUTH_TOKEN is set (Max subscription)"
else
echo "::error::No Claude credentials found. Set ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN as a repository secret."
echo ""
echo "Example workflow configuration:"
echo " - uses: asklokesh/loki-mode@v5"
echo " with:"
echo " github_token: \${{ secrets.GITHUB_TOKEN }}"
echo " provider: claude"
echo " env:"
echo " ANTHROPIC_API_KEY: \${{ secrets.ANTHROPIC_API_KEY }}"
exit 1
fi
;;
codex)
if [ -n "${OPENAI_API_KEY:-}" ]; then
echo "OPENAI_API_KEY is set"
else
echo "::error::OPENAI_API_KEY is not set. Add it as a repository secret and pass it via env: in your workflow."
echo ""
echo "Example workflow configuration:"
echo " - uses: asklokesh/loki-mode@v5"
echo " with:"
echo " github_token: \${{ secrets.GITHUB_TOKEN }}"
echo " provider: codex"
echo " env:"
echo " OPENAI_API_KEY: \${{ secrets.OPENAI_API_KEY }}"
exit 1
fi
;;
gemini)
if [ -n "${GOOGLE_API_KEY:-}" ]; then
echo "GOOGLE_API_KEY is set"
elif [ -n "${GEMINI_API_KEY:-}" ]; then
echo "GEMINI_API_KEY is set"
else
echo "::error::No Gemini credentials found. Set GOOGLE_API_KEY or GEMINI_API_KEY as a repository secret."
echo ""
echo "Example workflow configuration:"
echo " - uses: asklokesh/loki-mode@v5"
echo " with:"
echo " github_token: \${{ secrets.GITHUB_TOKEN }}"
echo " provider: gemini"
echo " env:"
echo " GOOGLE_API_KEY: \${{ secrets.GOOGLE_API_KEY }}"
exit 1
fi
;;
esac
- name: Get PR diff
id: pr-diff
if: inputs.mode == 'review'
shell: bash
env:
GH_TOKEN: ${{ inputs.github_token }}
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
if [ -z "$PR_NUMBER" ]; then
echo "WARNING: No pull request number found. Skipping diff."
echo "has_diff=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "--- Fetching diff for PR #${PR_NUMBER} ---"
DIFF_FILE="${RUNNER_TEMP}/pr-${PR_NUMBER}.diff"
gh pr diff "$PR_NUMBER" > "$DIFF_FILE" 2>/dev/null || true
if [ ! -s "$DIFF_FILE" ]; then
echo "WARNING: Empty diff. PR may have no file changes."
echo "has_diff=false" >> "$GITHUB_OUTPUT"
exit 0
fi
DIFF_LINES=$(wc -l < "$DIFF_FILE" | tr -d ' ')
echo "Diff has ${DIFF_LINES} lines"
echo "has_diff=true" >> "$GITHUB_OUTPUT"
echo "diff_file=${DIFF_FILE}" >> "$GITHUB_OUTPUT"
- name: Generate review PRD
id: review-prd
if: inputs.mode == 'review' && steps.pr-diff.outputs.has_diff == 'true'
shell: bash
env:
GH_TOKEN: ${{ inputs.github_token }}
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
PRD_FILE="${RUNNER_TEMP}/review-prd-${PR_NUMBER}.md"
DIFF_FILE="${{ steps.pr-diff.outputs.diff_file }}"
PR_TITLE=$(gh pr view "$PR_NUMBER" --json title -q '.title' 2>/dev/null || echo "PR #${PR_NUMBER}")
PR_BODY=$(gh pr view "$PR_NUMBER" --json body -q '.body' 2>/dev/null || echo "No description provided.")
# Truncate diff if too large (keep first 500 lines to stay within token limits)
DIFF_CONTENT=$(head -500 "$DIFF_FILE")
DIFF_TOTAL=$(wc -l < "$DIFF_FILE" | tr -d ' ')
TRUNCATED_NOTE=""
if [ "$DIFF_TOTAL" -gt 500 ]; then
TRUNCATED_NOTE="(Showing first 500 of ${DIFF_TOTAL} lines. Focus on the visible changes.)"
fi
{
printf '%s\n' "# Code Review: ${PR_TITLE}"
echo ""
echo "## Objective"
echo "Review PR #${PR_NUMBER} for code quality, bugs, security, and best practices."
echo ""
echo "## PR Description"
printf '%s\n' "${PR_BODY}"
echo ""
echo "## Review Checklist"
echo "- Identify potential bugs, logic errors, or edge cases"
echo "- Check for security vulnerabilities (injection, auth issues, data exposure)"
echo "- Evaluate code readability and maintainability"
echo "- Note any missing error handling or validation"
echo "- Flag performance concerns if applicable"
echo "- Check for test coverage gaps"
echo ""
echo "## Output Format"
echo "Provide a structured review with:"
echo "1. SUMMARY - One paragraph overall assessment"
echo "2. ISSUES - List of specific problems found (severity: critical/high/medium/low)"
echo "3. SUGGESTIONS - Improvements that are not blocking"
echo "4. VERDICT - APPROVE, REQUEST_CHANGES, or COMMENT"
echo ""
echo "## Diff ${TRUNCATED_NOTE}"
echo '```diff'
echo "${DIFF_CONTENT}"
echo '```'
} > "$PRD_FILE"
echo "PRD written to ${PRD_FILE}"
echo "prd_file=${PRD_FILE}" >> "$GITHUB_OUTPUT"
- name: Run loki review
id: loki-run
if: inputs.mode == 'review' && steps.pr-diff.outputs.has_diff == 'true'
shell: bash
env:
LOKI_PROVIDER: ${{ inputs.provider }}
LOKI_MAX_ITERATIONS: ${{ inputs.max_iterations }}
LOKI_DASHBOARD: 'false'
LOKI_COMPLEXITY: 'simple'
LOKI_BUDGET_LIMIT: ${{ inputs.budget || inputs.budget_limit }}
LOKI_COUNCIL_ENABLED: 'false'
LOKI_SKIP_PREREQS: 'true'
LOKI_AUTO_CONFIRM: ${{ inputs.auto_confirm }}
run: |
PRD_FILE="${{ steps.review-prd.outputs.prd_file }}"
OUTPUT_FILE="${RUNNER_TEMP}/loki-review-output.txt"
echo "--- Running loki review (provider: $LOKI_PROVIDER, max_iterations: $LOKI_MAX_ITERATIONS) ---"
# Build args array to handle paths with spaces safely
ARGS=(--simple --no-dashboard --provider "$LOKI_PROVIDER")
if [ -n "$LOKI_BUDGET_LIMIT" ]; then
ARGS+=(--budget "$LOKI_BUDGET_LIMIT")
fi
ARGS+=("$PRD_FILE")
# Run loki and capture output; do not fail the action on non-zero exit
# (loki may exit non-zero on timeout, which is acceptable)
loki start "${ARGS[@]}" \
2>&1 | tee "$OUTPUT_FILE" || true
echo "--- Loki review completed ---"
echo "output_file=${OUTPUT_FILE}" >> "$GITHUB_OUTPUT"
- name: Run loki fix
id: loki-fix
if: inputs.mode == 'fix'
shell: bash
env:
LOKI_PROVIDER: ${{ inputs.provider }}
LOKI_MAX_ITERATIONS: ${{ inputs.max_iterations }}
LOKI_DASHBOARD: 'false'
LOKI_COMPLEXITY: 'simple'
LOKI_BUDGET_LIMIT: ${{ inputs.budget || inputs.budget_limit }}
LOKI_COUNCIL_ENABLED: 'false'
LOKI_SKIP_PREREQS: 'true'
LOKI_AUTO_CONFIRM: ${{ inputs.auto_confirm }}
run: |
OUTPUT_FILE="${RUNNER_TEMP}/loki-fix-output.txt"
PRD_FILE="${{ inputs.prd_file }}"
echo "--- Running loki in fix mode (provider: $LOKI_PROVIDER) ---"
# Build args array to handle paths with spaces safely
ARGS=(--simple --no-dashboard --provider "$LOKI_PROVIDER")
if [ -n "$LOKI_BUDGET_LIMIT" ]; then
ARGS+=(--budget "$LOKI_BUDGET_LIMIT")
fi
if [ -n "$PRD_FILE" ]; then
ARGS+=("$PRD_FILE")
fi
loki start "${ARGS[@]}" \
2>&1 | tee "$OUTPUT_FILE" || true
echo "output_file=${OUTPUT_FILE}" >> "$GITHUB_OUTPUT"
- name: Run loki test
id: loki-test
if: inputs.mode == 'test'
shell: bash
env:
LOKI_PROVIDER: ${{ inputs.provider }}
LOKI_MAX_ITERATIONS: ${{ inputs.max_iterations }}
LOKI_DASHBOARD: 'false'
LOKI_COMPLEXITY: 'simple'
LOKI_BUDGET_LIMIT: ${{ inputs.budget || inputs.budget_limit }}
LOKI_COUNCIL_ENABLED: 'false'
LOKI_SKIP_PREREQS: 'true'
LOKI_AUTO_CONFIRM: ${{ inputs.auto_confirm }}
run: |
OUTPUT_FILE="${RUNNER_TEMP}/loki-test-output.txt"
PRD_FILE="${{ inputs.prd_file }}"
echo "--- Running loki in test mode (provider: $LOKI_PROVIDER) ---"
# Build args array to handle paths with spaces safely
ARGS=(--simple --no-dashboard --provider "$LOKI_PROVIDER")
if [ -n "$LOKI_BUDGET_LIMIT" ]; then
ARGS+=(--budget "$LOKI_BUDGET_LIMIT")
fi
if [ -n "$PRD_FILE" ]; then
ARGS+=("$PRD_FILE")
fi
loki start "${ARGS[@]}" \
2>&1 | tee "$OUTPUT_FILE" || true
echo "output_file=${OUTPUT_FILE}" >> "$GITHUB_OUTPUT"
- name: Post review as PR comment
if: inputs.mode == 'review' && steps.pr-diff.outputs.has_diff == 'true'
shell: bash
env:
GH_TOKEN: ${{ inputs.github_token }}
ACTION_PROVIDER: ${{ inputs.provider }}
ACTION_MODE: ${{ inputs.mode }}
ACTION_MAX_ITER: ${{ inputs.max_iterations }}
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
OUTPUT_FILE="${{ steps.loki-run.outputs.output_file }}"
if [ -z "$PR_NUMBER" ]; then
echo "No PR number available. Skipping comment."
exit 0
fi
if [ ! -s "$OUTPUT_FILE" ]; then
echo "No review output to post."
exit 0
fi
# Extract the meaningful portion of loki output
# Trim to last 64000 chars to stay within GitHub comment limits (65536 max)
REVIEW_BODY=$(tail -c 64000 "$OUTPUT_FILE")
# Build the comment
COMMENT_FILE="${RUNNER_TEMP}/pr-comment.md"
{
echo "## Loki Mode Code Review"
echo ""
echo "Automated review by [loki-mode](https://github.com/asklokesh/loki-mode)"
echo ""
echo "<details>"
echo "<summary>Review Output</summary>"
echo ""
echo '```'
echo "$REVIEW_BODY"
echo '```'
echo ""
echo "</details>"
echo ""
echo "---"
echo "*Provider: ${ACTION_PROVIDER} | Mode: ${ACTION_MODE} | Max Iterations: ${ACTION_MAX_ITER}*"
} > "$COMMENT_FILE"
echo "--- Posting review comment to PR #${PR_NUMBER} ---"
gh pr comment "$PR_NUMBER" --body-file "$COMMENT_FILE" || {
echo "WARNING: Failed to post PR comment. The review output is available in the job logs above."
}
- name: Cleanup
if: always()
shell: bash
run: |
# Kill any lingering loki processes
pkill -f "loki-run-" 2>/dev/null || true
# Clean temp files
rm -rf "${RUNNER_TEMP}/pr-"*.diff "${RUNNER_TEMP}/review-prd-"*.md \
"${RUNNER_TEMP}/loki-"*-output.txt "${RUNNER_TEMP}/pr-comment.md" \
/tmp/loki-* 2>/dev/null || true