5 Commits

2 changed files with 52 additions and 17 deletions

View File

@@ -90,25 +90,43 @@ function api_request() {
local url="$GITEA_URL/api/v1/$endpoint" local url="$GITEA_URL/api/v1/$endpoint"
local -a curl_opts 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 case "$method" in
GET) GET)
curl "${curl_opts[@]}" "$url" response=$(curl "${curl_opts[@]}" "$url")
;; ;;
POST|PATCH) POST|PATCH)
curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data" response=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data")
;; ;;
DELETE) DELETE)
# Для DELETE запросов тело не нужно, и curl вернет ошибку если его передать response=$(curl "${curl_opts[@]}" -X "$method" "$url")
# Gitea на успешное удаление часто возвращает код 204 No Content с пустым телом
curl -o /dev/null -w "%{http_code}" "${curl_opts[@]}" -X "$method" "$url"
;; ;;
*) *)
echo "Unsupported HTTP method: $method" >&2 echo "Unsupported HTTP method: $method" >&2
return 1 return 1
;; ;;
esac 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')] # [END_ENTITY: 'Function'('api_request')]
@@ -359,33 +377,50 @@ function merge_and_complete() {
# 1. Merge the PR # 1. Merge the PR
echo "Attempting to merge PR #$pr_id..." echo "Attempting to merge PR #$pr_id..."
local merge_data=$(jq -n '{Do: "merge"}' ) # Gitea API expects a MergePullRequestOption object local merge_data=$(jq -n '{Do: "merge"}' )
local merge_response=$(api_request "POST" "repos/$GITEA_OWNER/$GITEA_REPO/pulls/$pr_id/merge" "$merge_data") # Запускаем в подоболочке, чтобы обработать возможную ошибку, если api_request вернет 1
if echo "$merge_response" | jq -e '.merged' > /dev/null; then 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." echo "PR #$pr_id merged successfully."
else else
echo "Error merging PR #$pr_id: $merge_response" >&2 # Если тело не пустое, это может быть тоже успех (старые версии Gitea) или ошибка
return 1 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 fi
# 2. Delete the branch # 2. Delete the branch
echo "Attempting to delete branch $branch_to_delete..." 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 api_request "DELETE" "repos/$GITEA_OWNER/$GITEA_REPO/branches/$branch_to_delete" > /dev/null; then
if [[ "$delete_http_code" == "204" ]]; then # 204 No Content is success
echo "Branch $branch_to_delete deleted successfully." echo "Branch $branch_to_delete deleted successfully."
else 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 fi
# 3. Close the associated issue # 3. Close the associated issue
echo "Attempting to close issue #$issue_id..." echo "Attempting to close issue #$issue_id..."
local close_issue_data=$(jq -n '{state: "closed"}') 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 if echo "$close_response" | jq -e '.state == "closed"' > /dev/null; then
echo "Issue #$issue_id closed successfully." echo "Issue #$issue_id closed successfully."
else else
echo "Error closing issue #$issue_id: $close_response" >&2 echo "Error closing issue #$issue_id: Unexpected API response: $close_response" >&2
return 1 return 1
fi fi
} }

0
test_file_1757081493.txt Normal file
View File