Skip to content

Commit 7040bdf

Browse files
Copilotastandrik
andcommitted
Add validation and testing for Claude workflow fix
Co-authored-by: astandrik <[email protected]>
1 parent c3c3796 commit 7040bdf

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

.github/workflows/claude-code-review.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ jobs:
4242
ref: ${{ github.event_name == 'issue_comment' && steps.pr-details.outputs.pr_head_sha || github.ref }}
4343
fetch-depth: 0
4444

45+
- name: Validate checkout (verification step)
46+
run: |
47+
echo "=== Workflow Checkout Validation ==="
48+
echo "Event type: ${{ github.event_name }}"
49+
if [ "${{ github.event_name }}" = "issue_comment" ]; then
50+
echo "PR number: ${{ steps.pr-details.outputs.pr_number }}"
51+
echo "Expected PR head SHA: ${{ steps.pr-details.outputs.pr_head_sha }}"
52+
fi
53+
echo "Checked out commit: $(git rev-parse HEAD)"
54+
echo "Current branch/ref: $(git symbolic-ref HEAD 2>/dev/null || echo 'detached HEAD')"
55+
echo "=== Validation Complete ==="
56+
57+
# Verify we checked out the correct commit for PR comments
58+
if [ "${{ github.event_name }}" = "issue_comment" ]; then
59+
CURRENT_SHA=$(git rev-parse HEAD)
60+
EXPECTED_SHA="${{ steps.pr-details.outputs.pr_head_sha }}"
61+
if [ "$CURRENT_SHA" = "$EXPECTED_SHA" ]; then
62+
echo "✅ SUCCESS: Checked out correct PR head commit"
63+
else
64+
echo "❌ ERROR: Checked out $CURRENT_SHA but expected $EXPECTED_SHA"
65+
exit 1
66+
fi
67+
else
68+
echo "✅ SUCCESS: Workflow dispatch - using default ref"
69+
fi
70+
4571
- name: Run Claude Code Review
4672
id: claude-review
4773
uses: anthropics/claude-code-action@beta

.github/workflows/claude.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ jobs:
4141
ref: ${{ github.event_name == 'issue_comment' && steps.pr-details.outputs.pr_head_sha || github.ref }}
4242
fetch-depth: 0
4343

