-
-
Notifications
You must be signed in to change notification settings - Fork 0
225 lines (200 loc) · 9.26 KB
/
Copy pathcheck_ksef_openapi.yml
File metadata and controls
225 lines (200 loc) · 9.26 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
name: "Check KSeF OpenAPI changes (all environments)"
on:
schedule:
- cron: "0 22 * * *"
workflow_dispatch:
permissions:
contents: read
issues: write
concurrency:
group: check-ksef-openapi
cancel-in-progress: true
jobs:
check:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- env_name: test
url: "https://api-test.ksef.mf.gov.pl/docs/v2/openapi.json"
local_file: "spec/openapi-test.json"
- env_name: demo
url: "https://api-demo.ksef.mf.gov.pl/docs/v2/openapi.json"
local_file: "spec/openapi-demo.json"
- env_name: production
url: "https://api.ksef.mf.gov.pl/docs/v2/openapi.json"
local_file: "spec/openapi.json"
steps:
- name: Checkout test branch
uses: actions/checkout@v4
with:
ref: test
- name: Fetch remote OpenAPI spec (${{ matrix.env_name }})
id: fetch
shell: bash
run: |
set -euo pipefail
HTTP_CODE=$(curl -fsSL -w '%{http_code}' "${{ matrix.url }}" -o remote-openapi.json 2>/dev/null || true)
if [ ! -s remote-openapi.json ]; then
echo "fetch_ok=false" >> "$GITHUB_OUTPUT"
echo "http_code=$HTTP_CODE" >> "$GITHUB_OUTPUT"
echo "::warning::Failed to fetch OpenAPI spec from ${{ matrix.env_name }} (HTTP $HTTP_CODE)"
exit 0
fi
echo "fetch_ok=true" >> "$GITHUB_OUTPUT"
echo "remote_sha=$(sha256sum remote-openapi.json | awk '{print $1}')" >> "$GITHUB_OUTPUT"
# Canonical hash ignores info.description (KSeF embeds a redeploy build tag
# there — e.g. "2.5.0-te-20260512.2+0beff2cf..." — which churns on every deploy
# even when the API surface is unchanged, triggering false-positive drift alerts).
# Sorting keys makes the hash order-independent.
echo "remote_canonical_sha=$(jq -cS 'del(.info.description)' remote-openapi.json | sha256sum | awk '{print $1}')" >> "$GITHUB_OUTPUT"
# Extract version info if available
VERSION=$(python3 -c "import json; d=json.load(open('remote-openapi.json')); print(d.get('info',{}).get('version','unknown'))" 2>/dev/null || echo "unknown")
echo "remote_version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Compute local openapi.json hash
id: local
if: steps.fetch.outputs.fetch_ok == 'true'
shell: bash
run: |
set -euo pipefail
FILE="${{ matrix.local_file }}"
if [ ! -f "$FILE" ]; then
echo "local_exists=false" >> "$GITHUB_OUTPUT"
echo "local_sha=" >> "$GITHUB_OUTPUT"
echo "local_canonical_sha=" >> "$GITHUB_OUTPUT"
echo "local_version=" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "local_exists=true" >> "$GITHUB_OUTPUT"
echo "local_sha=$(sha256sum "$FILE" | awk '{print $1}')" >> "$GITHUB_OUTPUT"
echo "local_canonical_sha=$(jq -cS 'del(.info.description)' "$FILE" | sha256sum | awk '{print $1}')" >> "$GITHUB_OUTPUT"
VERSION=$(python3 -c "import json; d=json.load(open('$FILE')); print(d.get('info',{}).get('version','unknown'))" 2>/dev/null || echo "unknown")
echo "local_version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Compare
id: cmp
if: steps.fetch.outputs.fetch_ok == 'true'
shell: bash
run: |
set -euo pipefail
REMOTE="${{ steps.fetch.outputs.remote_canonical_sha }}"
LOCAL_EXISTS="${{ steps.local.outputs.local_exists }}"
LOCAL="${{ steps.local.outputs.local_canonical_sha }}"
if [ "$LOCAL_EXISTS" != "true" ]; then
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "reason=local_missing" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$REMOTE" != "$LOCAL" ]; then
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "reason=hash_differs" >> "$GITHUB_OUTPUT"
else
echo "changed=false" >> "$GITHUB_OUTPUT"
echo "reason=same" >> "$GITHUB_OUTPUT"
fi
- name: Create or update issue if changed
if: steps.cmp.outputs.changed == 'true'
uses: actions/github-script@v7
env:
ENV_NAME: ${{ matrix.env_name }}
ENV_URL: ${{ matrix.url }}
REMOTE_SHA: ${{ steps.fetch.outputs.remote_sha }}
REMOTE_VERSION: ${{ steps.fetch.outputs.remote_version }}
LOCAL_EXISTS: ${{ steps.local.outputs.local_exists }}
LOCAL_SHA: ${{ steps.local.outputs.local_sha }}
LOCAL_VERSION: ${{ steps.local.outputs.local_version }}
REASON: ${{ steps.cmp.outputs.reason }}
LOCAL_FILE: ${{ matrix.local_file }}
with:
script: |
const envName = process.env.ENV_NAME;
const envUrl = process.env.ENV_URL;
const remoteSha = process.env.REMOTE_SHA || "";
const remoteVersion = process.env.REMOTE_VERSION || "";
const localExists = process.env.LOCAL_EXISTS || "";
const localSha = process.env.LOCAL_SHA || "";
const localVersion = process.env.LOCAL_VERSION || "";
const reason = process.env.REASON || "";
const title = `KSeF OpenAPI changed — ${envName} environment`;
const body = [
`Wykryto zmianę w specu OpenAPI KSeF na środowisku **${envName}** względem \`${process.env.LOCAL_FILE}\`.`,
"",
`| | Local | Remote (${envName}) |`,
"|---|---|---|",
`| **Version** | ${localVersion || "(brak pliku)"} | ${remoteVersion} |`,
`| **SHA256** | \`${localSha || "(brak pliku)"}\` | \`${remoteSha}\` |`,
"",
`- **URL:** ${envUrl}`,
`- **Powód:** ${reason}`,
"",
`**Changelog:** https://github.com/CIRFMF/ksef-docs/blob/main/api-changelog.md`,
"",
"Sugestia:",
`- pobierz aktualny spec i zaktualizuj \`${process.env.LOCAL_FILE}\`.`,
`- jeśli zmiana na **${envName}** — przygotuj się na update produkcji.`
].join("\n");
// Search for existing open issue for this environment
const searchTitle = `KSeF OpenAPI changed — ${envName}`;
const { data: issues } = await github.rest.search.issuesAndPullRequests({
q: `repo:${context.repo.owner}/${context.repo.repo} is:issue is:open in:title "${searchTitle}"`
});
if (issues.items.length > 0) {
const issue_number = issues.items[0].number;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
body
});
} else {
await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title,
body,
labels: ["ksef-api"]
});
}
- name: Send Pushover notification if changed
if: steps.cmp.outputs.changed == 'true'
shell: bash
env:
PUSHOVER_TOKEN: ${{ secrets.PUSHOVER_APP_TOKEN }}
PUSHOVER_USER: ${{ secrets.PUSHOVER_USER_KEY }}
run: |
if [ -z "$PUSHOVER_TOKEN" ] || [ -z "$PUSHOVER_USER" ]; then
echo "::warning::Pushover secrets not configured — skipping notification"
exit 0
fi
ENV_NAME="${{ matrix.env_name }}"
LOCAL_VER="${{ steps.local.outputs.local_version }}"
REMOTE_VER="${{ steps.fetch.outputs.remote_version }}"
TITLE="KSeF OpenAPI changed — ${ENV_NAME}"
MESSAGE="Spec OpenAPI KSeF zmienił się na środowisku ${ENV_NAME}. Local: ${LOCAL_VER:-brak} -> Remote: ${REMOTE_VER:-unknown}. Powód: ${{ steps.cmp.outputs.reason }}"
curl -s \
--form-string "token=$PUSHOVER_TOKEN" \
--form-string "user=$PUSHOVER_USER" \
--form-string "title=$TITLE" \
--form-string "message=$MESSAGE" \
--form-string "priority=0" \
--form-string "url=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" \
--form-string "url_title=GitHub Actions run" \
https://api.pushover.net/1/messages.json
- name: Summary
if: always()
run: |
ENV="${{ matrix.env_name }}"
FETCH_OK="${{ steps.fetch.outputs.fetch_ok }}"
if [ "$FETCH_OK" != "true" ]; then
echo "### ⚠️ $ENV — fetch failed (HTTP ${{ steps.fetch.outputs.http_code }})" >> $GITHUB_STEP_SUMMARY
else
CHANGED="${{ steps.cmp.outputs.changed }}"
REASON="${{ steps.cmp.outputs.reason }}"
LOCAL_VER="${{ steps.local.outputs.local_version }}"
REMOTE_VER="${{ steps.fetch.outputs.remote_version }}"
echo "### $ENV" >> $GITHUB_STEP_SUMMARY
echo "- Changed: $CHANGED" >> $GITHUB_STEP_SUMMARY
echo "- Reason: $REASON" >> $GITHUB_STEP_SUMMARY
echo "- Local version: $LOCAL_VER" >> $GITHUB_STEP_SUMMARY
echo "- Remote version: $REMOTE_VER" >> $GITHUB_STEP_SUMMARY
fi