modified: .github/workflows/md-check.yml
This commit is contained in:
1 parent
185f387b34
commit
3bb0db00ed
2 files changed
+83
-122
No files matched your search
@@ -1,79 +0,0 @@
|
|||||||
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"
|
|
||||||
@@ -1,86 +1,126 @@
|
|||||||
name: Check cpp / md pair
|
name: Check cpp / md pair and analyze cpp
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
paths:
|
paths:
|
||||||
- '*.cpp'
|
- '**/*.cpp'
|
||||||
- '*.md'
|
- '**/*.md'
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check-md-header:
|
check-and-analyze:
|
||||||
name: Check markdown header
|
name: Check md headers and analyze cpp
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
outputs:
|
|
||||||
has_cpp: ${{ steps.check.outputs.has_cpp }}
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Validate md first line
|
- name: Check md headers for changed cpp files
|
||||||
id: check
|
env:
|
||||||
|
BEFORE: ${{ github.event.before }}
|
||||||
|
AFTER: ${{ github.event.after }}
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
BEFORE="${{ github.event.before }}"
|
|
||||||
AFTER="${{ github.event.after }}"
|
|
||||||
|
|
||||||
# 处理新分支第一次 push 的情况(before 为全零)
|
|
||||||
if [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then
|
if [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then
|
||||||
CHANGED_FILES=$(git diff --name-only --diff-filter=ACM 4b825dc642cb6eb9a060e54bf8d69288fbee4904 "$AFTER" || true)
|
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR 4b825dc642cb6eb9a060e54bf8d69288fbee4904 "$AFTER" || true)
|
||||||
else
|
else
|
||||||
CHANGED_FILES=$(git diff --name-only --diff-filter=ACM "$BEFORE" "$AFTER" || true)
|
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR "$BEFORE" "$AFTER" || true)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 筛选出根目录下的 .cpp / .md 文件
|
mapfile -t CPP_FILES < <(printf '%s\n' "$CHANGED_FILES" | grep -E '\.cpp$' || true)
|
||||||
CPP_FILES=$(echo "$CHANGED_FILES" | grep -E '^[^/]+\.cpp$' || true)
|
|
||||||
MD_FILES=$(echo "$CHANGED_FILES" | grep -E '^[^/]+\.md$' || true)
|
|
||||||
|
|
||||||
if [ -z "$CPP_FILES" ]; then
|
: > /tmp/cpp_files.txt
|
||||||
echo "本次 push 没有根目录下的 .cpp 文件变更,跳过检查。"
|
for f in "${CPP_FILES[@]}"; do
|
||||||
echo "has_cpp=false" >> "$GITHUB_OUTPUT"
|
[ -n "$f" ] && printf '%s\n' "$f" >> /tmp/cpp_files.txt
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ "${#CPP_FILES[@]}" -eq 0 ]; then
|
||||||
|
echo "本次 push 没有 .cpp 文件,跳过检查与分析。"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "has_cpp=true" >> "$GITHUB_OUTPUT"
|
echo "本次 push 中的 .cpp 文件:"
|
||||||
|
printf ' %s\n' "${CPP_FILES[@]}"
|
||||||
# 若提交中没有任何 .md 变更,则跳过标题配对检查,直接放行
|
|
||||||
if [ -z "$MD_FILES" ]; then
|
|
||||||
echo "本次 push 没有根目录下的 .md 文件变更,跳过标题配对检查。"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
EXIT_CODE=0
|
EXIT_CODE=0
|
||||||
|
for cpp_file in "${CPP_FILES[@]}"; do
|
||||||
while IFS= read -r cpp_file; do
|
|
||||||
[ -z "$cpp_file" ] && continue
|
[ -z "$cpp_file" ] && continue
|
||||||
base=$(basename "$cpp_file" .cpp)
|
|
||||||
md_file="${base}.md"
|
|
||||||
|
|
||||||
if [ ! -f "$md_file" ]; then
|
dir=$(dirname "$cpp_file")
|
||||||
echo "::error file=${cpp_file}::对应的 Markdown 文件 ${md_file} 不存在"
|
base=$(basename "$cpp_file" .cpp)
|
||||||
EXIT_CODE=1
|
|
||||||
continue
|
if [ "$dir" = "." ]; then
|
||||||
|
md_file="${base}.md"
|
||||||
|
else
|
||||||
|
md_file="${dir}/${base}.md"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ -f "$md_file" ]; then
|
||||||
FIRST_LINE=$(head -n 1 "$md_file")
|
FIRST_LINE=$(head -n 1 "$md_file")
|
||||||
|
|
||||||
# 检查第一行是否匹配 "# <base> 任意内容"
|
if [[ "$FIRST_LINE" != "# ${base} "* ]]; then
|
||||||
if ! grep -qE "^# ${base} .*$" <<< "$FIRST_LINE"; then
|
|
||||||
echo "::error file=${md_file},line=1::Markdown 第一行格式错误。期望以 '# ${base} ' 开头,实际为: ${FIRST_LINE}"
|
echo "::error file=${md_file},line=1::Markdown 第一行格式错误。期望以 '# ${base} ' 开头,实际为: ${FIRST_LINE}"
|
||||||
EXIT_CODE=1
|
EXIT_CODE=1
|
||||||
else
|
else
|
||||||
echo "✅ ${md_file} 第一行格式正确: ${FIRST_LINE}"
|
echo "✅ ${md_file} 第一行格式正确: ${FIRST_LINE}"
|
||||||
fi
|
fi
|
||||||
done <<< "$CPP_FILES"
|
else
|
||||||
|
echo "⏭️ ${cpp_file} 没有对应的 ${md_file},跳过 md 标题检查。"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
exit $EXIT_CODE
|
exit $EXIT_CODE
|
||||||
|
|
||||||
analyze:
|
- name: Submit changed cpp files for analysis
|
||||||
needs: check-md-header
|
env:
|
||||||
if: ${{ needs.check-md-header.outputs.has_cpp == 'true' && success() }}
|
API_KEY: ${{ secrets.API_KEY }}
|
||||||
uses: ./.github/workflows/analyze.yml
|
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
|
||||||
Reference in new issue
Block a user