1- name : PR Validation g
1+ name : chat
22
33on :
4- pull_request :
4+ pull_request_target :
55 branches :
66 - main
77 types :
2424 - name : Checkout PR code
2525 uses : actions/checkout@v3
2626 with :
27- token : ${{ secrets.GITHUB_TOKEN }}
27+ token : ${{ secrets.GITHUB_TOKEN }} # needed when using pull_request_target on forks
2828 fetch-depth : 0
2929 ref : ${{ github.event.pull_request.head.ref }}
3030 repository : ${{ github.event.pull_request.head.repo.full_name }}
@@ -38,11 +38,10 @@ jobs:
3838 BASE="${{ github.event.pull_request.base.sha }}"
3939 HEAD="${{ github.event.pull_request.head.sha }}"
4040
41- # List all .json files changed between base & head
4241 mapfile -t files < <(git diff --name-only "$BASE" "$HEAD" | grep -Ei '\.json$')
4342 echo "→ All .json files: ${files[*]:-<none>}"
4443
45- # Filter for files containing " poll" (case-insensitive)
44+ # Case-insensitive filter for “ poll”
4645 filtered=()
4746 for f in "${files[@]}"; do
4847 if [[ "$f" =~ [Pp]oll ]]; then
@@ -61,16 +60,15 @@ jobs:
6160 # Build JSON array payload
6261 json_array="["
6362 for f in "${filtered[@]}"; do
64- if jq -e . "$f" > /dev/null 2>&1 ; then
63+ if jq -e . "$f" > /dev/null; then
6564 content=$(jq -c . "$f")
6665 json_array+="{\"filename\":\"$f\",\"content\":$content},"
6766 else
68- echo "❌ Invalid JSON structure in file: $f"
67+ echo "❌ Invalid JSON in $f"
6968 exit 1
7069 fi
7170 done
7271 json_array="${json_array%,}]"
73-
7472 echo "$json_array" > files_payload.json
7573 echo "JSON_PAYLOAD_PATH=files_payload.json" >> $GITHUB_ENV
7674
@@ -87,28 +85,68 @@ jobs:
8785 echo "✔️ Got token (length=${#token})"
8886 echo "TOKEN=$token" >> $GITHUB_ENV
8987
88+
9089 - name : 📡 Health-check GET
9190 if : env.JSON_PAYLOAD_PATH != 'empty'
9291 run : |
9392 set -eux
9493 echo "🚀 Hitting $API_BASE…"
95- resp=$(curl -s -w "%{http_code}" -H "Authorization: Bearer $TOKEN" "$API_BASE")
96- body="${resp::-3}"
97- code="${resp: -3}"
98- echo "HTTP $code → $body"
99- if [[ "${code:0:1}" != "2" ]]; then
100- echo "❌ Health check failed"
94+ # Capture both body and status
95+ resp=$(curl -s -w "\n%{http_code}" \
96+ -H "Authorization: Bearer $TOKEN" \
97+ "$API_BASE")
98+ body=$(echo "$resp" | sed '$d') # all but last line
99+ status=$(echo "$resp" | tail -n1) # last line
100+ echo "🔁 HTTP status: $status"
101+ echo "🔍 Raw body:"
102+ echo "$body" # always printed
103+
104+ # Try to parse JSON, but don’t exit if it fails
105+ if echo "$body" | jq . > /dev/null 2>&1; then
106+ echo "✔️ Parsed JSON:"
107+ echo "$body" | jq .
108+ else
109+ echo "⚠️ Body is not JSON or empty"
110+ fi
111+
112+ # Exit non-2xx
113+ if [[ "$status" != 2* ]]; then
114+ echo "❌ Health check failed (status $status)"
101115 exit 1
102116 fi
103117
104118 - name : 📦 POST polling payload
105119 if : env.JSON_PAYLOAD_PATH != 'empty'
106120 run : |
107121 set -eux
108- echo "🚀 Sending POST to $API_BASE/polling…"
109- curl -s -X POST \
110- -H "Authorization: Bearer $TOKEN" \
111- -H "Content-Type: application/json" \
112- --data-binary "@$JSON_PAYLOAD_PATH" \
113- "$API_BASE/polling" \
114- | jq .
122+ echo "🚀 POSTing to $API_BASE/polling…"
123+ # Capture both body and status
124+ resp=$(curl -s -w "\n%{http_code}" -X POST \
125+ -H "Authorization: Bearer $TOKEN" \
126+ -H "Content-Type: application/json" \
127+ --data-binary "@$JSON_PAYLOAD_PATH" \
128+ "$API_BASE/polling")
129+ body=$(echo "$resp" | sed '$d')
130+ status=$(echo "$resp" | tail -n1)
131+ echo "🔁 HTTP status: $status"
132+ echo "🔍 Raw body:"
133+ echo "$body"
134+
135+ # Parse JSON if possible
136+ if echo "$body" | jq . > /dev/null 2>&1; then
137+ echo "✔️ Parsed JSON:"
138+ echo "$body" | jq .
139+ else
140+ echo "⚠️ Body is not JSON or empty"
141+ fi
142+
143+ # Now inspect your API’s own “status” field if you want:
144+ api_status=$(echo "$body" | jq -r '.status // empty')
145+ echo "➡️ API “status” field: ${api_status:-<none>}"
146+
147+ if [[ "$status" != 2* ]] || [[ "$api_status" != passed ]]; then
148+ echo "❌ Validation failed (HTTP $status / api.status=$api_status)"
149+ exit 1
150+ fi
151+
152+ echo "✅ All checks passed!"
0 commit comments