96ffe19fe9
Universal: Cascade Main → Dev / Cascade main → branches (push) Has been cancelled
Generic: Repo Health / Access control (push) Has been cancelled
Generic: Repo Health / Release configuration (push) Has been cancelled
Generic: Repo Health / Scripts governance (push) Has been cancelled
Generic: Repo Health / Repository health (push) Has been cancelled
Authored-by: Moko Consulting
124 lines
3.8 KiB
Bash
124 lines
3.8 KiB
Bash
#!/usr/bin/env bash
|
|
# Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
# BRIEF: Trigger a workflow across all client-waas repos in a Gitea org
|
|
|
|
set -euo pipefail
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Usage
|
|
# ---------------------------------------------------------------------------
|
|
usage() {
|
|
cat <<EOF
|
|
Usage: $(basename "$0") GITEA_URL TOKEN ORG WORKFLOW [REF] [INPUTS]
|
|
|
|
Arguments:
|
|
GITEA_URL Base URL of the Gitea instance (e.g. https://git.mokoconsulting.tech)
|
|
TOKEN Gitea API token with repo/action permissions
|
|
ORG Organisation or user that owns the repos
|
|
WORKFLOW Workflow filename to trigger (e.g. dependency-audit.yml)
|
|
REF Branch ref to run against (default: main)
|
|
INPUTS Optional JSON object of workflow inputs (e.g. '{"dry_run":"true"}')
|
|
|
|
Example:
|
|
$(basename "$0") https://git.mokoconsulting.tech abc123 MokoConsulting dependency-audit.yml main '{"notify":"true"}'
|
|
EOF
|
|
exit 1
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Argument parsing
|
|
# ---------------------------------------------------------------------------
|
|
if [ $# -lt 4 ]; then
|
|
usage
|
|
fi
|
|
|
|
GITEA_URL="${1%/}"
|
|
TOKEN="$2"
|
|
ORG="$3"
|
|
WORKFLOW="$4"
|
|
REF="${5:-main}"
|
|
INPUTS="${6:-{\}}"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Fetch all repos in the org, paginated
|
|
# ---------------------------------------------------------------------------
|
|
echo "Fetching repos for org '${ORG}' on ${GITEA_URL} ..."
|
|
|
|
PAGE=1
|
|
LIMIT=50
|
|
ALL_REPOS=""
|
|
|
|
while true; do
|
|
RESPONSE=$(curl -s \
|
|
-H "Authorization: token ${TOKEN}" \
|
|
-H "Accept: application/json" \
|
|
"${GITEA_URL}/api/v1/orgs/${ORG}/repos?page=${PAGE}&limit=${LIMIT}")
|
|
|
|
# Break if empty array
|
|
COUNT=$(echo "$RESPONSE" | jq -r 'length')
|
|
if [ "$COUNT" -eq 0 ]; then
|
|
break
|
|
fi
|
|
|
|
NAMES=$(echo "$RESPONSE" | jq -r '.[].name')
|
|
ALL_REPOS="${ALL_REPOS}${NAMES}"$'\n'
|
|
|
|
if [ "$COUNT" -lt "$LIMIT" ]; then
|
|
break
|
|
fi
|
|
|
|
PAGE=$((PAGE + 1))
|
|
done
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Filter for client-waas repos
|
|
# ---------------------------------------------------------------------------
|
|
CLIENT_REPOS=$(echo "$ALL_REPOS" | grep 'client-waas' | sort || true)
|
|
|
|
if [ -z "$CLIENT_REPOS" ]; then
|
|
echo "No client-waas repos found in org '${ORG}'."
|
|
exit 0
|
|
fi
|
|
|
|
TOTAL=$(echo "$CLIENT_REPOS" | wc -l | tr -d ' ')
|
|
echo "Found ${TOTAL} client-waas repo(s). Triggering workflow '${WORKFLOW}' (ref: ${REF}) ..."
|
|
echo ""
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Trigger workflow for each repo
|
|
# ---------------------------------------------------------------------------
|
|
SUCCESS=0
|
|
FAIL=0
|
|
|
|
while IFS= read -r REPO; do
|
|
[ -z "$REPO" ] && continue
|
|
|
|
PAYLOAD=$(jq -n --arg ref "$REF" --argjson inputs "$INPUTS" '{ref: $ref, inputs: $inputs}')
|
|
|
|
HTTP_CODE=$(curl -s -o /dev/null -w '%{http_code}' \
|
|
-X POST \
|
|
-H "Authorization: token ${TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$PAYLOAD" \
|
|
"${GITEA_URL}/api/v1/repos/${ORG}/${REPO}/actions/workflows/${WORKFLOW}/dispatches")
|
|
|
|
if [ "$HTTP_CODE" -eq 204 ] || [ "$HTTP_CODE" -eq 201 ]; then
|
|
echo " [OK] ${ORG}/${REPO} (HTTP ${HTTP_CODE})"
|
|
SUCCESS=$((SUCCESS + 1))
|
|
else
|
|
echo " [FAIL] ${ORG}/${REPO} (HTTP ${HTTP_CODE})"
|
|
FAIL=$((FAIL + 1))
|
|
fi
|
|
done <<< "$CLIENT_REPOS"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Summary
|
|
# ---------------------------------------------------------------------------
|
|
echo ""
|
|
echo "Done. Success: ${SUCCESS} | Failed: ${FAIL} | Total: ${TOTAL}"
|
|
|
|
if [ "$FAIL" -gt 0 ]; then
|
|
exit 1
|
|
fi
|