-
Notifications
You must be signed in to change notification settings - Fork 1k
110 lines (89 loc) · 4.03 KB
/
find-dead-code.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Dead code detection
on:
pull_request:
types: [opened, labeled, unlabeled, synchronize]
paths:
- '**/*.swift'
jobs:
dead-code-check:
runs-on: macos-13
permissions:
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install Periphery
run: brew install peripheryapp/periphery/periphery
- name: Build project and run Periphery scan
id: periphery-scan
run: |
periphery scan --config .periphery.yml --clean-build 2>&1 | sed -E 's#.*/##; s/:[0-9]+:[0-9]+:/: /' | grep 'is unused' > periphery_report_feature_formatted_sorted.txt
ruby ci_scripts/dead_code/process_periphery_output.rb periphery_report_feature_formatted_sorted.txt unused_code_feature.json
- name: Copy .periphery.yml to temporary location
run: |
# Copy necessary files to /tmp/ before checking out master
cp .periphery.yml /tmp/
cp ci_scripts/dead_code/process_periphery_output.rb /tmp/
- name: Compare Periphery output with master baseline
run: |
git fetch origin master:master
git checkout master
cp /tmp/.periphery.yml .periphery.yml
mkdir -p ci_scripts/dead_code/
cp /tmp/process_periphery_output.rb ci_scripts/dead_code/
periphery scan --config .periphery.yml --clean-build 2>&1 | sed -E 's#.*/##; s/:[0-9]+:[0-9]+:/: /' | grep 'is unused' > periphery_report_master_formatted_sorted.txt
ruby ci_scripts/dead_code/process_periphery_output.rb periphery_report_master_formatted_sorted.txt unused_code_master.json
- name: Compare Unused Code JSON Files
id: compare-dead-code
run: |
# Compare the keys in the JSON files to find new dead code
ruby -r json -e '
master_file = "unused_code_master.json"
feature_file = "unused_code_feature.json"
output_file = "new_dead_code.json"
master_unused_code = JSON.parse(File.read(master_file))
feature_unused_code = JSON.parse(File.read(feature_file))
new_dead_code = feature_unused_code.reject { |k, _| master_unused_code.key?(k) }
if new_dead_code.size > 200
puts "More than 200 keys present, skipping. This usually happens if a build fails"
elsif new_dead_code.empty?
puts "No new dead code detected."
else
File.write(output_file, JSON.pretty_generate(new_dead_code) + "\n")
end
'
# Check if new_dead_code.json exists and is not empty
if [ -s new_dead_code.json ]; then
echo "New dead code detected."
echo "diff<<EOF" >> $GITHUB_ENV
cat new_dead_code.json >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
echo "No new dead code detected."
fi
- uses: peter-evans/find-comment@v3
id: find_comment
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: '🚨 New dead code detected'
- uses: peter-evans/create-or-update-comment@v3
id: create_update_comment
if: env.diff != ''
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
🚨 New dead code detected in this PR:
```diff
${{ env.diff }}
```
Please remove the dead code before merging.
If this is intentional, you can bypass this check by adding the label `skip dead code check` to this PR.
ℹ️ If this comment appears to be left in error, make sure your branch is up-to-date with `master`.
edit-mode: replace
comment-id: ${{ steps.find_comment.outputs.comment-id }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: Fail if not acknowledged
if: env.diff != '' && !contains(github.event.pull_request.labels.*.name, 'skip dead code check')
run: exit 1