-
Notifications
You must be signed in to change notification settings - Fork 1
93 lines (77 loc) · 3 KB
/
check-stale-branches.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
name: 👀 Collect Stale Environments
run-name: 👀 Collect Stale Environments 👾 ${{ github.actor }}
on:
push:
branches:
- feature/CB2-11814
# workflow_dispatch:
# inputs:
# dry-run:
# description: Don't perform action, just output information
# type: boolean
# default: false
permissions:
id-token: write
contents: write
jobs:
collect-stale-branches:
name: Collect Stale branches
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
runs-on: X64
steps:
- name: checkout
uses: actions/checkout@v4
- name: List all branches with pagination
id: branches
run: |
page=1
per_page=100
branches=()
while true; do
response=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/dvsa/cvs-svc-app-logs/branches?per_page=$per_page\&page=$page \
--jq '.[].name | select(test("^(main|master|develop|devops|HEAD)$|^(release/.*)$") | not)')
if [[ -z "$response" ]]; then
break
fi
branches+=($response)
((page++))
done
branches_json=$(printf '%s\n' "${branches[@]}" | jq -R . | jq -s .)
echo "$branches_json" > branches.json
echo "branches=$(echo $branches_json | jq -c)" >> $GITHUB_OUTPUT
- name: Upload branches JSON as artifact
uses: actions/upload-artifact@v3
with:
name: branches-json
path: branches.json
- name: Test Fetch Commit Dates for All Branches
id: stale_branches
run: |
active_cutoff=$(date -v -30d +%s)
for branch in $(jq -r '.[]' <<< '${{ steps.branches.outputs.branches }}'); do
commit_dates=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
/repos/dvsa/cvs-svc-app-logs/commits?sha="$branch" --jq '.[].commit.committer.date' 2>/dev/null)
# Get last commit for this branch
last_commit_date=$(gh api repos/$GITHUB_REPOSITORY/branches/$branch --jq '.commit.commit | .author.date')
# convert to epoch time
epoch=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$last_commit_date" +"%s")
# Check if the branch is stale
if [ "$epoch" -lt "$active_cutoff" ]; then
echo "$branch is active"
active_branches+=("$branch")
else
echo "$branch is stale"
fi
done
active_branches_json=$(printf '%s\n' "${active_branches[@]}" | jq -R . | jq -s .)
echo "$active_branches_json" > active_branches.json
echo "active_branches=$(echo $active_branches_json | jq -c)" >> $GITHUB_OUTPUT
- name: Upload branches JSON as artifact
uses: actions/upload-artifact@v3
with:
name: active_branches-json
path: active_branches.json