forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbless-python-backend.sh
More file actions
executable file
·197 lines (179 loc) · 5.92 KB
/
Copy pathbless-python-backend.sh
File metadata and controls
executable file
·197 lines (179 loc) · 5.92 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
#!/usr/bin/env bash
# Bless a Python backend SHA by running required checks and publishing evidence.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BACKEND_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
REPO_ROOT="$(cd "$BACKEND_DIR/.." && pwd)"
TARGET_REF="HEAD"
REPO="BasedHardware/omi"
KEEP_EVIDENCE=0
usage() {
cat <<'USAGE'
Bless a Python backend SHA (tests + workflow contracts + OpenAPI evidence).
Usage:
backend/scripts/bless-python-backend.sh [--ref <git-ref>] [--repo owner/name] [--keep-evidence]
The script creates or updates GitHub Release python-backend-bless-<full-sha> and
uploads python-backend-bless-evidence-<short-sha>-<timestamp>.json.
USAGE
}
while [[ $# -gt 0 ]]; do
case "$1" in
--ref)
if [[ $# -lt 2 || "${2:-}" == -* ]]; then
echo "--ref requires a git ref value" >&2
usage >&2
exit 2
fi
TARGET_REF="${2:-}"
shift 2
;;
--repo)
if [[ $# -lt 2 || "${2:-}" == -* ]]; then
echo "--repo requires an owner/name value" >&2
usage >&2
exit 2
fi
REPO="${2:-}"
shift 2
;;
--keep-evidence)
KEEP_EVIDENCE=1
shift
;;
--help|-h)
usage
exit 0
;;
-*)
echo "unknown option: $1" >&2
usage >&2
exit 2
;;
*)
echo "unexpected argument: $1" >&2
usage >&2
exit 2
;;
esac
done
if [[ -z "$TARGET_REF" || -z "$REPO" ]]; then
usage >&2
exit 2
fi
if ! command -v gh >/dev/null 2>&1; then
echo "bless-python-backend.sh requires gh CLI" >&2
exit 1
fi
TARGET_SHA="$(git -C "$REPO_ROOT" rev-parse "$TARGET_REF^{commit}")"
CURRENT_SHA="$(git -C "$REPO_ROOT" rev-parse HEAD)"
if [[ "$CURRENT_SHA" != "$TARGET_SHA" ]]; then
echo "ref $TARGET_REF resolves to $TARGET_SHA, but this checkout is at $CURRENT_SHA" >&2
echo "Check out the target commit before blessing so evidence is collected from the blessed source." >&2
exit 1
fi
if [[ -n "$(git -C "$REPO_ROOT" status --porcelain --untracked-files=no)" ]]; then
echo "worktree has tracked changes; refusing to bless dirty source" >&2
git -C "$REPO_ROOT" status --short --untracked-files=no >&2
exit 1
fi
SHORT_SHA="${TARGET_SHA:0:12}"
STAMP="$(date -u +%Y%m%dT%H%M%SZ)"
ISO_STAMP="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
BLESS_TAG="python-backend-bless-${TARGET_SHA}"
EVIDENCE_DIR="$(mktemp -d "${TMPDIR:-/tmp}/python-backend-bless.XXXXXX")"
EVIDENCE_FILE="$EVIDENCE_DIR/python-backend-bless-evidence-${SHORT_SHA}-${STAMP}.json"
BODY_FILE="$EVIDENCE_DIR/release-body.md"
COMMANDS_FILE="$EVIDENCE_DIR/commands.jsonl"
cleanup() {
if [[ "$KEEP_EVIDENCE" -eq 0 ]]; then
rm -rf "$EVIDENCE_DIR"
else
echo "kept evidence directory: $EVIDENCE_DIR"
fi
}
trap cleanup EXIT
run_evidence_command() {
local name="$1"
shift
local log_file="$EVIDENCE_DIR/${name}.log"
local started_at ended_at status
started_at="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "running $name: $*"
set +e
(cd "$BACKEND_DIR" && "$@") >"$log_file" 2>&1
status=$?
set -e
ended_at="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
python3 - "$COMMANDS_FILE" "$name" "$started_at" "$ended_at" "$status" "$log_file" "$@" <<'PY'
import json
import sys
path, name, started_at, ended_at, status, log_file, *command = sys.argv[1:]
with open(path, "a", encoding="utf-8") as f:
f.write(
json.dumps(
{
"name": name,
"command": command,
"started_at": started_at,
"ended_at": ended_at,
"exit_code": int(status),
"log": log_file,
},
sort_keys=True,
)
+ "\n"
)
PY
if [[ "$status" -ne 0 ]]; then
echo "$name failed; see $log_file" >&2
tail -80 "$log_file" >&2 || true
return "$status"
fi
}
run_evidence_command test-preflight bash test-preflight.sh
run_evidence_command backend-test bash test.sh
run_evidence_command workflow-contracts python3 scripts/check_workflow_contracts.py
run_evidence_command openapi-public scripts/openapi_runner.sh scripts/export_openapi.py --check ../docs/api-reference/openapi.json
run_evidence_command openapi-app-client scripts/openapi_runner.sh scripts/export_openapi.py --surface app-client --check ../docs/api-reference/app-client-openapi.json
run_evidence_command openapi-integration-public scripts/openapi_runner.sh scripts/export_openapi.py --surface integration-public --check ../docs/api-reference/integration-public-openapi.json
python3 - "$COMMANDS_FILE" "$EVIDENCE_FILE" "$TARGET_SHA" "$ISO_STAMP" <<'PY'
import json
import sys
commands_path, evidence_path, target_sha, blessed_at = sys.argv[1:]
commands = [json.loads(line) for line in open(commands_path, encoding="utf-8")]
evidence = {
"surface": "python-backend",
"sha": target_sha,
"blessed_at": blessed_at,
"tier": "unit+workflow-contracts+openapi",
"passed": all(command["exit_code"] == 0 for command in commands),
"commands": commands,
}
with open(evidence_path, "w", encoding="utf-8") as f:
json.dump(evidence, f, indent=2, sort_keys=True)
f.write("\n")
PY
cat >"$BODY_FILE" <<EOF
Python backend blessing for ${TARGET_SHA}
KEY_VALUE_START
surface: python-backend
blessed.python-backend: true
blessed.python-backend.at: ${ISO_STAMP}
blessed.python-backend.sha: ${TARGET_SHA}
blessed.python-backend.tier: unit+workflow-contracts+openapi
blessed.python-backend.evidence: $(basename "$EVIDENCE_FILE")
KEY_VALUE_END
EOF
if gh release view "$BLESS_TAG" --repo "$REPO" >/dev/null 2>&1; then
gh release edit "$BLESS_TAG" --repo "$REPO" --notes-file "$BODY_FILE" --title "Python backend blessing ${SHORT_SHA}"
else
gh release create "$BLESS_TAG" "$EVIDENCE_FILE" \
--repo "$REPO" \
--target "$TARGET_SHA" \
--title "Python backend blessing ${SHORT_SHA}" \
--notes-file "$BODY_FILE" \
--prerelease \
--latest=false
fi
gh release upload "$BLESS_TAG" "$EVIDENCE_FILE" --repo "$REPO" --clobber
echo "blessed python-backend $TARGET_SHA with evidence $(basename "$EVIDENCE_FILE")"