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'"
0 commit comments