Skip to content

Commit a703ec6

Browse files
authored
Feat: GitHub integration (#25)
1 parent daa1577 commit a703ec6

3 files changed

Lines changed: 61 additions & 6 deletions

File tree

.github/workflows/code-review.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ jobs:
1919
pull-requests: write
2020
steps:
2121
- uses: actions/checkout@v4
22+
with:
23+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
24+
fetch-depth: 2
2225

2326
- uses: ./
2427
with:

action.yml

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,37 @@ runs:
206206
shell: bash
207207
env:
208208
GH_TOKEN: ${{ env.GITHUB_TOKEN || github.token }}
209+
GITHUB_REPOSITORY: ${{ github.repository }}
209210
run: |
210211
# Get authenticated user (the bot making API calls)
211-
echo "Debug: GH_TOKEN is set: $([ -n "$GH_TOKEN" ] && echo 'yes' || echo 'no')"
212-
echo "Debug: Attempting to detect bot identity..."
212+
# Note: /user endpoint doesn't work with GitHub App tokens, so we try multiple methods
213+
BOT_LOGIN=""
214+
215+
# Method 1: Try /user endpoint (works for PATs, not GitHub App tokens)
216+
if [ -z "$BOT_LOGIN" ]; then
217+
if BOT_LOGIN_TMP=$(gh api user --jq '.login' 2>/dev/null); then
218+
BOT_LOGIN="$BOT_LOGIN_TMP"
219+
echo "Detected bot via /user endpoint: $BOT_LOGIN"
220+
fi
221+
fi
222+
223+
# Method 2: Try installation endpoint (works for GitHub App tokens)
224+
if [ -z "$BOT_LOGIN" ]; then
225+
if INSTALLATION_DATA=$(gh api "repos/$GITHUB_REPOSITORY/installation" 2>/dev/null); then
226+
APP_SLUG=$(echo "$INSTALLATION_DATA" | jq -r '.app_slug // empty' 2>/dev/null)
227+
if [ -n "$APP_SLUG" ] && [ "$APP_SLUG" != "null" ]; then
228+
BOT_LOGIN="${APP_SLUG}[bot]"
229+
echo "Detected bot via installation endpoint: $BOT_LOGIN"
230+
fi
231+
fi
232+
fi
213233
214-
if ! BOT_LOGIN=$(gh api user --jq '.login' 2>&1); then
215-
echo "Warning: Failed to detect bot identity (error: $BOT_LOGIN), using default"
234+
# Method 3: Fallback to default
235+
if [ -z "$BOT_LOGIN" ]; then
216236
BOT_LOGIN="github-actions"
237+
echo "Using default bot identity: $BOT_LOGIN"
217238
fi
239+
218240
echo "bot_login=$BOT_LOGIN" >> $GITHUB_OUTPUT
219241
echo "Detected bot identity: $BOT_LOGIN"
220242
@@ -510,9 +532,31 @@ runs:
510532
CLAUDECODE_FINDINGS: ${{ steps.claudecode-scan.outputs.findings_count }}
511533
SILENCE_CLAUDECODE_COMMENTS: ${{ steps.claudecode-check.outputs.silence_claudecode_comments }}
512534
ACTION_PATH: ${{ github.action_path }}
535+
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha || steps.pr-info.outputs.pr_sha }}
513536
run: |
514537
node "$ACTION_PATH/scripts/comment-pr-findings.js"
515538
539+
- name: Request bot as reviewer
540+
if: steps.claudecode-check.outputs.enable_claudecode == 'true'
541+
shell: bash
542+
env:
543+
GH_TOKEN: ${{ env.GITHUB_TOKEN || github.token }}
544+
GITHUB_REPOSITORY: ${{ github.repository }}
545+
PR_NUMBER: ${{ github.event.pull_request.number || steps.pr-info.outputs.pr_number }}
546+
BOT_LOGIN: ${{ steps.bot-identity.outputs.bot_login }}
547+
run: |
548+
# Request the bot as a reviewer to make it appear in the PR's reviewer list
549+
# This is optional and will fail silently if the bot is already a reviewer or doesn't have permissions
550+
echo "Requesting $BOT_LOGIN as reviewer for PR #$PR_NUMBER..."
551+
552+
if gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/requested_reviewers" \
553+
-X POST \
554+
-f "reviewers[]=$BOT_LOGIN" 2>/dev/null; then
555+
echo "Successfully requested $BOT_LOGIN as reviewer"
556+
else
557+
echo "Note: Could not request bot as reviewer (this is normal if bot is already a reviewer or is the PR author)"
558+
fi
559+
516560
branding:
517561
icon: 'shield'
518562
color: 'red'

scripts/comment-pr-findings.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,24 @@ const { spawnSync } = require('child_process');
1111
const PR_SUMMARY_MARKER = '📋 **PR Summary:**';
1212

1313
// Parse GitHub context from environment
14+
const eventData = process.env.GITHUB_EVENT_PATH ? JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')) : {};
1415
const context = {
1516
repo: {
1617
owner: process.env.GITHUB_REPOSITORY?.split('/')[0] || '',
1718
repo: process.env.GITHUB_REPOSITORY?.split('/')[1] || ''
1819
},
1920
issue: {
20-
number: parseInt(process.env.GITHUB_EVENT_PATH ? JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')).pull_request?.number : '') || 0
21+
number: parseInt(eventData.pull_request?.number || eventData.issue?.number || 0)
2122
},
2223
payload: {
23-
pull_request: process.env.GITHUB_EVENT_PATH ? JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')).pull_request : {}
24+
pull_request: {
25+
...eventData.pull_request,
26+
head: {
27+
...(eventData.pull_request?.head || {}),
28+
// Use PR_HEAD_SHA from environment if available (more reliable than event payload)
29+
sha: process.env.PR_HEAD_SHA || eventData.pull_request?.head?.sha || ''
30+
}
31+
}
2432
}
2533
};
2634

0 commit comments

Comments
 (0)