Skip to content

Commit d1f8fc0

Browse files
mvadariCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent f36132d commit d1f8fc0

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

.github/scripts/process-stale-discussions.sh

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,34 @@ set -e
1515
# - GH_TOKEN: GitHub token for API access
1616
# - DRY_RUN: Set to "true" to only print what would happen without making changes (optional)
1717

18+
# Validate required environment variables
19+
required_env_vars=(STALE_DAYS WARNING_DAYS WARNING_MESSAGE CLOSE_MESSAGE GITHUB_REPOSITORY_OWNER GITHUB_REPOSITORY_NAME GH_TOKEN)
20+
for var in "${required_env_vars[@]}"; do
21+
if [ -z "${!var}" ]; then
22+
echo "Error: required environment variable ${var} is not set or empty." >&2
23+
exit 1
24+
fi
25+
done
1826
# Calculate cutoff dates
1927
SECONDS_IN_DAY=86400
20-
STALE_CUTOFF=$(date -u -d "@$(($(date +%s) - STALE_DAYS * SECONDS_IN_DAY))" '+%Y-%m-%dT%H:%M:%SZ')
21-
CLOSE_CUTOFF=$(date -u -d "@$(($(date +%s) - (STALE_DAYS + WARNING_DAYS) * SECONDS_IN_DAY))" '+%Y-%m-%dT%H:%M:%SZ')
2228

29+
# Use epoch seconds and support both GNU date (-d) and BSD/macOS date (-r)
30+
NOW_EPOCH=$(date -u +%s)
31+
STALE_CUTOFF_EPOCH=$((NOW_EPOCH - STALE_DAYS * SECONDS_IN_DAY))
32+
CLOSE_CUTOFF_EPOCH=$((NOW_EPOCH - (STALE_DAYS + WARNING_DAYS) * SECONDS_IN_DAY))
33+
34+
if date -u -d "@0" '+%Y-%m-%dT%H:%M:%SZ' >/dev/null 2>&1; then
35+
# GNU date
36+
STALE_CUTOFF=$(date -u -d "@$STALE_CUTOFF_EPOCH" '+%Y-%m-%dT%H:%M:%SZ')
37+
CLOSE_CUTOFF=$(date -u -d "@$CLOSE_CUTOFF_EPOCH" '+%Y-%m-%dT%H:%M:%SZ')
38+
elif date -u -r 0 '+%Y-%m-%dT%H:%M:%SZ' >/dev/null 2>&1; then
39+
# BSD/macOS date
40+
STALE_CUTOFF=$(date -u -r "$STALE_CUTOFF_EPOCH" '+%Y-%m-%dT%H:%M:%SZ')
41+
CLOSE_CUTOFF=$(date -u -r "$CLOSE_CUTOFF_EPOCH" '+%Y-%m-%dT%H:%M:%SZ')
42+
else
43+
echo "Error: unsupported 'date' implementation; cannot compute cutoff dates." >&2
44+
exit 1
45+
fi
2346
echo "Stale cutoff (for warnings): $STALE_CUTOFF"
2447
echo "Close cutoff (for closing): $CLOSE_CUTOFF"
2548

@@ -51,7 +74,7 @@ gh api graphql -f query='
5174
updatedAt
5275
closed
5376
locked
54-
comments(last: 10) {
77+
comments(last: 100) {
5578
nodes {
5679
body
5780
createdAt
@@ -68,12 +91,12 @@ gh api graphql -f query='
6891

6992
# Process discussions to close
7093
# A discussion should be closed if:
71-
# 1. It has a warning comment containing "will be closed in 30 days"
94+
# 1. It has a warning comment containing the configured WARNING_MESSAGE
7295
# 2. That warning comment is older than WARNING_DAYS
7396
# 3. The discussion hasn't been updated since the warning (or updates are also old)
7497
echo ""
7598
echo "=== Discussions to close - warned ${WARNING_DAYS}+ days ago with no activity ==="
76-
cat discussions.json | jq -r --arg warningCutoff "$CLOSE_CUTOFF" '.data.repository.discussions.nodes[] | select(.closed == false) | . as $discussion | ((.comments.nodes // []) | map(select(.body | contains("will be closed in 30 days"))) | last) as $warningComment | select($warningComment != null) | select($warningComment.createdAt < $warningCutoff) | select($discussion.updatedAt <= $warningComment.createdAt or $discussion.updatedAt < $warningCutoff) | @json' | while IFS= read -r discussion; do
99+
cat discussions.json | jq -r --arg warningCutoff "$CLOSE_CUTOFF" --arg warningMessage "$WARNING_MESSAGE" '.data.repository.discussions.nodes[] | select(.closed == false) | . as $discussion | ((.comments.nodes // []) | map(select(.body | contains($warningMessage))) | last) as $warningComment | select($warningComment != null) | select($warningComment.createdAt < $warningCutoff) | select($discussion.updatedAt <= $warningComment.createdAt or $discussion.updatedAt < $warningCutoff) | @json' | while IFS= read -r discussion; do
77100
if [ -n "$discussion" ]; then
78101
DISCUSSION_ID=$(echo "$discussion" | jq -r '.id')
79102
DISCUSSION_NUMBER=$(echo "$discussion" | jq -r '.number')

0 commit comments

Comments
 (0)