name: Check cpp / md pair and analyze cpp on: push: permissions: contents: read jobs: check-and-analyze: name: Check md headers and analyze cpp runs-on: ubuntu-latest-slim steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Check md headers for changed cpp files env: BEFORE: ${{ github.event.before }} AFTER: ${{ github.event.after }} run: | set -euo pipefail if [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR 4b825dc642cb6eb9a060e54bf8d69288fbee4904 "$AFTER" || true) else CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR "$BEFORE" "$AFTER" || true) fi mapfile -t CPP_FILES < <(printf '%s\n' "$CHANGED_FILES" | grep -E '\.cpp$' || true) : > /tmp/cpp_files.txt for f in "${CPP_FILES[@]}"; do [ -n "$f" ] && printf '%s\n' "$f" >> /tmp/cpp_files.txt done if [ "${#CPP_FILES[@]}" -eq 0 ]; then echo "本次 push 没有 .cpp 文件,跳过检查与分析。" exit 0 fi echo "本次 push 中的 .cpp 文件:" printf ' %s\n' "${CPP_FILES[@]}" EXIT_CODE=0 for cpp_file in "${CPP_FILES[@]}"; do [ -z "$cpp_file" ] && continue dir=$(dirname "$cpp_file") base=$(basename "$cpp_file" .cpp) if [ "$dir" = "." ]; then md_file="${base}.md" else md_file="${dir}/${base}.md" fi if [ -f "$md_file" ]; then FIRST_LINE=$(head -n 1 "$md_file") if [[ "$FIRST_LINE" != "# ${base} "* ]]; then echo "::error file=${md_file},line=1::Markdown 第一行格式错误。期望以 '# ${base} ' 开头,实际为: ${FIRST_LINE}" EXIT_CODE=1 else echo "✅ ${md_file} 第一行格式正确: ${FIRST_LINE}" fi else echo "⏭️ ${cpp_file} 没有对应的 ${md_file},跳过 md 标题检查。" fi done exit $EXIT_CODE - name: Submit changed cpp files for analysis env: API_KEY: ${{ secrets.API_KEY }} run: | set -euo pipefail API_BASE="https://oia.vblg.top" if [ ! -s /tmp/cpp_files.txt ]; then echo "没有需要分析的 .cpp 文件。" exit 0 fi while IFS= read -r cpp_file; do [ -z "$cpp_file" ] && continue echo "提交分析: $cpp_file" BASE=$(basename "$cpp_file" .cpp) CODE=$(cat "$cpp_file") PAYLOAD=$(jq -n \ --arg problem_id "$BASE" \ --arg problem "" \ --arg code "$CODE" \ --arg notes "" \ --arg language "cpp17" \ '{ problem_id: $problem_id, problem: $problem, code: $code, notes: $notes, language: $language }') HTTP_CODE=$(curl -sS -o /tmp/resp.json -w "%{http_code}" \ -X POST "${API_BASE}/api/v1/analyze" \ -H "Authorization: Bearer ${API_KEY}" \ -H "Content-Type: application/json" \ -d "$PAYLOAD") if [ "$HTTP_CODE" != "202" ]; then echo "::error file=${cpp_file}::提交分析失败 (HTTP ${HTTP_CODE})" cat /tmp/resp.json || true exit 1 fi echo "已提交: $(cat /tmp/resp.json)" done < /tmp/cpp_files.txt