FIX: echo "$ISSUES" | while ... passes a single empty line to the loop
All checks were successful
Close Stale Issues / stale (push) Successful in 3s
All checks were successful
Close Stale Issues / stale (push) Successful in 3s
This commit is contained in:
@@ -20,45 +20,38 @@ jobs:
|
||||
THIRTY_DAYS_AGO_SEC=$(date -d '30 days ago' '+%s')
|
||||
|
||||
# Get all open issues sorted by oldest update first
|
||||
# REMOVED 'since' because it filters for NEWER issues
|
||||
ISSUES=$(curl -s -H "Authorization: token $API_TOKEN" "$API_BASE_URL/repos/$REPO/issues?state=open&sort=updated&order=asc" | jq -c '.[]')
|
||||
|
||||
# Check if we have issues to process
|
||||
# Safety Check 1: Exit if no issues found
|
||||
if [ -z "$ISSUES" ]; then
|
||||
echo "No open issues found."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "$ISSUES" | while IFS= read -r issue; do
|
||||
# Prevent processing empty lines
|
||||
# Safety Check 2: Skip empty lines
|
||||
if [ -z "$issue" ]; then continue; fi
|
||||
|
||||
ISSUE_NUMBER=$(echo "$issue" | jq -r '.number')
|
||||
UPDATED_AT=$(echo "$issue" | jq -r '.updated_at')
|
||||
|
||||
# Convert issue update time to seconds
|
||||
UPDATED_AT_SEC=$(date -d "$UPDATED_AT" '+%s')
|
||||
|
||||
# LOGIC CHECK: If the issue is newer than 30 days, stop (since list is sorted asc)
|
||||
# Stop processing if we reach issues newer than 30 days
|
||||
if [ "$UPDATED_AT_SEC" -gt "$THIRTY_DAYS_AGO_SEC" ]; then
|
||||
# Found an issue updated recently, so we are done with the old ones
|
||||
continue
|
||||
fi
|
||||
|
||||
HAS_STALE_LABEL=$(echo "$issue" | jq -r '.labels[] | select(.name == "status: stale") | .name')
|
||||
|
||||
# Skip if already has stale label
|
||||
if [ -n "$HAS_STALE_LABEL" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "Marking issue #$ISSUE_NUMBER as stale (last updated: $UPDATED_AT)"
|
||||
|
||||
# Add stale label
|
||||
LABELS='{"labels": ["status: stale"]}'
|
||||
curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$LABELS" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/labels"
|
||||
|
||||
# Add stale comment
|
||||
STALE_MSG="This issue has been automatically marked as stale because it has not had recent activity.\n\nIt will be closed in **14 days** if no further activity occurs.\n\nIf this issue is still relevant, please:\n- Add a comment with updates\n- Remove the \`status: stale\` label\n\nThank you for your contributions! 🙏"
|
||||
COMMENT_JSON=$(jq -n --arg body "$STALE_MSG" '{"body": $body}')
|
||||
curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$COMMENT_JSON" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/comments"
|
||||
@@ -66,25 +59,30 @@ jobs:
|
||||
|
||||
- name: Close stale issues
|
||||
run: |
|
||||
# Issues marked stale for 14+ days
|
||||
FOURTEEN_DAYS_AGO=$(date -u -d '14 days ago' '+%Y-%m-%dT%H:%M:%SZ')
|
||||
|
||||
# Get stale issues
|
||||
ISSUES=$(curl -s -H "Authorization: token $API_TOKEN" "$API_BASE_URL/repos/$REPO/issues?state=open&labels=status:stale" | jq -c '.[]')
|
||||
|
||||
# FIX: Check if list is empty
|
||||
if [ -z "$ISSUES" ]; then
|
||||
echo "No stale issues found to close."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "$ISSUES" | while IFS= read -r issue; do
|
||||
# FIX: Check for empty line
|
||||
if [ -z "$issue" ]; then continue; fi
|
||||
|
||||
ISSUE_NUMBER=$(echo "$issue" | jq -r '.number')
|
||||
UPDATED_AT=$(echo "$issue" | jq -r '.updated_at')
|
||||
|
||||
# Check if updated_at is older than 14 days
|
||||
if [[ "$UPDATED_AT" < "$FOURTEEN_DAYS_AGO" ]]; then
|
||||
echo "Closing stale issue #$ISSUE_NUMBER (last updated: $UPDATED_AT)"
|
||||
|
||||
# Close the issue
|
||||
CLOSE_JSON='{"state": "closed"}'
|
||||
curl -s -f -X PATCH -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$CLOSE_JSON" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER"
|
||||
|
||||
# Add closing comment
|
||||
CLOSE_MSG="This issue was automatically closed due to inactivity.\n\nIf you believe this was closed in error, please:\n1. Reopen the issue\n2. Provide updated information\n3. Remove the \`status: stale\` label\n\nThank you! 🙏"
|
||||
COMMENT_JSON=$(jq -n --arg body "$CLOSE_MSG" '{"body": $body}')
|
||||
curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$COMMENT_JSON" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/comments"
|
||||
@@ -93,21 +91,29 @@ jobs:
|
||||
|
||||
- name: Remove stale label on activity
|
||||
run: |
|
||||
# Get stale issues that were recently updated
|
||||
ISSUES=$(curl -s -H "Authorization: token $API_TOKEN" "$API_BASE_URL/repos/$REPO/issues?state=open&labels=status:stale" | jq -c '.[]')
|
||||
|
||||
# FIX: Check if list is empty
|
||||
if [ -z "$ISSUES" ]; then
|
||||
echo "No stale issues found to check for activity."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "$ISSUES" | while IFS= read -r issue; do
|
||||
# FIX: Check for empty line
|
||||
if [ -z "$issue" ]; then continue; fi
|
||||
|
||||
ISSUE_NUMBER=$(echo "$issue" | jq -r '.number')
|
||||
|
||||
# Get comments
|
||||
COMMENTS=$(curl -s -H "Authorization: token $API_TOKEN" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/comments")
|
||||
LATEST_COMMENT_AUTHOR=$(echo "$COMMENTS" | jq -r '.[-1].user.login')
|
||||
LATEST_COMMENT_AUTHOR=$(echo "$COMMENTS" | jq -r '.[-1].user.login // empty')
|
||||
|
||||
# Skip if no comments exist or fetch failed
|
||||
if [ -z "$LATEST_COMMENT_AUTHOR" ]; then continue; fi
|
||||
|
||||
# If latest comment is not from bot, remove stale label
|
||||
if [ "$LATEST_COMMENT_AUTHOR" != "EC-bot" ] && [ "$LATEST_COMMENT_AUTHOR" != "gitea-actions" ]; then
|
||||
echo "Removing stale label from issue #$ISSUE_NUMBER (recent activity detected)"
|
||||
|
||||
# Get label ID
|
||||
LABELS=$(curl -s -H "Authorization: token $API_TOKEN" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/labels")
|
||||
STALE_LABEL_ID=$(echo "$LABELS" | jq -r '.[] | select(.name == "status: stale") | .id')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user