-
Notifications
You must be signed in to change notification settings - Fork 0
73 lines (65 loc) · 2.23 KB
/
cleanup_branches_and_issues.yml
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
name: Cleanup branches and issues
on:
schedule:
- cron: '0 0 * * *' # Run this workflow every day at midnight
workflow_dispatch:
env:
DAYS: 7
jobs:
deleteOldBranches:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install jq
run: sudo apt-get install -y jq
- name: Fetch all branches
run: git fetch --all
- name: Delete old branches
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PATTERN="issue-*"
NOW=$(date +%s)
DAYS_MAX=${DAYS}
MAX_AGE=$((DAYS_MAX * 24 * 60 * 60 * 1000))
# Get all branches matching the pattern
BRANCHES=$(git branch -r | grep "$PATTERN" | sed 's/origin\///')
for BRANCH in $BRANCHES; do
# Get the last commit date of the branch
LAST_COMMIT_DATE=$(git log -1 --format=%ct origin/$BRANCH)
AGE=$((NOW - LAST_COMMIT_DATE))
if [ $AGE -gt $MAX_AGE ]; then
echo "Deleting branch $BRANCH"
# Delete the branch
curl -X DELETE -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/git/refs/heads/$BRANCH"
fi
done
closeOldIssues:
runs-on: ubuntu-latest
steps:
- name: Close old data request issues
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const max_age = process.env.DAYS * 24 * 60 * 60 * 1000;
const now = new Date();
const issues = await github.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
labels: 'data request',
state: 'open'
});
for (const issue of issues.data) {
const createdAt = new Date(issue.created_at);
if (now - createdAt > max_age) {
await github.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'closed'
});
}
}