44+
- name: Validate checkout (verification step)
45+
run: |
46+
echo "=== Workflow Checkout Validation ==="
47+
echo "Event type: ${{ github.event_name }}"
48+
if [ "${{ github.event_name }}" = "issue_comment" ]; then
49+
echo "PR number: ${{ steps.pr-details.outputs.pr_number }}"
50+
echo "Expected PR head SHA: ${{ steps.pr-details.outputs.pr_head_sha }}"
51+
fi
52+
echo "Checked out commit: $(git rev-parse HEAD)"
53+
echo "Current branch/ref: $(git symbolic-ref HEAD 2>/dev/null || echo 'detached HEAD')"
54+
echo "=== Validation Complete ==="
55+
56+
# Verify we checked out the correct commit for PR comments
57+
if [ "${{ github.event_name }}" = "issue_comment" ]; then
58+
CURRENT_SHA=$(git rev-parse HEAD)
59+
EXPECTED_SHA="${{ steps.pr-details.outputs.pr_head_sha }}"
60+
if [ "$CURRENT_SHA" = "$EXPECTED_SHA" ]; then
61+
echo "✅ SUCCESS: Checked out correct PR head commit"
62+
else
63+
echo "❌ ERROR: Checked out $CURRENT_SHA but expected $EXPECTED_SHA"
64+
exit 1
65+
fi
66+
else
67+
echo "✅ SUCCESS: Workflow dispatch - using default ref"
68+
fi
69+
4470
- name: Run Claude Code
4571
id: claude
4672
uses: anthropics/claude-code-action@beta
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash
2+
3+
# Test script to validate Claude workflow checkout fix
4+
# This script can be run manually to verify the workflow logic works correctly
5+
6+
set -e
7+
8+
echo "=== Testing Claude Workflow Checkout Fix ==="
9+
echo
10+
11+
# Test 1: Simulate fetching PR details like the workflow does
12+
echo "Test 1: Simulating PR details API call"
13+
echo "Note: This test requires a valid GitHub token and PR number"
14+
echo
15+
16+
if [ -z "$GITHUB_TOKEN" ]; then
17+
echo "⚠️ GITHUB_TOKEN not set - skipping API test"
18+
echo " To test with real data, set GITHUB_TOKEN environment variable"
19+
else
20+
if [ -z "$1" ]; then
21+
echo "⚠️ No PR number provided - skipping API test"
22+
echo " Usage: $0 <pr_number>"
23+
else
24+
PR_NUMBER="$1"
25+
REPO="${GITHUB_REPOSITORY:-ydb-platform/ydb-embedded-ui}"
26+
27+
echo "Fetching PR details for #$PR_NUMBER in $REPO..."
28+
PR_DATA=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
29+
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER")
30+
31+
if echo "$PR_DATA" | jq -e '.head.sha' > /dev/null 2>&1; then
32+
PR_HEAD_SHA=$(echo "$PR_DATA" | jq -r '.head.sha')
33+
echo "✅ Successfully fetched PR head SHA: $PR_HEAD_SHA"
34+
else
35+
echo "❌ Failed to fetch PR details"
36+
echo "Response: $PR_DATA"
37+
exit 1
38+
fi
39+
fi
40+
fi
41+
42+
echo
43+
44+
# Test 2: Validate the conditional logic used in workflows
45+
echo "Test 2: Testing conditional checkout logic"
46+
echo
47+
48+
test_checkout_ref() {
49+
local event_name="$1"
50+
local pr_head_sha="$2"
51+
local github_ref="$3"
52+
53+
if [ "$event_name" = "issue_comment" ] && [ -n "$pr_head_sha" ]; then
54+
echo "$pr_head_sha"
55+
else
56+
echo "$github_ref"
57+
fi
58+
}
59+
60+
# Test workflow_dispatch scenario
61+
WORKFLOW_DISPATCH_REF=$(test_checkout_ref "workflow_dispatch" "" "refs/heads/main")
62+
echo "Workflow dispatch ref: $WORKFLOW_DISPATCH_REF"
63+
if [ "$WORKFLOW_DISPATCH_REF" = "refs/heads/main" ]; then
64+
echo "✅ Workflow dispatch correctly uses github.ref"
65+
else
66+
echo "❌ Workflow dispatch logic failed"
67+
exit 1
68+
fi
69+
70+
# Test issue_comment scenario
71+
ISSUE_COMMENT_REF=$(test_checkout_ref "issue_comment" "abc123def456" "refs/heads/main")
72+
echo "Issue comment ref: $ISSUE_COMMENT_REF"
73+
if [ "$ISSUE_COMMENT_REF" = "abc123def456" ]; then
74+
echo "✅ Issue comment correctly uses PR head SHA"
75+
else
76+
echo "❌ Issue comment logic failed"
77+
exit 1
78+
fi
79+
80+
echo
81+
82+
# Test 3: Validate that jq is available (required for parsing GitHub API response)
83+
echo "Test 3: Checking dependencies"
84+
echo
85+
86+
if command -v jq >/dev/null 2>&1; then
87+
echo "✅ jq is available"
88+
else
89+
echo "❌ jq is not available - required for parsing GitHub API responses"
90+
exit 1
91+
fi
92+
93+
if command -v curl >/dev/null 2>&1; then
94+
echo "✅ curl is available"
95+
else
96+
echo "❌ curl is not available - required for GitHub API calls"
97+
exit 1
98+
fi
99+
100+
echo
101+
102+
echo "=== All Tests Passed! ==="
103+
echo
104+
echo "The Claude workflow fix should work correctly:"
105+
echo "• When triggered by '/claude_review' or '@claude' comments on PRs"
106+
echo "• The workflow will checkout the PR's head commit instead of the default branch"
107+
echo "• The validation step will verify the correct commit was checked out"
108+
echo
109+
echo "To test manually:"
110+
echo "1. Create a PR with some changes"
111+
echo "2. Add a comment with '/claude_review' or '@claude'"
112+
echo "3. Check the workflow logs for the 'Validate checkout' step"
113+
echo "4. Verify it shows '✅ SUCCESS: Checked out correct PR head commit'"

CLAUDE.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,35 @@ ref: ${{ github.event_name == 'issue_comment' && steps.pr-details.outputs.pr_hea
4444
4545
This ensures Claude always reviews the current state of the PR, including any recent commits.
4646
47+
### Testing the Workflow Fix
48+
49+
To verify the Claude workflows are correctly checking out PR head commits:
50+
51+
1. **Automatic Validation**: Both workflows include a validation step that:
52+
53+
- Logs the event type and commit SHA being checked out
54+
- Verifies the checked out commit matches the expected PR head
55+
- Fails with a clear error if the wrong commit is checked out
56+
57+
2. **Manual Testing**:
58+
59+
- Create a test PR with some changes
60+
- Add a comment with `/claude_review` or `@claude`
61+
- Check the workflow logs for "Validate checkout (verification step)"
62+
- Look for `✅ SUCCESS: Checked out correct PR head commit`
63+
64+
3. **Test Script**: Run `.github/workflows/scripts/test-claude-workflow-fix.sh` to validate the logic locally
65+
66+
4. **Expected Log Output**:
67+
```
68+
=== Workflow Checkout Validation ===
69+
Event type: issue_comment
70+
PR number: 123
71+
Expected PR head SHA: abc123def456...
72+
Checked out commit: abc123def456...
73+
✅ SUCCESS: Checked out correct PR head commit
74+
```
75+
4776
---
4877

4978
# Project Development Guide

0 commit comments

Comments
 (0)