From 3105d27d75b10bfadc815691dc489e0c5c7cf54a Mon Sep 17 00:00:00 2001 From: sit002 Date: Tue, 2 Dec 2025 12:58:11 +0800 Subject: [PATCH] fix: use POST for new files, PUT only for updates --- .gitea/workflows/autograde.yml | 39 ++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/.gitea/workflows/autograde.yml b/.gitea/workflows/autograde.yml index 53dccde..36d6d1c 100644 --- a/.gitea/workflows/autograde.yml +++ b/.gitea/workflows/autograde.yml @@ -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