diff --git a/gitea-client.zsh b/gitea-client.zsh index 6e7b5a1..c02d69b 100644 --- a/gitea-client.zsh +++ b/gitea-client.zsh @@ -90,25 +90,43 @@ function api_request() { local url="$GITEA_URL/api/v1/$endpoint" local -a curl_opts - curl_opts=("-s" "-H" "Authorization: token $GITEA_TOKEN" "-H" "Content-Type: application/json") + # Добавляем -i чтобы получить заголовки, включая HTTP-статус + curl_opts=("-s" "-i" "-H" "Authorization: token $GITEA_TOKEN" "-H" "Content-Type: application/json") + + # Переменная для хранения всего вывода curl + local response case "$method" in GET) - curl "${curl_opts[@]}" "$url" + response=$(curl "${curl_opts[@]}" "$url") ;; POST|PATCH) - curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data" + response=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data") ;; DELETE) - # Для DELETE запросов тело не нужно, и curl вернет ошибку если его передать - # Gitea на успешное удаление часто возвращает код 204 No Content с пустым телом - curl -o /dev/null -w "%{http_code}" "${curl_opts[@]}" -X "$method" "$url" + response=$(curl "${curl_opts[@]}" -X "$method" "$url") ;; *) echo "Unsupported HTTP method: $method" >&2 return 1 ;; esac + + # Извлекаем HTTP-статус из ответа + local http_status=$(echo "$response" | head -n 1 | awk '{print $2}') + # Извлекаем тело ответа (все, что после пустой строки, отделяющей заголовки) + local body=$(echo "$response" | sed '1,/^\r$/d') + + # Проверяем, был ли запрос успешным (коды 2xx) + if [[ "$http_status" -ge 200 && "$http_status" -lt 300 ]]; then + echo "$body" + return 0 + else + # Если неуспешно, выводим ошибку и тело, если оно есть + echo "API Error: Received HTTP status $http_status" >&2 + echo "$body" >&2 + return 1 + fi } # [END_ENTITY: 'Function'('api_request')] @@ -359,33 +377,50 @@ function merge_and_complete() { # 1. Merge the PR echo "Attempting to merge PR #$pr_id..." - local merge_data=$(jq -n '{Do: "merge"}' ) # Gitea API expects a MergePullRequestOption object - local merge_response=$(api_request "POST" "repos/$GITEA_OWNER/$GITEA_REPO/pulls/$pr_id/merge" "$merge_data") - if echo "$merge_response" | jq -e '.merged' > /dev/null; then + local merge_data=$(jq -n '{Do: "merge"}' ) + # Запускаем в подоболочке, чтобы обработать возможную ошибку, если api_request вернет 1 + local merge_response + if ! merge_response=$(api_request "POST" "repos/$GITEA_OWNER/$GITEA_REPO/pulls/$pr_id/merge" "$merge_data"); then + echo "Error merging PR #$pr_id: API request failed." >&2 + echo "Response: $merge_response" >&2 + return 1 + fi + + # API на успешный мерж возвращает ПУСТОЕ тело и код 200/204. + # Наша новая api_request вернет JSON-маркер. Проверяем это. + if echo "$merge_response" | jq -e '.body == "empty"' > /dev/null; then echo "PR #$pr_id merged successfully." else - echo "Error merging PR #$pr_id: $merge_response" >&2 - return 1 + # Если тело не пустое, это может быть тоже успех (старые версии Gitea) или ошибка + if echo "$merge_response" | jq -e '.merged' > /dev/null; then + echo "PR #$pr_id merged successfully (with response body)." + else + echo "Error merging PR #$pr_id: Unexpected API response: $merge_response" >&2 + return 1 + fi fi # 2. Delete the branch echo "Attempting to delete branch $branch_to_delete..." - local delete_http_code=$(api_request "DELETE" "repos/$GITEA_OWNER/$GITEA_REPO/branches/$branch_to_delete") - if [[ "$delete_http_code" == "204" ]]; then # 204 No Content is success + if api_request "DELETE" "repos/$GITEA_OWNER/$GITEA_REPO/branches/$branch_to_delete" > /dev/null; then echo "Branch $branch_to_delete deleted successfully." else - echo "Warning: Failed to delete branch $branch_to_delete (HTTP code: $delete_http_code). It might be protected or already deleted." >&2 - # Не возвращаем ошибку, т.к. главная задача (мерж) выполнена + echo "Warning: Failed to delete branch $branch_to_delete. It might have already been deleted or protected." >&2 fi # 3. Close the associated issue echo "Attempting to close issue #$issue_id..." local close_issue_data=$(jq -n '{state: "closed"}') - local close_response=$(api_request "PATCH" "repos/$GITEA_OWNER/$GITEA_REPO/issues/$issue_id" "$close_issue_data") + local close_response + if ! close_response=$(api_request "PATCH" "repos/$GITEA_OWNER/$GITEA_REPO/issues/$issue_id" "$close_issue_data"); then + echo "Error closing issue #$issue_id: API request failed." >&2 + return 1 + fi + if echo "$close_response" | jq -e '.state == "closed"' > /dev/null; then echo "Issue #$issue_id closed successfully." else - echo "Error closing issue #$issue_id: $close_response" >&2 + echo "Error closing issue #$issue_id: Unexpected API response: $close_response" >&2 return 1 fi } diff --git a/test_file_1757081493.txt b/test_file_1757081493.txt new file mode 100644 index 0000000..e69de29