fix: gitea controller
This commit is contained in:
@@ -150,6 +150,89 @@ gcr_gitea_job_state() {
|
||||
| jq -r '.status + ":" + (.conclusion // "")'
|
||||
}
|
||||
|
||||
# List every repository job without a status filter. Each line is compact JSON;
|
||||
# complete pagination and strict schema validation make absence authoritative.
|
||||
gcr_gitea_list_jobs() {
|
||||
jobs_repo="$1"
|
||||
gcr_gitea_admin_token >/dev/null || return 1
|
||||
jobs_owner="${jobs_repo%%/*}"
|
||||
jobs_name="${jobs_repo#*/}"
|
||||
jobs_page=1
|
||||
while :; do
|
||||
jobs_json="$(gcr_gitea_curl -fsS \
|
||||
"$GCR_GITEA_URL/api/v1/repos/$jobs_owner/$jobs_name/actions/jobs?page=$jobs_page&limit=50")" \
|
||||
|| return 1
|
||||
printf '%s' "$jobs_json" | jq -e \
|
||||
'.jobs | select(type == "array")
|
||||
| all(.[];
|
||||
(.id | type) == "number" and .id > 0
|
||||
and (.run_attempt | type) == "number" and .run_attempt >= 0
|
||||
and (.status | type) == "string" and (.status | length) > 0
|
||||
and ((.conclusion | type) == "string" or
|
||||
(.conclusion | type) == "null")
|
||||
and ((.runner_id | type) == "number" or
|
||||
(.runner_id | type) == "null")
|
||||
and ((.runner_name | type) == "string" or
|
||||
(.runner_name | type) == "null"))' \
|
||||
>/dev/null || return 1
|
||||
printf '%s' "$jobs_json" | jq -c \
|
||||
'.jobs[] | {id, run_attempt, status,
|
||||
conclusion:(.conclusion // ""),
|
||||
runner_id:(.runner_id // null),
|
||||
runner_name:(.runner_name // "")}' || return 1
|
||||
jobs_count="$(printf '%s' "$jobs_json" | jq -er '.jobs | length')" \
|
||||
|| return 1
|
||||
[ "$jobs_count" -lt 50 ] && return 0
|
||||
jobs_page=$((jobs_page + 1))
|
||||
done
|
||||
}
|
||||
|
||||
# Exit 0 only when complete unfiltered history proves no active repository job.
|
||||
gcr_gitea_repo_has_active_jobs() {
|
||||
active_jobs="$(gcr_gitea_list_jobs "$1")" || return 2
|
||||
if printf '%s\n' "$active_jobs" \
|
||||
| jq -s -e 'any(.[]; .status == "in_progress")' >/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# Confirm the current job belonging to repository-scoped RUNNER_ID and require
|
||||
# that it is the requested job/attempt. One active match takes precedence over
|
||||
# terminal history; without an active match, exactly one terminal match is
|
||||
# required. Prints current authoritative state; event state is not trusted.
|
||||
gcr_gitea_confirm_runner_job() {
|
||||
confirm_repo="$1"; confirm_runner="$2"
|
||||
confirm_job="$3"; confirm_attempt="$4"
|
||||
case "$confirm_runner:$confirm_job:$confirm_attempt" in
|
||||
*[!0-9:]*|0:*|*:0:*|:*|*::*|*:) return 1 ;;
|
||||
esac
|
||||
confirm_jobs="$(gcr_gitea_list_jobs "$confirm_repo")" || return 1
|
||||
confirm_matches="$(printf '%s\n' "$confirm_jobs" | jq -cs \
|
||||
--arg runner "$confirm_runner" \
|
||||
'[.[] | select((.runner_id | tostring) == $runner)]')" || return 1
|
||||
confirm_selected="$(printf '%s' "$confirm_matches" | jq -ce '
|
||||
[.[] | select(.status == "in_progress")] as $active
|
||||
| if ($active | length) == 1 then $active[0]
|
||||
elif ($active | length) > 1 then empty
|
||||
else [.[] | select(.status == "completed")] as $terminal
|
||||
| if ($terminal | length) == 1 then $terminal[0] else empty end
|
||||
end')" || return 1
|
||||
printf '%s' "$confirm_selected" | jq -e --arg job "$confirm_job" \
|
||||
--arg attempt "$confirm_attempt" \
|
||||
'select((.id | tostring) == $job
|
||||
and (.run_attempt | tostring) == $attempt)' \
|
||||
>/dev/null || return 1
|
||||
confirm_status="$(printf '%s' "$confirm_selected" | jq -r '.status')"
|
||||
confirm_conclusion="$(printf '%s' "$confirm_selected" | jq -r \
|
||||
'.conclusion')"
|
||||
case "$confirm_status" in
|
||||
in_progress) printf 'vm_active' ;;
|
||||
completed) printf 'completed:%s' "${confirm_conclusion:-unknown}" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Exit 0 when an in-progress repository job is assigned to RUNNER_ID, 1 when
|
||||
# complete pagination proves none, and 2 on API or schema uncertainty.
|
||||
gcr_gitea_runner_has_active_job() {
|
||||
|
||||
Reference in New Issue
Block a user