fix: use POST for new files, PUT only for updates

This commit is contained in:
sit002 2025-12-02 12:58:11 +08:00
parent f54d84136c
commit 3105d27d75

View File

@ -238,28 +238,35 @@ jobs:
# Base64 编码
CONTENT=$(base64 -w 0 "$REPORT_FILE")
# 检查文件是否存在
SHA=$(curl -s -H "Authorization: token $TOKEN" \
"$API_URL/repos/$REPO/contents/$DEST_PATH" \
| python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('sha','') if isinstance(d,dict) else '')" 2>/dev/null || echo "")
# 构建请求
if [ -n "$SHA" ]; then
JSON_DATA="{\"message\": \"Update grade report for $SHORT_SHA\", \"content\": \"$CONTENT\", \"sha\": \"$SHA\"}"
else
JSON_DATA="{\"message\": \"Add grade report for $SHORT_SHA\", \"content\": \"$CONTENT\"}"
fi
# 上传文件
RESULT=$(curl -s -X PUT -H "Authorization: token $TOKEN" \
# 先尝试 POST 创建新文件
RESULT=$(curl -s -X POST -H "Authorization: token $TOKEN" \
-H "Content-Type: application/json" \
"$API_URL/repos/$REPO/contents/$DEST_PATH" \
-d "$JSON_DATA")
-d "{\"message\": \"Add grade report for $SHORT_SHA\", \"content\": \"$CONTENT\"}")
if echo "$RESULT" | grep -q '"content"'; then
echo "✅ Report uploaded to $DEST_PATH"
else
echo "⚠️ Failed to upload report: $RESULT"
# POST 失败,可能文件已存在,尝试获取 SHA 并 PUT 更新
echo "POST failed, trying PUT with SHA..."
SHA=$(curl -s -H "Authorization: token $TOKEN" \
"$API_URL/repos/$REPO/contents/$DEST_PATH" \
| python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('sha','') if isinstance(d,dict) and 'sha' in d else '')" 2>/dev/null || echo "")
if [ -n "$SHA" ]; then
RESULT=$(curl -s -X PUT -H "Authorization: token $TOKEN" \
-H "Content-Type: application/json" \
"$API_URL/repos/$REPO/contents/$DEST_PATH" \
-d "{\"message\": \"Update grade report for $SHORT_SHA\", \"content\": \"$CONTENT\", \"sha\": \"$SHA\"}")
if echo "$RESULT" | grep -q '"content"'; then
echo "✅ Report updated at $DEST_PATH"
else
echo "⚠️ Failed to update report: $RESULT"
fi
else
echo "⚠️ Could not get file SHA, upload failed"
fi
fi
fi