-
Notifications
You must be signed in to change notification settings - Fork 50
88 lines (73 loc) · 2.66 KB
/
clear-old-test-rules.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: Remove Stale from test-rules Branch
# This exists because the 'Remove from test-rules Branch' workflow won't work on PRs opened from a fork because it
# won't have permissions. This will just retroactively clean anything by running every few minutes and removing anything
# that doesn't reference an open PR.
on:
schedule:
- cron: "*/10 * * * *"
workflow_dispatch: {}
jobs:
remove-stale:
runs-on: ubuntu-20.04
permissions:
contents: write
pull-requests: read
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: "test-rules"
path: destination
- name: Get Open PRs
id: open_prs
uses: actions/github-script@v4
with:
script: |
github.paginate(
github.pulls.list,
{
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
},
(response) => response.data.map((pr) => pr.number)
).then((openPRs) => {
console.log(`::set-output name=open_prs::${openPRs.join(',')}`);
});
- name: Delete stale test files
env:
OPEN_PRS: ${{ steps.open_prs.outputs.open_prs }}
run: |
if [[ "${{ github.repository }}" != "sublime-security/sublime-rules" ]]; then
echo "This is a forked repository. Skipping the job."
exit 0
fi
echo "Open PRs: [$OPEN_PRS]"
echo "Scheduled cleanup" > message.txt
echo "" >> message.txt
cd destination
files=$(ls **/*.yml) || true
for file in $files; do
file_pr_num=$(yq '.testing_pr' $file)
in_open_pr=false
IFS=',' read -ra PR_ARRAY <<< "$OPEN_PRS"
for pr_num in "${PR_ARRAY[@]}"; do
if [[ "$pr_num" = "$file_pr_num" ]]; then
in_open_pr=true
fi
done
echo "$file is in open PR: $in_open_pr. File PR num: $file_pr_num"
if [[ "$in_open_pr" = "false" ]]; then
rm $file
echo "Removed $file_pr_num" >> ../message.txt
fi
done
if [[ -z $(git status --porcelain) ]]; then
echo "Nothing to do"
exit 0
fi
git add -A
git config --global user.name 'Sublime Rule Testing Bot'
git config --global user.email '[email protected]'
git commit --allow-empty -F ../message.txt
git push origin test-rules