11name : " Manage Stale Issues, PRs & Unmerged Branches"
2-
32on :
43 schedule :
54 - cron : ' 30 1 * * *' # Runs daily at 1:30 AM UTC
65 workflow_dispatch : # Allows manual triggering
7-
86permissions :
97 contents : write
108 issues : write
119 pull-requests : write
12-
1310jobs :
1411 stale :
1512 runs-on : ubuntu-latest
@@ -23,51 +20,60 @@ jobs:
2320 days-before-close : 30
2421 exempt-issue-labels : " keep"
2522 exempt-pr-labels : " keep"
26-
2723 cleanup-branches :
2824 runs-on : ubuntu-latest
2925 steps :
3026 - name : Checkout Repository
3127 uses : actions/checkout@v4
3228 with :
3329 fetch-depth : 0 # Fetch full history for accurate branch checks
34-
3530 - name : Fetch All Branches
3631 run : git fetch --all --prune
37-
38- - name : Create CSV File
39- run : echo "Branch,Last Commit Date,Merged Into,Status" > stale-branches-report.csv
40-
41- - name : Identify & List Stale Merged Branches with No Activity in Last 3 Months
32+ - name : List Merged Branches With No Activity in Last 3 Months
4233 run : |
43- echo "Checking merged branches with no activity in the last 3 months..."
44- for branch_info in $(git for-each-ref --format '%(refname:short) %(committerdate:unix)' refs/remotes/origin | awk -v date=$(date -d '3 months ago' +%s) '$2 < date {print $1","$2}'); do
45- branch=$(echo $branch_info | cut -d',' -f1)
34+
35+ echo "Branch Name,Last Commit Date,Committed In Branch,Action" > merged_branches_report.csv
36+
37+ for branch in $(git for-each-ref --format '%(refname:short) %(committerdate:unix)' refs/remotes/origin | awk -v date=$(date -d '3 months ago' +%s) '$2 < date {print $1}'); do
38+ if [[ "$branch" != "origin/main" && "$branch" != "origin/dev" ]]; then
39+ branch_name=${branch#origin/}
40+ # Ensure the branch exists locally before getting last commit date
41+ git fetch origin "$branch_name" || echo "Could not fetch branch: $branch_name"
42+ last_commit_date=$(git log -1 --format=%ci "origin/$branch_name" || echo "Unknown")
43+ committed_in_branch=$(git branch -r --contains "origin/$branch_name" | tr -d ' ' | paste -sd "," -)
44+ echo "$branch_name,$last_commit_date,$committed_in_branch,Delete" >> merged_branches_report.csv
45+ fi
46+ done
47+ - name : List PR Approved and Merged Branches Older Than 30 Days
48+ run : |
49+
50+ for branch in $(gh api repos/${{ github.repository }}/pulls --jq '.[] | select(.merged_at != null and (.base.ref == "main" or .base.ref == "dev")) | select(.merged_at | fromdateiso8601 < (now - 2592000)) | .head.ref'); do
51+ # Ensure the branch exists locally before getting last commit date
52+ git fetch origin "$branch" || echo "Could not fetch branch: $branch"
4653 last_commit_date=$(git log -1 --format=%ci origin/$branch || echo "Unknown")
47- merged_into=$(git branch -r --contains $branch | grep -E 'origin/main|origin/dev' | awk -F'/' '{print $2}' | tr '\n' ',' | sed 's/,$//')
48- merged_into=${merged_into:-"Unknown"}
49-
50- echo "$branch,$last_commit_date,$merged_into,Marked for Deletion" >> stale-branches-report.csv
51-
54+ committed_in_branch=$(git branch -r --contains "origin/$branch" | tr -d ' ' | paste -sd "," -)
55+ echo "$branch,$last_commit_date,$committed_in_branch,Delete" >> merged_branches_report.csv
5256 done
53-
54- - name : Identify & List PR Approved & Merged Branches (Older Than 30 Days)
57+ env :
58+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
59+ - name : List Open PR Branches With No Activity in Last 3 Months
5560 run : |
56- echo "Checking PR branches that were approved and merged more than 30 days ago..."
57- for branch_info in $(gh api repos/${{ github.repository }}/pulls --jq '.[] | select(.merged_at != null and (.base.ref == "main" or .base.ref == "dev")) | select(.merged_at | fromdateiso8601 < (now - 2592000)) | [.head.ref, .merged_at, .base.ref] | @csv'); do
58- branch=$(echo $branch_info | awk -F, '{print $1}' | tr -d '"')
61+
62+ for branch in $(gh api repos/${{ github.repository }}/pulls --state open --jq '.[] | select(.base.ref == "main" or .base.ref == "dev") | .head.ref'); do
63+ # Ensure the branch exists locally before getting last commit date
64+ git fetch origin "$branch" || echo "Could not fetch branch: $branch"
5965 last_commit_date=$(git log -1 --format=%ci origin/$branch || echo "Unknown")
60- merged_into=$(echo $branch_info | awk -F, '{print $3}' | tr -d '"')
61-
62- echo "$branch,$last_commit_date,$merged_into,Marked for Deletion" >> stale-branches-report.csv
63-
66+ if [[ $(date -d "$last_commit_date" +%s) -lt $(date -d '3 months ago' +%s) ]]; then
67+ # If no commit in the last 3 months, mark for deletion
68+ committed_in_branch=$(git branch -r --contains "origin/$branch" | tr -d ' ' | paste -sd "," -)
69+ echo "$branch,$last_commit_date,$committed_in_branch,Delete" >> merged_branches_report.csv
70+ fi
6471 done
6572 env :
6673 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
67-
68- - name : Upload Stale Branch Report
74+ - name : Upload CSV Report of Inactive Branches
6975 uses : actions/upload-artifact@v4
7076 with :
71- name : stale -branches-report
72- path : stale -branches-report.csv
73- retention-days : 30
77+ name : merged -branches-report
78+ path : merged -branches-report.csv
79+ retention-days : 30
0 commit comments