Skip to content

Commit 16fe695

Browse files
locus313Copilot
andauthored
refactor: eliminate plan_credits duplication, _copilot_api wrapper, and test boilerplate (#43)
- Collapse plan_credits() from two identical case blocks (promo/standard) into one case keyed by plan type with inline promo check — saves ~16 lines - Inline gh_api --api-version 2026-03-10 at both call sites and delete the _copilot_api() 2-line wrapper that only existed to fix that flag - Source lib/github-common.sh in install-hooks.sh to reuse color vars (RED/GREEN/YELLOW/NC) instead of redeclaring them; keep the [HOOKS]-labelled print functions since they differ from the lib's [SUCCESS]/[WARNING]/[ERROR] - Add _run_script() helper to test_script_validation.bats and replace all 59 single-line run bash -c invocations — removes the repeated "export PATH='${MOCK_BIN}:${PATH}';" boilerplate from every test body All 91 bats tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e0a1165 commit 16fe695

3 files changed

Lines changed: 79 additions & 99 deletions

File tree

install-hooks.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
set -euo pipefail
2222

2323
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
24+
# shellcheck source=lib/github-common.sh
25+
source "${SCRIPT_DIR}/lib/github-common.sh"
2426

25-
RED='\033[0;31m'
26-
GREEN='\033[0;32m'
27-
YELLOW='\033[1;33m'
28-
NC='\033[0m'
29-
27+
# Override lib's print functions with [HOOKS] prefix for this script's output.
3028
print_success() { echo -e "${GREEN}[HOOKS]${NC} $1"; }
3129
print_warning() { echo -e "${YELLOW}[HOOKS]${NC} $1"; }
3230
print_error() { echo -e "${RED}[HOOKS]${NC} $1" >&2; }

reporting/github-copilot-report/github-copilot-report.sh

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,13 @@ if [[ "$_today" -ge "20260601" && "$_today" -lt "20260901" ]]; then
7777
fi
7878

7979
plan_credits() {
80-
# If an explicit override is set, always use it
81-
if [[ -n "$CREDITS_PER_SEAT_OVERRIDE" ]]; then
82-
echo "$CREDITS_PER_SEAT_OVERRIDE"
83-
return
84-
fi
85-
if [[ "$IN_PROMO_PERIOD" == "true" ]]; then
86-
case "${1,,}" in
87-
business) echo "3000" ;;
88-
enterprise) echo "7000" ;;
89-
pro_plus|proplus) echo "7000" ;;
90-
free) echo "50" ;;
91-
pro) echo "3000" ;;
92-
*) echo "3000" ;;
93-
esac
94-
else
95-
case "${1,,}" in
96-
business) echo "1900" ;;
97-
enterprise) echo "3900" ;;
98-
pro_plus|proplus) echo "3900" ;;
99-
free) echo "50" ;;
100-
pro) echo "1900" ;;
101-
*) echo "1900" ;;
102-
esac
103-
fi
80+
[[ -n "$CREDITS_PER_SEAT_OVERRIDE" ]] && { echo "$CREDITS_PER_SEAT_OVERRIDE"; return; }
81+
local promo="$IN_PROMO_PERIOD"
82+
case "${1,,}" in
83+
enterprise|pro_plus|proplus) [[ "$promo" == "true" ]] && echo "7000" || echo "3900" ;;
84+
free) echo "50" ;;
85+
*) [[ "$promo" == "true" ]] && echo "3000" || echo "1900" ;;
86+
esac
10487
}
10588

10689
# ── Help text ─────────────────────────────────────────────────────────────────
@@ -206,22 +189,14 @@ else
206189
print_warning "az CLI is not logged in — department/division grouping will be skipped."
207190
print_warning "Run 'az login' to enable Entra ID enrichment, or pass --no-entra."
208191
fi
209-
210-
# ── GitHub API via curl with Copilot API version ──────────────────────────────
211-
# The Copilot usage-metrics endpoints require API version 2026-03-10.
212-
_copilot_api() {
213-
local url="$1"; shift
214-
gh_api "${url}" --api-version 2026-03-10 "$@"
215-
}
216-
217192
# fetch_usage_ndjson REPORT_PATH
218193
# Calls the new usage metrics API (which returns signed download_links to NDJSON
219194
# files rather than inline JSON), downloads each file, and emits one NDJSON
220195
# object per line on stdout. Exits cleanly with empty output if unavailable.
221196
fetch_usage_ndjson() {
222197
local path="$1"
223198
local resp links
224-
resp=$(_copilot_api "${path}" 2>/dev/null) || return 0
199+
resp=$(gh_api "${path}" --api-version 2026-03-10 2>/dev/null) || return 0
225200
[[ "${resp}" == "__404__" || "${resp}" == "__422__" ]] && return 0
226201
links=$(echo "$resp" | jq -r '.download_links[]? // empty' 2>/dev/null) || return 0
227202
[[ -z "$links" ]] && return 0
@@ -323,8 +298,7 @@ while IFS= read -r _login; do
323298
[[ -z "$_login" ]] && continue
324299
_login_i=$(( _login_i + 1 ))
325300
printf '\r [%d/%d] %s ' "$_login_i" "$_LOGIN_COUNT" "$_login" >&2
326-
_resp=$(_copilot_api \
327-
"/enterprises/${GITHUB_ENTERPRISE}/settings/billing/ai_credit/usage?user=${_login}&year=${_BILLING_YEAR}&month=${_BILLING_MONTH}") || _resp=""
301+
_resp=$(gh_api "/enterprises/${GITHUB_ENTERPRISE}/settings/billing/ai_credit/usage?user=${_login}&year=${_BILLING_YEAR}&month=${_BILLING_MONTH}" --api-version 2026-03-10) || _resp=""
328302
if [[ "${_resp}" == "__404__" || "${_resp}" == "__422__" ]]; then
329303
# No usage data this month — valid, treat as 0 credits.
330304
_credits="0"

0 commit comments

Comments
 (0)