Skip to content

Commit 3e232f1

Browse files
Merge branch 'Mayank-goel360:main' into main
2 parents e1c4c1e + a0604cb commit 3e232f1

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

.github/workflows/final.yml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: chat
2+
3+
on:
4+
pull_request_target:
5+
branches:
6+
- main
7+
types:
8+
- opened
9+
- synchronize
10+
11+
permissions:
12+
id-token: write
13+
contents: read
14+
15+
env:
16+
CLIENT_ID: da7fd62a-590e-4e7a-8525-55ea01ffb1ac
17+
API_BASE: https://sentinel-content-validationapi-prod-bvgsc3hjhyeqangg.canadacentral-01.azurewebsites.net/
18+
19+
jobs:
20+
pr-validate:
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout PR code
25+
uses: actions/checkout@v3
26+
with:
27+
token: ${{ secrets.GITHUB_TOKEN }} # needed when using pull_request_target on forks
28+
fetch-depth: 0
29+
ref: ${{ github.event.pull_request.head.ref }}
30+
repository: ${{ github.event.pull_request.head.repo.full_name }}
31+
32+
- name: Get changed JSON files and contents
33+
id: prepare_json_files
34+
run: |
35+
36+
echo "🔍 Collecting changed JSON files…"
37+
BASE="${{ github.event.pull_request.base.sha }}"
38+
HEAD="${{ github.event.pull_request.head.sha }}"
39+
40+
mapfile -t files < <(git diff --name-only "$BASE" "$HEAD" | grep -Ei '\.json$')
41+
echo "→ All .json files: ${files[*]:-<none>}"
42+
43+
# Case-insensitive filter for “poll”
44+
filtered=()
45+
for f in "${files[@]}"; do
46+
if [[ "$f" =~ [Pp]oll ]]; then
47+
filtered+=("$f")
48+
fi
49+
done
50+
51+
if [ ${#filtered[@]} -eq 0 ]; then
52+
echo "→ No polling JSON files changed; skipping validation."
53+
echo "JSON_PAYLOAD_PATH=empty" >> $GITHUB_ENV
54+
exit 0
55+
fi
56+
57+
echo "→ Polling files: ${filtered[*]}"
58+
59+
# Build JSON array payload
60+
json_array="["
61+
for f in "${filtered[@]}"; do
62+
if jq -e . "$f" > /dev/null; then
63+
content=$(jq -c . "$f")
64+
json_array+="{\"filename\":\"$f\",\"content\":$content},"
65+
else
66+
echo "❌ Invalid JSON in $f"
67+
exit 1
68+
fi
69+
done
70+
json_array="${json_array%,}]"
71+
echo "$json_array" > files_payload.json
72+
echo "JSON_PAYLOAD_PATH=files_payload.json" >> $GITHUB_ENV
73+
74+
- name: Request OIDC token from GitHub
75+
if: env.JSON_PAYLOAD_PATH != 'empty'
76+
id: fetch_token
77+
run: |
78+
79+
echo "🔐 Fetching OIDC token…"
80+
raw=$(curl -s \
81+
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
82+
"${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=api://${CLIENT_ID}")
83+
token=$(echo "$raw" | jq -r .value)
84+
echo "✔️ Got token (length=${#token})"
85+
echo "TOKEN=$token" >> $GITHUB_ENV
86+
87+
88+
- name: 📡 Health-check GET
89+
if: env.JSON_PAYLOAD_PATH != 'empty'
90+
run: |
91+
92+
echo "🚀 Hitting $API_BASE…"
93+
# Capture both body and status
94+
resp=$(curl -s -w "\n%{http_code}" \
95+
-H "Authorization: Bearer $TOKEN" \
96+
"$API_BASE")
97+
body=$(echo "$resp" | sed '$d') # all but last line
98+
status=$(echo "$resp" | tail -n1) # last line
99+
echo "🔁 HTTP status: $status"
100+
echo "🔍 Raw body:"
101+
echo "$body" # always printed
102+
103+
# Try to parse JSON, but don’t exit if it fails
104+
if echo "$body" | jq . > /dev/null 2>&1; then
105+
echo "✔️ Parsed JSON:"
106+
echo "$body" | jq .
107+
else
108+
echo "⚠️ Body is not JSON or empty"
109+
fi
110+
111+
# Exit non-2xx
112+
if [[ "$status" != 2* ]]; then
113+
echo "❌ Health check failed (status $status)"
114+
exit 1
115+
fi
116+
117+
- name: 📦 POST polling payload
118+
if: env.JSON_PAYLOAD_PATH != 'empty'
119+
run: |
120+
121+
echo "🚀 POSTing to $API_BASE/polling…"
122+
# Capture both body and status
123+
resp=$(curl -s -w "\n%{http_code}" -X POST \
124+
-H "Authorization: Bearer $TOKEN" \
125+
-H "Content-Type: application/json" \
126+
--data-binary "@$JSON_PAYLOAD_PATH" \
127+
"$API_BASE/polling")
128+
body=$(echo "$resp" | sed '$d')
129+
status=$(echo "$resp" | tail -n1)
130+
echo "🔁 HTTP status: $status"
131+
echo "🔍 Raw body:"
132+
echo "$body"
133+
134+
# Parse JSON if possible
135+
if echo "$body" | jq . > /dev/null 2>&1; then
136+
echo "✔️ Parsed JSON:"
137+
echo "$body" | jq .
138+
else
139+
echo "⚠️ Body is not JSON or empty"
140+
fi
141+
142+
143+
api_status=$(echo "$body" | jq -r '.status // empty')
144+
echo "➡️ API “status” field: ${api_status:-<none>}"
145+
146+
if [[ "$status" != 2* ]] || [[ "$api_status" != Passed ]]; then
147+
echo "❌ Validation failed (HTTP $status / api.status=$api_status)"
148+
exit 1
149+
fi
150+
151+
echo "✅ All checks passed!"

0 commit comments

Comments
 (0)