1
0

check-issues.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env bash
  2. # Surface open GitHub issues you may not have seen — externally-authored and stale.
  3. #
  4. # Read-only (gh issue list). Built to flag the blind spot: issues filed by someone
  5. # other than the repo owner, and issues left untouched for a while. Designed to run
  6. # advisory at push-time without ever gating the push.
  7. #
  8. # Usage: check-issues.sh [--repo OWNER/REPO | --remote NAME] [--stale-days N]
  9. # [--limit N] [--advisory] [--json] [-h|--help]
  10. # Input: argv only. Default repo = derived from the 'origin' remote of the cwd.
  11. # Output: stdout = data (human summary, or --json envelope). Framing on stderr.
  12. # Stderr: headers, the advisory banner, skip notices, errors.
  13. # Exit: 0 nothing you're missing (no open issues, or all are yours and fresh)
  14. # 2 usage
  15. # 5 gh not installed (standalone mode; --advisory downgrades this to a skip)
  16. # 7 unavailable — not a GitHub remote, gh not authed, offline, rate-limited,
  17. # or the lookup timed out (ADVISORY signal; never a real failure)
  18. # 10 open external and/or stale issues present (the thing to look at)
  19. #
  20. # Examples:
  21. # check-issues.sh # origin of the cwd
  22. # check-issues.sh --repo 0xDarkMatter/flarecrawl
  23. # check-issues.sh --remote origin --stale-days 14
  24. # check-issues.sh --json | jq '.data[] | select(.external)'
  25. # check-issues.sh --advisory --remote origin # compact, silent when clean
  26. set -uo pipefail
  27. EX_OK=0; EX_USAGE=2; EX_MISSING_DEP=5; EX_UNAVAILABLE=7; EX_FINDINGS=10
  28. GH_TIMEOUT="${GH_TIMEOUT:-15}" # seconds; bounds the network call
  29. # Terminal design system (skills/_lib/term.sh). Framing prints to stderr, so detect
  30. # color on fd 2. Degrade to plain output if the shared lib isn't reachable.
  31. __lib="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../_lib" 2>/dev/null && pwd || true)"
  32. if [ -n "${__lib:-}" ] && [ -f "$__lib/term.sh" ]; then . "$__lib/term.sh"; term_init 2
  33. else
  34. term_panel_open() { printf '== %s %s ==\n' "${2:-}" "${3:-}"; }
  35. term_panel_close() { [ -n "${1:-}" ] && printf '%s\n' "$1"; }
  36. term_panel_vert() { :; }
  37. term_panel_line() { printf ' %s\n' "$*"; }
  38. term_color() { shift; printf '%s' "$*"; }
  39. term_mark() { case "${1:-}" in ok) printf '+';; bad|gap) printf 'x';; warn) printf '!';; skip|na) printf '-';; unknown) printf '?';; *) printf '.';; esac; }
  40. term_health() { shift; printf '%s' "$*"; }
  41. TERM_ARROW="->"
  42. fi
  43. REPO=""; REMOTE="origin"; STALE_DAYS=30; LIMIT=50; ADVISORY=0; JSON=0
  44. while [ $# -gt 0 ]; do
  45. case "$1" in
  46. --repo) REPO="${2:?--repo needs OWNER/REPO}"; shift 2 ;;
  47. --remote) REMOTE="${2:?--remote needs a name}"; shift 2 ;;
  48. --stale-days) STALE_DAYS="${2:?--stale-days needs N}"; shift 2 ;;
  49. --limit) LIMIT="${2:?--limit needs N}"; shift 2 ;;
  50. --advisory) ADVISORY=1; shift ;;
  51. --json) JSON=1; shift ;;
  52. -h|--help) sed -n '2,30p' "$0" | sed 's/^# \{0,1\}//'; exit "$EX_OK" ;;
  53. *) echo "check-issues: unknown argument: $1" >&2; exit "$EX_USAGE" ;;
  54. esac
  55. done
  56. # In advisory mode, ANY inability to check is a silent skip (never disturb a push).
  57. skip() { # message
  58. [ "$ADVISORY" -eq 1 ] || echo "check-issues: $1" >&2
  59. exit "$EX_UNAVAILABLE"
  60. }
  61. command -v gh >/dev/null 2>&1 || {
  62. [ "$ADVISORY" -eq 1 ] && exit "$EX_UNAVAILABLE"
  63. echo "check-issues: gh not installed (https://cli.github.com)" >&2
  64. exit "$EX_MISSING_DEP"
  65. }
  66. # Resolve OWNER/REPO from the remote if not given explicitly.
  67. if [ -z "$REPO" ]; then
  68. url="$(git remote get-url "$REMOTE" 2>/dev/null)" || skip "no '$REMOTE' remote here"
  69. case "$url" in
  70. *github.com[:/]*)
  71. # strip everything up to github.com<sep>, then a trailing .git and/or slash
  72. REPO="$(printf '%s' "$url" | sed -E 's#^.*github\.com[:/]+##; s#\.git$##; s#/$##')" ;;
  73. *) skip "remote '$REMOTE' is not a github.com repo" ;;
  74. esac
  75. fi
  76. OWNER="${REPO%%/*}"
  77. # Bounded, read-only lookup. Any failure (auth/offline/rate-limit/timeout) -> skip/7.
  78. runner() { if command -v timeout >/dev/null 2>&1; then timeout "$GH_TIMEOUT" "$@"; else "$@"; fi; }
  79. raw="$(runner gh issue list --repo "$REPO" --state open --limit "$LIMIT" \
  80. --json number,title,author,createdAt,updatedAt,labels 2>/dev/null)" \
  81. || skip "gh issue list failed for $REPO (not authed / offline / rate-limited?)"
  82. [ -n "$raw" ] || skip "empty response from gh for $REPO"
  83. # Classify with jq: external = author.login != owner; stale = updatedAt older than N days.
  84. command -v jq >/dev/null 2>&1 || skip "jq not installed"
  85. analysis="$(printf '%s' "$raw" | jq -c --arg owner "$OWNER" --argjson stale "$STALE_DAYS" '
  86. (now - ($stale * 86400)) as $cutoff
  87. | map(. + {
  88. external: (.author.login != $owner),
  89. stale: ((.updatedAt | sub("\\.[0-9]+";"") | strptime("%Y-%m-%dT%H:%M:%SZ") | mktime) < $cutoff)
  90. })
  91. | { total: length,
  92. flagged: map(select(.external or .stale)),
  93. }' 2>/dev/null)" || skip "could not parse gh output"
  94. total="$(printf '%s' "$analysis" | jq -r '.total')"
  95. flagged_n="$(printf '%s' "$analysis" | jq -r '.flagged | length')"
  96. if [ "$JSON" -eq 1 ]; then
  97. printf '%s' "$analysis" | jq -c --arg repo "$REPO" \
  98. '{data: .flagged, meta: {repo: $repo, total_open: .total, flagged: (.flagged|length), schema: "claude-mods.github-ops.check-issues/v1"}}'
  99. fi
  100. # Human / advisory output (stderr framing; the data above is the stdout product).
  101. if [ "$flagged_n" -eq 0 ]; then
  102. [ "$ADVISORY" -eq 1 ] || echo "check-issues: $REPO — $total open, none external or stale." >&2
  103. exit "$EX_OK"
  104. fi
  105. {
  106. term_panel_open github-ops "OPEN ISSUES" "$REPO $flagged_n of $total flagged"
  107. term_panel_vert
  108. while IFS= read -r ln; do term_panel_line "$ln"; done < <(printf '%s' "$analysis" | jq -r --arg m "$(term_mark warn)" '.flagged[]
  109. | "\($m) #\(.number) [\(if .external then "external" else "yours" end)\(if .stale then ",stale" else "" end)] by \(.author.login) \(.title)"')
  110. term_panel_vert
  111. term_panel_close \
  112. "$(term_color dim "${TERM_ARROW} gh issue view <n> read-only, never blocks a push")" \
  113. "$(term_health warning "$flagged_n flagged")"
  114. } >&2
  115. exit "$EX_FINDINGS"