Compare commits
2 Commits
main
...
test/auto-
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c722dab25 | |||
| b9bc090d11 |
81
gitea-client.zsh
Normal file → Executable file
81
gitea-client.zsh
Normal file → Executable file
@@ -83,56 +83,32 @@ fi
|
|||||||
#
|
#
|
||||||
# @returns 0 on success, 1 on unsupported method. Curl exit code on curl failure.
|
# @returns 0 on success, 1 on unsupported method. Curl exit code on curl failure.
|
||||||
# [/CONTRACT]
|
# [/CONTRACT]
|
||||||
# ЗАМЕНИТЕ ВСЮ ФУНКЦИЮ api_request НА ЭТУ ВЕРСИЮ
|
|
||||||
|
|
||||||
function api_request() {
|
function api_request() {
|
||||||
local method="$1"
|
local method="$1"
|
||||||
local endpoint="$2"
|
local endpoint="$2"
|
||||||
local data="$3"
|
local data="$3"
|
||||||
local url="$GITEA_URL/api/v1/$endpoint"
|
local url="$GITEA_URL/api/v1/$endpoint"
|
||||||
|
|
||||||
local http_code
|
|
||||||
local response_body
|
|
||||||
|
|
||||||
# Создаем временный файл для хранения тела ответа
|
|
||||||
local body_file=$(mktemp)
|
|
||||||
|
|
||||||
local -a curl_opts
|
local -a curl_opts
|
||||||
# -s: silent
|
curl_opts=("-s" "-H" "Authorization: token $GITEA_TOKEN" "-H" "Content-Type: application/json")
|
||||||
# -w '%{http_code}': записать http-код в stdout ПОСЛЕ ответа
|
|
||||||
# -o "$body_file": записать тело ответа в файл
|
|
||||||
curl_opts=("-s" "-w" "%{http_code}" "-o" "$body_file" \
|
|
||||||
"-H" "Authorization: token $GITEA_TOKEN" \
|
|
||||||
"-H" "Content-Type: application/json")
|
|
||||||
|
|
||||||
case "$method" in
|
case "$method" in
|
||||||
GET|DELETE)
|
GET)
|
||||||
http_code=$(curl "${curl_opts[@]}" -X "$method" "$url")
|
curl "${curl_opts[@]}" "$url"
|
||||||
;;
|
;;
|
||||||
POST|PATCH)
|
POST|PATCH)
|
||||||
http_code=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data")
|
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"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Unsupported HTTP method: $method" >&2
|
echo "Unsupported HTTP method: $method" >&2
|
||||||
rm -f "$body_file" # Очистка перед выходом
|
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
response_body=$(<"$body_file")
|
|
||||||
rm -f "$body_file" # Очистка после использования
|
|
||||||
|
|
||||||
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
|
||||||
if [[ -z "$response_body" ]]; then
|
|
||||||
echo "{\"http_status\": $http_code, \"body\": \"empty\"}"
|
|
||||||
else
|
|
||||||
echo "$response_body"
|
|
||||||
fi
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
echo "API Error: Received HTTP status $http_code. Body: $response_body" >&2
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
# [END_ENTITY: 'Function'('api_request')]
|
# [END_ENTITY: 'Function'('api_request')]
|
||||||
|
|
||||||
@@ -383,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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user