Compare commits
2 Commits
test/auto-
...
test/auto-
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c722dab25 | |||
| b9bc090d11 |
71
gitea-client.zsh
Normal file → Executable file
71
gitea-client.zsh
Normal file → Executable file
@@ -90,43 +90,25 @@ 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
|
||||||
# Добавляем -i чтобы получить заголовки, включая HTTP-статус
|
curl_opts=("-s" "-H" "Authorization: token $GITEA_TOKEN" "-H" "Content-Type: application/json")
|
||||||
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)
|
||||||
response=$(curl "${curl_opts[@]}" "$url")
|
curl "${curl_opts[@]}" "$url"
|
||||||
;;
|
;;
|
||||||
POST|PATCH)
|
POST|PATCH)
|
||||||
response=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data")
|
curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data"
|
||||||
;;
|
;;
|
||||||
DELETE)
|
DELETE)
|
||||||
response=$(curl "${curl_opts[@]}" -X "$method" "$url")
|
# Для DELETE запросов тело не нужно, и curl вернет ошибку если его передать
|
||||||
|
# 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')]
|
||||||
|
|
||||||
@@ -377,50 +359,33 @@ 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"}' )
|
local merge_data=$(jq -n '{Do: "merge"}' ) # Gitea API expects a MergePullRequestOption object
|
||||||
# Запускаем в подоболочке, чтобы обработать возможную ошибку, если api_request вернет 1
|
local merge_response=$(api_request "POST" "repos/$GITEA_OWNER/$GITEA_REPO/pulls/$pr_id/merge" "$merge_data")
|
||||||
local merge_response
|
if echo "$merge_response" | jq -e '.merged' > /dev/null; then
|
||||||
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
|
||||||
# Если тело не пустое, это может быть тоже успех (старые версии Gitea) или ошибка
|
echo "Error merging PR #$pr_id: $merge_response" >&2
|
||||||
if echo "$merge_response" | jq -e '.merged' > /dev/null; then
|
return 1
|
||||||
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..."
|
||||||
if api_request "DELETE" "repos/$GITEA_OWNER/$GITEA_REPO/branches/$branch_to_delete" > /dev/null; then
|
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
|
||||||
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. It might have already been deleted or protected." >&2
|
echo "Warning: Failed to delete branch $branch_to_delete (HTTP code: $delete_http_code). It might be protected or already deleted." >&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
|
local close_response=$(api_request "PATCH" "repos/$GITEA_OWNER/$GITEA_REPO/issues/$issue_id" "$close_issue_data")
|
||||||
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: Unexpected API response: $close_response" >&2
|
echo "Error closing issue #$issue_id: $close_response" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -476,4 +441,4 @@ function return_to_dev() {
|
|||||||
}
|
}
|
||||||
# [END_ENTITY: 'Function'('return_to_dev')]
|
# [END_ENTITY: 'Function'('return_to_dev')]
|
||||||
|
|
||||||
# Здесь может быть функция main_dispatch, если она вам нужна
|
# Здесь может быть функция main_dispatch, если она вам нужна
|
||||||
Reference in New Issue
Block a user