4
0

sync-game-labels.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. # sync-game-labels.sh
  3. # Reads lgsm/data/serverlist.csv and ensures a "game: <name>" label exists in
  4. # the GitHub repo for every unique game name. Safe to run multiple times.
  5. #
  6. # Requires: gh CLI authenticated with issues:write scope.
  7. # Usage: .github/scripts/sync-game-labels.sh [OWNER/REPO]
  8. #
  9. # The OWNER/REPO argument is optional; if omitted gh uses the current repo.
  10. set -euo pipefail
  11. REPO="${1:-}"
  12. SERVERLIST="lgsm/data/serverlist.csv"
  13. LABEL_COLOR="5b21b6"
  14. LABEL_PREFIX="game: "
  15. normalize_label() {
  16. printf '%s' "$1" | tr '[:upper:]' '[:lower:]'
  17. }
  18. if [[ ! -f "${SERVERLIST}" ]]; then
  19. echo "ERROR: ${SERVERLIST} not found. Run from the repository root."
  20. exit 1
  21. fi
  22. declare -A EXISTING_COLORS=()
  23. declare -A EXISTING_DESCRIPTIONS=()
  24. declare -A EXISTING_NAMES=()
  25. # Fetch all existing game label metadata once (up to 1000) and cache locally.
  26. echo "Fetching existing labels..."
  27. while IFS=$'\t' read -r NAME COLOR DESCRIPTION; do
  28. [[ -n "${NAME}" ]] || continue
  29. EXISTING_COLORS["${NAME}"]="${COLOR}"
  30. EXISTING_DESCRIPTIONS["${NAME}"]="${DESCRIPTION}"
  31. EXISTING_NAMES["$(normalize_label "${NAME}")"]="${NAME}"
  32. done < <(
  33. gh label list --limit 1000 --json name,color,description ${REPO:+--repo "$REPO"} \
  34. | jq -r '.[] | select(.name | startswith("game: ")) | [.name, .color, (.description // "")] | @tsv'
  35. )
  36. # Parse unique game names from the CSV (column 3, skip header).
  37. mapfile -t GAMES < <(
  38. tail -n +2 "${SERVERLIST}" \
  39. | cut -d',' -f3 \
  40. | sort -u
  41. )
  42. CREATED=0
  43. UPDATED=0
  44. UNCHANGED=0
  45. for GAME in "${GAMES[@]}"; do
  46. LABEL="${LABEL_PREFIX}${GAME}"
  47. DESCRIPTION="Issues related to ${GAME}"
  48. NORMALIZED_LABEL="$(normalize_label "${LABEL}")"
  49. if [[ -v EXISTING_NAMES["${NORMALIZED_LABEL}"] ]]; then
  50. CURRENT_LABEL="${EXISTING_NAMES["${NORMALIZED_LABEL}"]}"
  51. CURRENT_COLOR="${EXISTING_COLORS["${CURRENT_LABEL}"]}"
  52. CURRENT_DESCRIPTION="${EXISTING_DESCRIPTIONS["${CURRENT_LABEL}"]}"
  53. if [[ "${CURRENT_LABEL}" != "${LABEL}" || "${CURRENT_COLOR}" != "${LABEL_COLOR}" || "${CURRENT_DESCRIPTION}" != "${DESCRIPTION}" ]]; then
  54. echo " update ${LABEL}"
  55. gh label edit "${CURRENT_LABEL}" \
  56. --name "${LABEL}" \
  57. --color "${LABEL_COLOR}" \
  58. --description "${DESCRIPTION}" \
  59. ${REPO:+--repo "$REPO"}
  60. ((UPDATED++)) || true
  61. else
  62. echo " ok ${LABEL}"
  63. ((UNCHANGED++)) || true
  64. fi
  65. else
  66. echo " create ${LABEL}"
  67. gh label create "${LABEL}" \
  68. --color "${LABEL_COLOR}" \
  69. --description "${DESCRIPTION}" \
  70. ${REPO:+--repo "$REPO"}
  71. ((CREATED++)) || true
  72. fi
  73. done
  74. echo ""
  75. echo "Done. Created: ${CREATED} Updated: ${UPDATED} Unchanged: ${UNCHANGED}"