-
Notifications
You must be signed in to change notification settings - Fork 0
135 lines (121 loc) · 4.86 KB
/
api-validate.yml
File metadata and controls
135 lines (121 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
name: API Validate
on:
workflow_dispatch:
push:
branches:
- main
- "feat/**"
paths:
- ".github/workflows/api-validate.yml"
- "api-server/**"
- "package.json"
pull_request:
paths:
- ".github/workflows/api-validate.yml"
- "api-server/**"
- "package.json"
jobs:
api-validate:
runs-on: ubuntu-latest
timeout-minutes: 20
env:
API_HOST: "127.0.0.1"
API_PORT: "3001"
API_BASE_URL: "http://127.0.0.1:3001"
API_KEY_CI: ${{ secrets.API_KEY_GITHUB_ACTIONS || 'ci-fallback-api-key-1234567890abcdef' }}
NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
DATABASE_ID: ${{ secrets.DATABASE_ID }}
DATA_SOURCE_ID: ${{ secrets.DATA_SOURCE_ID }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
DEFAULT_DOCS_PAGE: "overview"
CI_FETCH_HOLD_MS: "3000"
GITHUB_REPO_URL: "https://github.com/${{ github.repository }}.git"
GITHUB_TOKEN: ${{ github.token }}
GIT_AUTHOR_NAME: "github-actions[bot]"
GIT_AUTHOR_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com"
WORKDIR: ${{ github.workspace }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: "1"
- name: Install dependencies
run: bun i --frozen-lockfile
- name: Rebuild sharp for CI environment
run: npm rebuild sharp
- name: Start local API
run: |
set -euo pipefail
bun run api:server > /tmp/api-validate-server.log 2>&1 &
echo $! > /tmp/api-validate-server.pid
- name: Wait for health endpoint
run: |
set -euo pipefail
for i in $(seq 1 60); do
if curl -sf "${API_BASE_URL}/health" >/dev/null; then
exit 0
fi
sleep 1
done
echo "API health endpoint did not become ready in time"
exit 1
- name: Run API smoke assertions
run: |
set -euo pipefail
test -n "${API_KEY_CI}"
# 401 envelope for missing auth on create-job endpoint.
HTTP_CODE=$(curl -sS -o /tmp/api-validate-unauthorized.json -w "%{http_code}" \
-X POST "${API_BASE_URL}/jobs" \
-H "Content-Type: application/json" \
-d '{"type":"fetch-ready","options":{"dryRun":true,"maxPages":1}}')
test "${HTTP_CODE}" = "401"
jq -e '.status == "failed" and .error.code == "UNAUTHORIZED" and (.jobId | not)' /tmp/api-validate-unauthorized.json >/dev/null
# Sequential 202 (accepted) then immediate 409 (lock held by CI_FETCH_HOLD_MS).
HTTP_CODE=$(curl -sS -o /tmp/api-validate-job-1.json -w "%{http_code}" \
-X POST "${API_BASE_URL}/jobs" \
-H "Authorization: Bearer ${API_KEY_CI}" \
-H "Content-Type: application/json" \
-d '{"type":"fetch-ready","options":{"dryRun":true,"maxPages":1}}')
test "${HTTP_CODE}" = "202"
JOB_ID=$(jq -r '.jobId' /tmp/api-validate-job-1.json)
test -n "${JOB_ID}"
test "${JOB_ID}" != "null"
jq -e '.status == "pending"' /tmp/api-validate-job-1.json >/dev/null
HTTP_CODE=$(curl -sS -o /tmp/api-validate-job-2.json -w "%{http_code}" \
-X POST "${API_BASE_URL}/jobs" \
-H "Authorization: Bearer ${API_KEY_CI}" \
-H "Content-Type: application/json" \
-d '{"type":"fetch-all","options":{"dryRun":true,"maxPages":1}}')
test "${HTTP_CODE}" = "409"
jq -e '.status == "failed" and .error.code == "CONFLICT" and (.jobId | not)' /tmp/api-validate-job-2.json >/dev/null
# Poll the accepted fetch-ready job to terminal state.
STATUS=""
for i in $(seq 1 180); do
curl -sS \
-H "Authorization: Bearer ${API_KEY_CI}" \
"${API_BASE_URL}/jobs/${JOB_ID}" > /tmp/api-validate-job-status.json
STATUS=$(jq -r '.status' /tmp/api-validate-job-status.json)
if [ "${STATUS}" = "completed" ] || [ "${STATUS}" = "failed" ]; then
break
fi
sleep 1
done
test "${STATUS}" = "completed"
jq -e '.dryRun == true and .commitHash == null and (.pagesProcessed | type == "number")' /tmp/api-validate-job-status.json >/dev/null
- name: Cleanup local API
if: always()
run: |
set +e
if [ -f /tmp/api-validate-server.pid ]; then
PID="$(cat /tmp/api-validate-server.pid)"
if [ -n "${PID}" ] && kill -0 "${PID}" 2>/dev/null; then
kill "${PID}" 2>/dev/null || true
sleep 1
fi
fi
if [ -f /tmp/api-validate-server.log ]; then
echo "=== api-validate-server.log ==="
tail -n 200 /tmp/api-validate-server.log || true
fi