Close Old Issues #192
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Close Old Issues | |
on: | |
schedule: | |
- cron: "0 0 * * *" # Runs daily at midnight | |
jobs: | |
close-issues: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Close Old Issues | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
# Fetch open issues | |
open_issues=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
"https://api.github.com/repos/${{ github.repository }}/issues?state=open&per_page=100" \ | |
| jq -r '.[] | select(.pull_request == null) | .number') | |
for issue in $open_issues; do | |
# Get the last updated timestamp of the issue | |
last_updated=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/$issue" \ | |
| jq -r '.updated_at') | |
# Calculate days since the last update | |
days_since_update=$(( ( $(date +%s) - $(date -d "$last_updated" +%s) ) / 86400 )) | |
if [ $days_since_update -gt 5 ]; then | |
# Close the issue | |
curl -s -X PATCH -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-d '{"state":"closed"}' \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/$issue" | |
# Add a comment explaining why the issue was closed | |
curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-d '{"body":"This issue has been automatically closed because it has been inactive for more than 5 days. If you believe this is still relevant, feel free to reopen it or create a new one. Thank you!"}' \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/$issue/comments" | |
fi | |
done |