Skip to content

Commit 5c6b199

Browse files
check token for get
1 parent b6306d1 commit 5c6b199

File tree

2 files changed

+89
-121
lines changed

2 files changed

+89
-121
lines changed

.github/workflows/dra.yml

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
2-
name: PR Validation for Polling file prod
1+
name: PR Validation for Polling g
32

43
on:
5-
pull_request_target:
4+
pull_request: # ← use pull_request so forks can be checked out
65
branches:
7-
# - doNotMerge-ContentValidationCCP ##########################################################################
86
- main
97
types: [opened, synchronize]
108

@@ -14,7 +12,7 @@ permissions:
1412

1513
env:
1614
CLIENT_ID: ${{ secrets.AZURE_CONTENT_VALIDATION_CLIENT_ID }}
17-
API_BASE: https://sentinel-content-validationapi-prod-bvgsc3hjhyeqangg.canadacentral-01.azurewebsites.net/
15+
API_BASE: https://sentinel-content-validationapi-prod-bvgsc3hjhyeqangg.canadacentral-01.azurewebsites.net/
1816

1917
jobs:
2018
pr-validate:
@@ -24,96 +22,83 @@ jobs:
2422
- name: Checkout PR code
2523
uses: actions/checkout@v3
2624
with:
25+
token: ${{ secrets.GITHUB_TOKEN }} # ← ensure forks can be checked out
2726
fetch-depth: 0
2827
ref: ${{ github.event.pull_request.head.ref }}
2928
repository: ${{ github.event.pull_request.head.repo.full_name }}
3029

