From c295e9ca3074ba013130edb76fcb7b07230af125 Mon Sep 17 00:00:00 2001 From: hanyixuanten <105723997+hanyixuanten@users.noreply.github.com> Date: Mon, 12 Oct 2026 19:42:09 +0800 Subject: [PATCH] new file: .github/workflows/analyze.yml --- .github/workflows/analyze.yml | 79 ++++++++++++++++++++++++++++++++++ .github/workflows/md-check.yml | 73 +------------------------------ 2 files changed, 81 insertions(+), 71 deletions(-) create mode 100644 .github/workflows/analyze.yml diff --git a/.github/workflows/analyze.yml b/.github/workflows/analyze.yml new file mode 100644 index 0000000..351567a --- /dev/null +++ b/.github/workflows/analyze.yml @@ -0,0 +1,79 @@ +name: Analyze cpp files + +on: + workflow_call: + +permissions: + contents: read + +jobs: + analyze: + name: Analyze cpp files + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Submit code for analysis + env: + API_KEY: ${{ secrets.API_KEY }} + run: | + set -euo pipefail + + API_BASE="https://oia.vblg.top" + + BEFORE="${{ github.event.before }}" + AFTER="${{ github.event.after }}" + + if [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then + CHANGED_FILES=$(git diff --name-only --diff-filter=ACM 4b825dc642cb6eb9a060e54bf8d69288fbee4904 "$AFTER" || true) + else + CHANGED_FILES=$(git diff --name-only --diff-filter=ACM "$BEFORE" "$AFTER" || true) + fi + + CPP_FILES=$(echo "$CHANGED_FILES" | grep -E '^[^/]+\.cpp$' || true) + + if [ -z "$CPP_FILES" ]; 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") + + # 202 = 已接受,后台执行;无需等待分析结束 + 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 <<< "$CPP_FILES" \ No newline at end of file diff --git a/.github/workflows/md-check.yml b/.github/workflows/md-check.yml index e272308..7ac9be1 100644 --- a/.github/workflows/md-check.yml +++ b/.github/workflows/md-check.yml @@ -80,76 +80,7 @@ jobs: exit $EXIT_CODE - # 校验通过后触发代码分析(异步,不等待结果) analyze: - name: Analyze cpp files needs: check-md-header - if: needs.check-md-header.outputs.has_cpp == 'true' - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Submit code for analysis - env: - API_KEY: ${{ secrets.API_KEY }} - run: | - set -euo pipefail - - API_BASE="https://oia.vblg.top" - - BEFORE="${{ github.event.before }}" - AFTER="${{ github.event.after }}" - - if [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then - CHANGED_FILES=$(git diff --name-only --diff-filter=ACM 4b825dc642cb6eb9a060e54bf8d69288fbee4904 "$AFTER" || true) - else - CHANGED_FILES=$(git diff --name-only --diff-filter=ACM "$BEFORE" "$AFTER" || true) - fi - - CPP_FILES=$(echo "$CHANGED_FILES" | grep -E '^[^/]+\.cpp$' || true) - - if [ -z "$CPP_FILES" ]; 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") - - # 202 = 已接受,后台执行;无需等待分析结束 - 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 <<< "$CPP_FILES" \ No newline at end of file + if: ${{ needs.check-md-header.outputs.has_cpp == 'true' && success() }} + uses: ./.github/workflows/analyze.yml \ No newline at end of file