Remind and Delete Stale Branches #4
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: Remind and Delete Stale Branches | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Runs daily | |
jobs: | |
remind_delete_stale_branches: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Identify Stale Branches | |
run: | | |
# Write your script here to identify stale branches | |
# For demonstration purposes, I'll provide a simple example using git commands | |
# Fetch all branches from the remote repository | |
git fetch --all | |
# Define the threshold for stale branches (for example, branches not updated in the last 1 day) | |
threshold_date=$(date -d "1 day ago" +"%Y-%m-%d") | |
# List all remote branches and iterate over each one | |
git branch -r --format="%(refname:short) %(authordate:short)" | while read branch_date branch_name; do | |
# Remove "origin/" prefix from the branch name | |
branch_name=${branch_name#"origin/"} | |
# Compare the branch's last commit date with the threshold date | |
if [[ "$branch_date" < "$threshold_date" ]]; then | |
# Check if a reminder count file exists for the branch | |
reminder_file=".github/reminders/$branch_name" | |
if [[ -f "$reminder_file" ]]; then | |
# Read the reminder count from the file | |
reminder_count=$(<"$reminder_file") | |
else | |
# Create a reminder count file for the branch | |
reminder_count=0 | |
fi | |
# Increment the reminder count | |
((reminder_count++)) | |
# Save the updated reminder count to the file | |
echo "$reminder_count" > "$reminder_file" | |
# If the reminder count exceeds 5, delete the stale branch | |
if ((reminder_count >= 5)); then | |
# Delete the stale branch | |
git push origin --delete "$branch_name" | |
echo "Deleted stale branch: $branch_name" | |
# Optionally, you can also remove the reminder count file after deletion | |
rm "$reminder_file" | |
else | |
# Send a reminder notification to the branch owner | |
# Replace the following line with your notification logic | |
echo "Reminder $reminder_count: Please delete stale branch '$branch_name'." | |
fi | |
fi | |
done |