31-
- name: Get changed JSON files and contents to send to API endpoint
30+
- name: Get changed JSON files and contents
3231
id: prepare_json_files
3332
run: |
34-
echo "Collecting changed JSON files:"
33+
set -eux
34+
echo "🔍 Collecting changed JSON files…"
3535
BASE="${{ github.event.pull_request.base.sha }}"
3636
HEAD="${{ github.event.pull_request.head.sha }}"
3737
38-
mapfile -t files < <(git diff --name-only "$BASE" "$HEAD" | grep '\.json$')
39-
echo "Changed JSON files:"
40-
for file in "${files[@]}"; do
41-
echo "$file"
42-
done
43-
44-
filtered_files=()
45-
for file in "${files[@]}"; do
46-
if [[ "$file" =~ [Pp][Oo][Ll][Ll] ]]; then
47-
filtered_files+=("$file")
48-
fi
49-
done
38+
mapfile -t files < <(git diff --name-only "$BASE" "$HEAD" | grep -Ei '\.json$')
39+
echo "→ All .json files: ${files[*]:-<none>}"
5040
51-
echo "Polling JSON files:"
52-
for file in "${filtered_files[@]}"; do
53-
echo "$file"
41+
# Filter to only ones containing “poll” (case-insensitive)
42+
filtered=()
43+
for f in "${files[@]}"; do
44+
[[ "$f" =~ poll ]] && filtered+=("$f")
5445
done
5546
56-
if [ ${#filtered_files[@]} -eq 0 ]; then
57-
echo "No JSON files with 'poll' in the name changed in this PR."
47+
if [ ${#filtered[@]} -eq 0 ]; then
48+
echo "No polling JSON files changed; skipping validation."
5849
echo "JSON_PAYLOAD_PATH=empty" >> $GITHUB_ENV
5950
exit 0
6051
fi
6152
62-
json_array="["
63-
for file in "${filtered_files[@]}"; do
64-
if [ -f "$file" ]; then
65-
if jq -e . "$file" > /dev/null 2>&1; then
66-
content=$(jq -c . < "$file")
67-
json_array+="{\"filename\": \"${file}\", \"content\": $content},"
68-
else
69-
echo "❌ Invalid JSON structure in file: $file"
70-
exit 1
71-
fi
72-
fi
53+
echo "→ Polling files: ${filtered[*]}"
54+
# Build the JSON array
55+
json='['
56+
for f in "${filtered[@]}"; do
57+
jq -c . "$f" \
58+
| xargs -I% printf '{"filename":"%s","content":%s},' "$f" "%" \
59+
>> tmp_payload.json
7360
done
74-
json_array="${json_array%,}]"
75-
76-
# 🔍 Debug echo
77-
echo "Constructed JSON array: $json_array"
78-
79-
echo "$json_array" > files_payload.json
61+
# trim trailing comma and wrap
62+
payload=$(<tmp_payload.json)
63+
payload="[${payload%,}]"
64+
echo "$payload" > files_payload.json
8065
echo "JSON_PAYLOAD_PATH=files_payload.json" >> $GITHUB_ENV
8166
8267
- name: Request OIDC token from GitHub
83-
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
68+
if: env.JSON_PAYLOAD_PATH != 'empty'
8469
id: fetch_token
8570
run: |
86-
raw=$(curl -s -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=api://${CLIENT_ID}")
87-
token=$(echo "$raw" | jq -r '.value')
71+
set -eux
72+
echo "🔐 Fetching OIDC token…"
73+
raw=$(curl -s \
74+
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
75+
"${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=api://${CLIENT_ID}")
76+
token=$(echo "$raw" | jq -r .value)
77+
echo "✔️ Got token, length=${#token}"
8878
echo "TOKEN=$token" >> $GITHUB_ENV
8979
90-
- name: Send GET request to check if API is live
91-
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
92-
id: check_api
93-
run: |
94-
response=$(curl -s -H "Authorization: Bearer $TOKEN" "$API_BASE")
95-
echo "$response" | jq . || true
96-
97-
- name: 📡 Send GET request with Bearer token
98-
run: |
99-
echo "🚀 Sending GET to $API_BASE"
100-
response=$(curl -s -H "Authorization: Bearer $TOKEN" "$API_BASE")
101-
echo "🔍 Response JSON:"
102-
echo "$response" | jq .
103-
104-
- name: Send POST request with JSON payload
105-
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
80+
- name: 📡 Health-check GET
81+
if: env.JSON_PAYLOAD_PATH != 'empty'
10682
run: |
107-
echo "Sending JSON payload to API"
108-
109-
response=$(curl -s -X POST "$API_BASE" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" --data-binary "@$JSON_PAYLOAD_PATH")
110-
111-
result_status=$(echo "$response" | jq -r '.status // empty')
112-
message=$(echo "$response" | jq -r '.message // "No message provided"')
113-
114-
if [ "$result_status" != "passed" ]; then
115-
echo -e " ❌ Validation failed: $message"
83+
set -eux
84+
echo "🚀 Hitting $API_BASE…"
85+
resp=$(curl -s -w "%{http_code}" -H "Authorization: Bearer $TOKEN" "$API_BASE")
86+
body="${resp::-3}" # all but last 3 chars
87+
code="${resp: -3}" # last 3 chars
88+
echo "HTTP $code → $body"
89+
if [ "${code:0:1}" != "2" ]; then
90+
echo "❌ Health check failed"
11691
exit 1
11792
fi
11893
119-
echo "✅ $message"
94+
- name: 📦 POST polling payload
95+
if: env.JSON_PAYLOAD_PATH != 'empty'
96+
run: |
97+
set -eux
98+
echo "🚀 Sending POST to $API_BASE/polling…"
99+
curl -s -X POST \
100+
-H "Authorization: Bearer $TOKEN" \
101+
-H "Content-Type: application/json" \
102+
--data-binary "@$JSON_PAYLOAD_PATH" \
103+
"$API_BASE/polling" \
104+
| jq .

.github/workflows/dra1.yml

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
2-
name: dev
1+
name: PR Validation for Polling file prod
32

43
on:
54
pull_request_target:
65
branches:
7-
# - doNotMerge-ContentValidationCCP ##########################################################################
86
- main
97
types: [opened, synchronize]
108

@@ -14,7 +12,7 @@ permissions:
1412

1513
env:
1614
CLIENT_ID: ${{ secrets.AZURE_CONTENT_VALIDATION_CLIENT_ID }}
17-
API_BASE: ${{ secrets.SENTINEL_CONTENT_API_URL }}
15+
API_BASE: https://sentinel-content-validationapi-prod-bvgsc3hjhyeqangg.canadacentral-01.azurewebsites.net/
1816

1917
jobs:
2018
pr-validate:
@@ -31,15 +29,9 @@ jobs:
3129
- name: Get changed JSON files and contents to send to API endpoint
3230
id: prepare_json_files
3331
run: |
34-
echo "Collecting changed JSON files:"
3532
BASE="${{ github.event.pull_request.base.sha }}"
3633
HEAD="${{ github.event.pull_request.head.sha }}"
37-
3834
mapfile -t files < <(git diff --name-only "$BASE" "$HEAD" | grep '\.json$')
39-
echo "Changed JSON files:"
40-
for file in "${files[@]}"; do
41-
echo "$file"
42-
done
4335
4436
filtered_files=()
4537
for file in "${files[@]}"; do
@@ -48,76 +40,67 @@ jobs:
4840
fi
4941
done
5042
51-
echo "Polling JSON files:"
52-
for file in "${filtered_files[@]}"; do
53-
echo "$file"
54-
done
55-
5643
if [ ${#filtered_files[@]} -eq 0 ]; then
57-
echo "No JSON files with 'poll' in the name changed in this PR."
44+
echo "No polling JSON files changed."
5845
echo "JSON_PAYLOAD_PATH=empty" >> $GITHUB_ENV
5946
exit 0
6047
fi
6148
6249
json_array="["
6350
for file in "${filtered_files[@]}"; do
64-
if [ -f "$file" ]; then
65-
if jq -e . "$file" > /dev/null 2>&1; then
66-
content=$(jq -c . < "$file")
67-
json_array+="{\"filename\": \"${file}\", \"content\": $content},"
68-
else
69-
echo "❌ Invalid JSON structure in file: $file"
70-
exit 1
71-
fi
51+
if [ -f "$file" ] && jq -e . "$file" > /dev/null 2>&1; then
52+
content=$(jq -c . < "$file")
53+
json_array+="{\"filename\": \"${file}\", \"content\": $content},"
54+
else
55+
echo "❌ Invalid JSON in $file"
56+
exit 1
7257
fi
7358
done
7459
json_array="${json_array%,}]"
75-
76-
# 🔍 Debug echo
77-
echo "Constructed JSON array: $json_array"
78-
7960
echo "$json_array" > files_payload.json
8061
echo "JSON_PAYLOAD_PATH=files_payload.json" >> $GITHUB_ENV
8162
8263
- name: Request OIDC token from GitHub
8364
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
8465
id: fetch_token
8566
run: |
86-
raw=$(curl -s -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=api://${CLIENT_ID}")
87-
token=$(echo "$raw" | jq -r '.value')
88-
echo "TOKEN=$token" >> $GITHUB_ENV
89-
90-
- name: Send GET request to check if API is live
91-
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
92-
id: check_api
93-
run: |
94-
response=$(curl -s -H "Authorization: Bearer $TOKEN" "$API_BASE/")
95-
echo "$response" | jq . || true
67+
echo "Requesting OIDC token..."
68+
raw=$(curl -s -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=api://${CLIENT_ID}")
69+
echo "Raw OIDC response: $raw"
70+
token=$(echo "$raw" | jq -r '.value // empty')
71+
if [ -z "$token" ]; then
72+
echo "❌ OIDC token is empty. Likely due to PR from fork. Skipping API calls."
73+
echo "SKIP_API=true" >> $GITHUB_ENV
74+
else
75+
echo "TOKEN=$token" >> $GITHUB_ENV
76+
echo "SKIP_API=false" >> $GITHUB_ENV
77+
fi
9678
97-
- name: 📡 Send GET request with Bearer token
79+
- name: Check if API is live
80+
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' && env.SKIP_API != 'true' }}
9881
run: |
99-
echo "🚀 Sending GET to $API_BASE"
82+
echo "Checking API health..."
10083
response=$(curl -s -H "Authorization: Bearer $TOKEN" "$API_BASE")
101-
echo "🔍 Response JSON:"
102-
echo "$response" | jq .
84+
echo "$response" | jq . || echo "⚠️ Invalid JSON response"
10385
10486
- name: Send POST request with JSON payload
105-
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' }}
87+
if: ${{ env.JSON_PAYLOAD_PATH != 'empty' && env.SKIP_API != 'true' }}
10688
run: |
107-
echo "Sending JSON payload to API"
89+
echo "Sending JSON payload to API..."
90+
response=$(curl -s -X POST "$API_BASE/polling" \
91+
-H "Authorization: Bearer $TOKEN" \
92+
-H "Content-Type: application/json" \
93+
--data-binary "@$JSON_PAYLOAD_PATH")
10894
109-
response=$(curl -s -X POST "$API_BASE" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" --data-binary "@$JSON_PAYLOAD_PATH")
95+
echo "API response:"
96+
echo "$response" | jq .
11097
11198
result_status=$(echo "$response" | jq -r '.status // empty')
11299
message=$(echo "$response" | jq -r '.message // "No message provided"')
113100
114-
echo $result_status
115-
echo "$result_status"
116-
117-
echo "✅ $message"
118101
if [ "$result_status" != "passed" ]; then
119-
echo -e " ❌ Validation failed: $message "
102+
echo "❌ Validation failed: $message"
120103
exit 1
121104
fi
122105
123-
echo "✅ $message"
106+
echo "✅ $message"

0 commit comments

Comments
 (0)