Skip to content

Commit 7d2ed18

Browse files
locus313Copilot
andauthored
refactor: extract is_bsd_date helper to lib/github-common.sh (#54)
Deduplicates the BSD-vs-GNU date detection probe that was repeated across github-archive-old-repos.sh (2 call sites) and github-copilot-budget-advisor.sh (1 call site) into a single shared helper, and documents it in the shared library docs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c4eddb4 commit 7d2ed18

7 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Provides reusable helpers sourced by scripts:
3232
| `configure_gh_auth [scope_hint]` | Bridge GITHUB_TOKEN→GH_TOKEN or verify gh auth session |
3333
| `validate_github_token [bearer]` | Verify GITHUB_TOKEN via /user endpoint |
3434
| `validate_slug <value> <label>` | Reject values with non-alphanumeric/hyphen/underscore chars |
35+
| `is_bsd_date` | Returns 0 if `date` is BSD-style (macOS), 1 for GNU (Linux); branch between `date -v` and `date -d` syntax |
3536
| `gh_api <path> [--api-version V] [curl args...]` | Bearer-auth REST helper with 5-retry rate-limit handling; optional `--api-version` overrides the default `2022-11-28` header; returns literal `__404__` or `__422__` for those HTTP statuses — callers must check for these sentinels |
3637
| `gh_api_paginate <path> [filter] [version]` | Paginated REST helper, follows Link headers, streams items; returns silently with empty output on 404/422 |
3738
| `get_enterprise_orgs` | Three-tier enterprise org resolver (REST → GraphQL → /user/orgs) |

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bats tests/test_common.bats
9393

9494
| File | What it covers |
9595
|------|----------------|
96-
| `tests/test_common.bats` | `lib/github-common.sh` — pure-logic functions (`validate_slug`, `require_env_var`, `require_command`, `err`, `configure_gh_auth`, `validate_token`, `get_repo_page_count`) and API helpers (`gh_api` sentinels, `gh_api_paginate`) |
96+
| `tests/test_common.bats` | `lib/github-common.sh` — pure-logic functions (`validate_slug`, `require_env_var`, `require_command`, `err`, `configure_gh_auth`, `validate_token`, `get_repo_page_count`, `is_bsd_date`) and API helpers (`gh_api` sentinels, `gh_api_paginate`) |
9797
| `tests/test_script_validation.bats` | Every script — missing required env vars exit 1, invalid CLI args exit 1, `--help` exits 0, script-specific enum/allowlist validation |
9898
| `tests/mock_curl.sh` | Universal drop-in curl mock (used by both test files); response data via env vars `MOCK_CURL_CODE`, `MOCK_CURL_BODY`, `MOCK_CURL_LINK` |
9999

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ All scripts can leverage a shared utility library for common operations like val
937937
- `validate_token <VAR_NAME> [bearer]` — Validate GitHub token by calling `/user` endpoint
938938
- `validate_github_token [bearer]` — Convenience wrapper for `GITHUB_TOKEN` validation
939939
- `validate_slug <value> <label>` — Exit if value contains characters other than alphanumeric, hyphen, or underscore
940+
- `is_bsd_date` — Return 0 if the system `date` is BSD-style (macOS), 1 for GNU (Linux); use to branch between `date -v` and `date -d` syntax
940941

941942
**Auth helpers:**
942943
- `configure_gh_auth [scope_hint]` — Bridge `GITHUB_TOKEN→GH_TOKEN` for scripts that use the `gh` CLI, or verify an active `gh` auth session if no token is set

‎lib/github-common.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# validate_github_token [bearer] — verify GITHUB_TOKEN via /user endpoint
1919
# validate_token <VAR_NAME> — verify a secondary token variable
2020
# validate_slug <value> [label] — exit if value contains unsafe chars
21+
# is_bsd_date — 0 if `date` is BSD-style (macOS), 1 for GNU (Linux)
2122
# gh_api <path|url> [--api-version V] [curl args…] — Bearer-auth REST helper with retry;
2223
# returns "__404__"/"__422__" (exit 0) for those codes
2324
# gh_api_paginate <path> [filter] [version] — paginated REST, follows Link headers;
@@ -153,6 +154,15 @@ get_repo_page_count() {
153154
echo "${pages:-1}"
154155
}
155156

157+
###
158+
## is_bsd_date
159+
## Returns 0 if the system `date` is BSD-style (macOS), 1 for GNU (Linux).
160+
## Use to branch between `date -v` and `date -d` syntax.
161+
###
162+
is_bsd_date() {
163+
date -v-1d > /dev/null 2>&1
164+
}
165+
156166
###
157167
## validate_slug <value> <label>
158168
## Exits with status 1 if the value contains characters other than

‎org-admin/github-archive-old-repos/github-archive-old-repos.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ trap cleanup EXIT
5454
###
5555
calculate_cutoff_date() {
5656
# Using 'date' compatible with both GNU (Linux) and BSD (macOS)
57-
if date -v-1d > /dev/null 2>&1; then
57+
if is_bsd_date; then
5858
# BSD date (macOS)
5959
CUTOFF_DATE=$(date -u -v-${YEARS_THRESHOLD}y +"%Y-%m-%dT%H:%M:%SZ")
6060
else
@@ -98,7 +98,7 @@ fetch_old_repos() {
9898

9999
if [[ "$REPO_UPDATEDAT" < "$cutoff_date" ]]; then
100100
local UPDATED_EPOCH CURRENT_EPOCH DAYS_SINCE
101-
if date -v-1d > /dev/null 2>&1; then
101+
if is_bsd_date; then
102102
UPDATED_EPOCH=$(date -jf "%Y-%m-%dT%H:%M:%SZ" "$REPO_UPDATEDAT" +%s 2>/dev/null || echo "0")
103103
else
104104
UPDATED_EPOCH=$(date -d "$REPO_UPDATEDAT" +%s 2>/dev/null || echo "0")

‎reporting/github-copilot-budget-advisor/github-copilot-budget-advisor.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ _CURR_YEAR=$(date +%Y)
124124
_CURR_MONTH=$(( 10#$(date +%m) )) # strip leading zero for arithmetic
125125
_CURR_DAY=$(( 10#$(date +%d) ))
126126

127-
if date -v-1m +%Y >/dev/null 2>&1; then
127+
if is_bsd_date; then
128128
# BSD date (macOS)
129129
_PREV_YEAR=$(date -v-1m +%Y)
130130
_PREV_MONTH=$(( 10#$(date -v-1m +%m) ))

‎tests/test_common.bats‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ _mock_curl() {
9191
[ "$status" -eq 1 ]
9292
}
9393

94+
# ─── is_bsd_date ──────────────────────────────────────────────────────────────
95+
96+
@test "is_bsd_date: matches whether 'date -v' is supported on this system" {
97+
run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; is_bsd_date"
98+
if date -v-1d > /dev/null 2>&1; then
99+
[ "$status" -eq 0 ]
100+
else
101+
[ "$status" -eq 1 ]
102+
fi
103+
}
104+
94105
# ─── require_env_var ──────────────────────────────────────────────────────────
95106

96107
@test "require_env_var: exits 1 when variable is unset" {

0 commit comments

Comments
 (0)