chore: fixed merge vs rebase #3
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: cleanup-merged-branches | ||
on: | ||
schedule: | ||
# Every week on Monday morning at 00:00 | ||
- cron: '0 0 * * 1' | ||
workflow_dispatch: | ||
inputs: | ||
dryRun: | ||
description: "Dry Run" | ||
required: false | ||
default: true | ||
type: choice | ||
options: | ||
- false | ||
- true | ||
jobs: | ||
cleanup: | ||
runs-on: ubuntu-latest | ||
env: | ||
DRY_RUN: ${{ inputs.dryRun }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
# With fetch-depth 0 the checkout action will also fetch all tags and branches. | ||
fetch-depth: 0 | ||
token: ${{ secrets.GH_PAT }} | ||
- name: Configure git name/email | ||
run: | | ||
git config user.name "IBM/Instana/Team Node.js" | ||
git config user.email [email protected] | ||
- name: Fetch all branches | ||
run: git fetch --prune --all | ||
- name: Delete old merged branches | ||
run: | | ||
threshold_date=$(date +%s -d '60 days ago') | ||
# NOTE: "--merged origin/main" ensures that we only filter branches | ||
# that have been merged into main. | ||
git branch -r --merged origin/main | grep -E 'origin/(chore-|docs-|fix-|feat-|test-|refactor-)' | while read -r branch; do | ||
branch_name=$(echo $branch | sed 's|origin/||') | ||
# NOTE: We figure out the last commit from the branch in main | ||
last_commit_in_main=$(git log origin/main --oneline --grep="$branch_name" -1 --format=%H) | ||
# NOTE: If no commit from the branch is found in main, skip it. | ||
if [ -z "$last_commit_in_main" ]; then | ||
echo "Branch $branch_name has no commits in main. Skipping." | ||
continue | ||
fi | ||
# NOTE: We get the date of the last commit from the branch in main. | ||
merge_commit_date=$(git log -1 --format=%ct $last_commit_in_main) | ||
# NOTE: Ensure no new commits are on this branch | ||
last_commit_date=$(git log -1 --format=%ct origin/$branch_name) | ||
if [[ $merge_commit_date -lt $threshold_date && $last_commit_date -le $merge_commit_date ]]; then | ||
echo "Deleting branch: $branch_name" | ||
if [[ $DRY_RUN = true ]]; then | ||
echo "Dry run: git push origin --delete $branch_name" | ||
else | ||
git push origin --delete $branch_name | ||
fi | ||
else | ||
echo "Skipping branch: $branch_name (either not 60 days old or has new commits)." | ||
fi | ||
done | ||