-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
175 lines (158 loc) · 6.54 KB
/
action.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: 'gocyclo Analysis Reports'
description: 'Analyze Go files with gocyclo and report cyclomatic complexity.'
author: 'snkrheadz <[email protected]>'
branding:
icon: check
color: green
inputs:
ignore_pattern:
description: 'Pattern to ignore files in gocyclo analysis'
required: false
default: ''
over:
description: 'Show functions with complexity > N only and return exit code 1 if the set is non-empty'
required: false
default: ''
top:
description: 'Show the top N most complex functions only'
required: false
default: ''
runs:
using: 'composite'
steps:
- name: Checkout base and head
uses: actions/[email protected]
with:
fetch-depth: 0
- name: Set up Go
uses: actions/[email protected]
with:
go-version-file: ./go.mod
- name: Get diff of Go files
id: changes
env:
BASE_REF: ${{ github.event.pull_request.base.ref }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
shell: bash
run: |
git fetch origin "$BASE_REF" "$HEAD_REF" --depth=1
git diff --name-only FETCH_HEAD.."$GITHUB_SHA" -- '*.go' > changed_files.txt
cat changed_files.txt
- name: Install gocyclo
shell: bash
run: go install github.com/fzipp/gocyclo/cmd/[email protected]
- name: Run gocyclo and analyze results
id: gocyclo
shell: bash
run: |
set -x
set +e
# Retrieve the option specified by the user from environment variables
IGNORE_PATTERN="${{ inputs.ignore_pattern }}"
OVER="${{ inputs.over }}"
TOP="${{ inputs.top }}"
# Build the gocyclo command
GOCYCLO_OPTS=""
if [ -n "$IGNORE_PATTERN" ]; then
GOCYCLO_OPTS="$GOCYCLO_OPTS -ignore $IGNORE_PATTERN"
fi
if [ -n "$OVER" ]; then
GOCYCLO_OPTS="$GOCYCLO_OPTS -over $OVER"
fi
if [ -n "$TOP" ]; then
GOCYCLO_OPTS="$GOCYCLO_OPTS -top $TOP"
fi
GOCYCLO_CMD="gocyclo ${GOCYCLO_OPTS}"
# Prepare the output file
touch raw_gocyclo_output.txt error_gocyclo_output.txt
# Analyze each changed file
while IFS= read -r file; do
if [[ -f "$file" ]]; then
echo "Analyzing file: \"$file\""
gocyclo_output=$($GOCYCLO_CMD "$file" 2>&1)
status=$?
if [ $status -eq 0 ] || { [ -n "$OVER" ] && [ $status -eq 1 ]; }; then
echo "$gocyclo_output" >> raw_gocyclo_output.txt
status=0
else
echo "Error analyzing \"$file\": $gocyclo_output" >> error_gocyclo_output.txt
fi
fi
done < changed_files.txt
# -over option shows functions with complexity > N only and
# returns exit code 1 if the set is non-empty
set -e
# Process the analysis results
if [[ -s raw_gocyclo_output.txt ]]; then
grep -v 'Error analyzing' raw_gocyclo_output.txt > filtered_gocyclo_output.txt
if [[ -s filtered_gocyclo_output.txt ]]; then
sorted_results=$(sort -nr -k1 filtered_gocyclo_output.txt)
over_40=$(echo "$sorted_results" | awk '$1 > 40')
between_20_40=$(echo "$sorted_results" | awk '$1 >= 20 && $1 <= 40')
between_10_20=$(echo "$sorted_results" | awk '$1 >= 10 && $1 < 20')
under_10=$(echo "$sorted_results" | awk '$1 < 10')
else
sorted_results=""
over_40=""
between_20_40=""
between_10_20=""
under_10=""
fi
else
sorted_results=""
over_40=""
between_20_40=""
between_10_20=""
under_10=""
fi
# Create a report
gocyclo_report=""
{
echo "## gocyclo Report"
if [ -n "$over_40" ]; then
echo "### 💣 Results with cyclomatic complexity over 40"
echo "**Extremely complex**"
echo "Code with a cyclomatic complexity of over 40 is extremely complex and difficult to understand. Maintenance and testing are extremely difficult, and the risk of errors and vulnerabilities is very high, making refactoring essential."
echo ""
echo "| complexity | function | file:line:column |"
echo "|--|--|--|"
echo "$over_40" | awk '{print "| " $1 " | " $3 " | " $4 " |"}'
fi
if [ -n "$between_20_40" ]; then
echo "### 🔥 Results with cyclomatic complexity between 20 and 40"
echo "**High complexity**"
echo "Code with a cyclomatic complexity of 20 to 40 is very complex, making it difficult to understand and maintain, and increasing the risk of errors and bugs. Refactoring is strongly recommended."
echo ""
echo "| complexity | function | file:line:column |"
echo "|--|--|--|"
echo "$between_20_40" | awk '{print "| " $1 " | " $3 " | " $4 " |"}'
fi
if [ -n "$between_10_20" ];then
echo "### 🧐 Results with cyclomatic complexity between 10 and 20"
echo "**Moderate complexity**"
echo "Code with a cyclomatic complexity of 10 to 20 has moderate complexity, making it difficult to understand and maintain. The complexity at this level makes testing and debugging the code time-consuming, and increases the risk of errors. Therefore, it is recommended to refactor as much as possible."
echo ""
echo "| complexity | function | file:line:column |"
echo "|--|--|--|"
echo "$between_10_20" | awk '{print "| " $1 " | " $3 " | " $4 " |"}'
fi
if [ -n "$under_10" ];then
echo "### 🌟 Results with cyclomatic complexity under 10"
echo "**Low complexity**"
echo "Code with a cyclomatic complexity of less than 10 is simple, easy to understand, and easy to maintain. It has fewer bugs, allowing for rapid development and the delivery of high-quality software."
echo ""
echo "| complexity | function | file:line:column |"
echo "|--|--|--|"
echo "$under_10" | awk '{print "| " $1 " | " $3 " | " $4 " |"}'
fi
echo "## gocyclo Errors"
echo ""
if [[ -s error_gocyclo_output.txt ]];then
cat error_gocyclo_output.txt
else
echo "✅ No errors found during gocyclo analysis."
fi
} > gocyclo_report
echo "gocyclo_report<<EOF" >> $GITHUB_ENV
cat gocyclo_report >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV