Close Issues with Status Stale Label #434
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 Issues with Status Stale Label | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Run every day at midnight | |
workflow_dispatch: # Allows manual triggering | |
jobs: | |
close-issues: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Install Node.js | |
uses: actions/setup-node@v2 | |
- name: Install jq | |
run: sudo apt-get update && sudo apt-get install -y jq | |
- name: Close Issues | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
LABEL_NAME: 'Status%3A%20Stale' | |
CUSTOM_MESSAGE: 'Apologies that we were unable to address this issue in a timely manner! \n \n Given that we have millions of free users this has been a challenge, and we are working to improve over time. As part of an effort to focus on driving the most recent issues to resolution, this issue is being closed. \n \n If this issue is still meaningful, please comment with an update and re-open the issue for troubleshooting! Thanks for your continued loyalty to the StackBlitz platform over the years!' | |
run: | | |
set -e # Stop the script if any command fails | |
set -x # Echo each command before executing it | |
page=1 | |
while : ; do | |
echo "Fetching page $page of issues..." | |
response=$(curl -sS -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues?state=open&labels=$LABEL_NAME&per_page=100&page=$page") | |
echo $response > issues.json | |
if [ $(jq length issues.json) -eq 0 ]; then | |
echo "No more issues found." | |
break | |
fi | |
echo "Commenting and closing issues..." | |
jq -c '.[] | .number' issues.json | while read -r issue; do | |
# Comment on the issue | |
COMMENT_RESPONSE=$(curl -sS -w "%{http_code}" -X POST -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$issue/comments" \ | |
-d "{\"body\": \"$CUSTOM_MESSAGE\"}") | |
HTTP_STATUS=$(echo $COMMENT_RESPONSE | rev | cut -c 1-3 | rev) | |
RESPONSE_BODY=$(echo $COMMENT_RESPONSE | rev | cut -c 4- | rev) | |
if [ "$HTTP_STATUS" -ge 200 ] && [ "$HTTP_STATUS" -lt 300 ]; then | |
echo "Successfully commented on issue $issue." | |
else | |
echo "Failed to comment on issue $issue. HTTP Status: $HTTP_STATUS, Response: $RESPONSE_BODY" | |
exit 1 | |
fi | |
# Close the issue | |
RESPONSE=$(curl -sS -w "%{http_code}" -X PATCH -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$issue" \ | |
-d "{\"state\": \"closed\"}") | |
HTTP_STATUS=$(echo $RESPONSE | rev | cut -c 1-3 | rev) | |
RESPONSE_BODY=$(echo $RESPONSE | rev | cut -c 4- | rev) | |
if [ "$HTTP_STATUS" -ge 200 ] && [ "$HTTP_STATUS" -lt 300 ]; then | |
echo "Successfully closed issue $issue." | |
else | |
echo "Failed to close issue $issue. HTTP Status: $HTTP_STATUS, Response: $RESPONSE_BODY" | |
exit 1 | |
fi | |
done | |
((page++)) | |
done |