1- name : chatuujojh
1+ name : PR Validation for Polling file
22
33on :
44 pull_request_target :
55 branches :
6- - main
7- types :
8- - opened
9- - synchronize
6+ - doNotMerge-ContentValidationCCP
7+ types : [opened, synchronize]
108
119permissions :
1210 id-token : write
1311 contents : read
1412
1513env :
16- CLIENT_ID : ${{ secrets.AZURE_CLIENT_ID }}
17- API_BASE : https://sentinel -content-validationapi-prod-bvgsc3hjhyeqangg .canadacentral-01.azurewebsites.net/
14+ CLIENT_ID : da7fd62a-590e-4e7a-8525-55ea01ffb1ac
15+ API_BASE : https://sentintel -content-dev-fue4ashcg9fnfge9 .canadacentral-01.azurewebsites.net/
1816
1917jobs :
2018 pr-validate :
@@ -24,129 +22,89 @@ jobs:
2422 - name : Checkout PR code
2523 uses : actions/checkout@v3
2624 with :
27- token : ${{ secrets.GITHUB_TOKEN }} # needed when using pull_request_target on forks
2825 fetch-depth : 0
2926 ref : ${{ github.event.pull_request.head.ref }}
3027 repository : ${{ github.event.pull_request.head.repo.full_name }}
3128
32- - name : Get changed JSON files and contents
29+ - name : Get changed JSON files and contents to send to API endpoint
3330 id : prepare_json_files
3431 run : |
35- set -eux
36-
37- echo "🔍 Collecting changed JSON files…"
32+ echo "Collecting changed JSON files:"
3833 BASE="${{ github.event.pull_request.base.sha }}"
3934 HEAD="${{ github.event.pull_request.head.sha }}"
4035
41- mapfile -t files < <(git diff --name-only "$BASE" "$HEAD" | grep -Ei '\.json$')
42- echo "→ All .json files: ${files[*]:-<none>}"
36+ mapfile -t files < <(git diff --name-only "$BASE" "$HEAD" | grep '\.json$')
37+ echo "Changed JSON files:"
38+ for file in "${files[@]}"; do
39+ echo "$file"
40+ done
4341
44- # Case-insensitive filter for “poll”
45- filtered=()
46- for f in "${files[@]}"; do
47- if [[ "$f" =~ [Pp]oll ]]; then
48- filtered+=("$f")
42+ filtered_files=()
43+ for file in "${files[@]}"; do
44+ if [[ "$file" =~ [Pp][Oo][Ll][Ll] ]]; then
45+ filtered_files+=("$file")
4946 fi
5047 done
5148
52- if [ ${#filtered[@]} -eq 0 ]; then
53- echo "→ No polling JSON files changed; skipping validation."
49+ echo "Polling JSON files:"
50+ for file in "${filtered_files[@]}"; do
51+ echo "$file"
52+ done
53+
54+ if [ ${#filtered_files[@]} -eq 0 ]; then
55+ echo "No JSON files with 'poll' in the name changed in this PR."
5456 echo "JSON_PAYLOAD_PATH=empty" >> $GITHUB_ENV
5557 exit 0
5658 fi
5759
58- echo "→ Polling files: ${filtered[*]}"
59-
60- # Build JSON array payload
6160 json_array="["
62- for f in "${filtered[@]}"; do
63- if jq -e . "$f" > /dev/null; then
64- content=$(jq -c . "$f")
65- json_array+="{\"filename\":\"$f\",\"content\":$content},"
66- else
67- echo "❌ Invalid JSON in $f"
68- exit 1
61+ for file in "${filtered_files[@]}"; do
62+ if [ -f "$file" ]; then
63+ if jq -e . "$file" > /dev/null 2>&1; then
64+ content=$(jq -c . < "$file")
65+ json_array+="{\"filename\": \"${file}\", \"content\": $content},"
66+ else
67+ echo "❌ Invalid JSON structure in file: $file"
68+ exit 1
69+ fi
6970 fi
7071 done
7172 json_array="${json_array%,}]"
73+
74+ # 🔍 Debug echo
75+ echo "Constructed JSON array: $json_array"
76+
7277 echo "$json_array" > files_payload.json
7378 echo "JSON_PAYLOAD_PATH=files_payload.json" >> $GITHUB_ENV
7479
7580 - name : Request OIDC token from GitHub
76- if : env.JSON_PAYLOAD_PATH != 'empty'
81+ if : ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
7782 id : fetch_token
7883 run : |
79- set -eux
80- echo "🔐 Fetching OIDC token…"
81- raw=$(curl -s \
82- -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
83- "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=api://${CLIENT_ID}")
84- token=$(echo "$raw" | jq -r .value)
85- echo "✔️ Got token (length=${#token})"
84+ raw=$(curl -s -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=api://${CLIENT_ID}")
85+ token=$(echo "$raw" | jq -r '.value')
8686 echo "TOKEN=$token" >> $GITHUB_ENV
8787
88-
89- - name : 📡 Health-check GET
90- if : env.JSON_PAYLOAD_PATH != 'empty'
88+ - name : Send GET request to check if API is live
89+ if : ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
90+ id : check_api
9191 run : |
92- set -eux
93- echo "🚀 Hitting $API_BASE…"
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
92+ response=$(curl -s -H "Authorization: Bearer $TOKEN" "$API_BASE/")
93+ echo "$response" | jq . || true
11194
112- # Exit non-2xx
113- if [[ "$status" != 2* ]]; then
114- echo "❌ Health check failed (status $status)"
115- exit 1
116- fi
117-
118- - name : 📦 POST polling payload
119- if : env.JSON_PAYLOAD_PATH != 'empty'
95+ - name : Send POST request with JSON payload
96+ if : ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
12097 run : |
121- set -eux
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
98+ echo "Sending JSON payload to API"
99+
100+ response=$(curl -s -X POST "$API_BASE/polling" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" --data-binary "@$JSON_PAYLOAD_PATH")
142101
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>}"
102+ result_status=$(echo "$response" | jq -r '.status // empty')
103+ message=$(echo "$response" | jq -r '.message // "No message provided"')
146104
147- if [[ "$status " != 2* ]] || [[ "$api_status" != Passed ] ]; then
148- echo " ❌ Validation failed (HTTP $status / api.status=$api_status) "
105+ if [ "$result_status " != "passed" ]; then
106+ echo -e " ❌ Validation failed: $message "
149107 exit 1
150108 fi
151109
152- echo "✅ All checks passed! "
110+ echo "✅ $message "
0 commit comments