Skip to content

Commit add4c80

Browse files
author
Abdullah Khawer
committedSep 2, 2024
fix: Add a condition to only generate the custom report if 1 or more secrets are found.
1 parent 952abdc commit add4c80

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed
 

‎gitleaks.sh

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,38 @@ gitleaks detect -r ${LOCAL_PATH_TO_GIT_REPO}/gitleaks-report-detailed.json -f js
1313

1414
# create a final report in JSON using the detailed report having relevant information only
1515
echo "Creating a final report in JSON using the detailed report having relevant information only..."
16-
echo "[" > ./gitleaks-report.json
17-
cat ${LOCAL_PATH_TO_GIT_REPO}/gitleaks-report-detailed.json | jq -c '.[]' | while read -r line; do
18-
description=$(jq -r '.Description' <<< "$line")
19-
start_line=$(jq -r '.StartLine' <<< "$line")
20-
file=$(jq -r '.File' <<< "$line")
21-
file=$(echo "$file" | sed "s|^${LOCAL_PATH_TO_GIT_REPO}/||")
22-
secret_type=$(jq -r '.RuleID' <<< "$line")
23-
24-
# use 'git blame' to find the commit id and author for each finding
25-
blame=$(cd ${LOCAL_PATH_TO_GIT_REPO} && git blame -L "$start_line","$start_line" ./"$file" --porcelain)
26-
commit_id=$(echo "$blame" | awk 'NR==1' | awk -F ' ' '{print $1}')
27-
author=$(echo "$blame" | awk 'NR==2' | awk -F 'author ' '{print $2}')
28-
29-
# append final JSON objects to the new report
30-
jq -n \
31-
--arg desc "$description" \
32-
--arg file "$file" \
33-
--arg line_no "$start_line" \
34-
--arg url "${REMOTE_PATH_TO_GIT_REPO}/-/blob/${BRANCH_NAME}/${file}#L${start_line}" \
35-
--arg type "$secret_type" \
36-
--arg commit "$commit_id" \
37-
--arg author "$author" \
38-
'{"Description": $desc, "File": $file, "Line No.": $line_no, "Link": $url, "Secret Type": $type, "Commit": $commit, "Author": $author}' >> ./gitleaks-report.json
39-
40-
echo "," >> ./gitleaks-report.json
41-
done
42-
head -n $(($(wc -l < ./gitleaks-report.json) - 1)) ./gitleaks-report.json > ./temp.json && mv ./temp.json ./gitleaks-report.json
43-
echo "]" >> ./gitleaks-report.json
44-
cat ./gitleaks-report.json | jq > ./temp.json && mv ./temp.json ./gitleaks-report.json
16+
if grep -q "^\[\]\n$" ${LOCAL_PATH_TO_GIT_REPO}/gitleaks-report-detailed.json; then
17+
echo "[" > ./gitleaks-report.json
18+
cat ${LOCAL_PATH_TO_GIT_REPO}/gitleaks-report-detailed.json | jq -c '.[]' | while read -r line; do
19+
description=$(jq -r '.Description' <<< "$line")
20+
start_line=$(jq -r '.StartLine' <<< "$line")
21+
file=$(jq -r '.File' <<< "$line")
22+
file=$(echo "$file" | sed "s|^${LOCAL_PATH_TO_GIT_REPO}/||")
23+
secret_type=$(jq -r '.RuleID' <<< "$line")
24+
25+
# use 'git blame' to find the commit id and author for each finding
26+
blame=$(cd ${LOCAL_PATH_TO_GIT_REPO} && git blame -L "$start_line","$start_line" ./"$file" --porcelain)
27+
commit_id=$(echo "$blame" | awk 'NR==1' | awk -F ' ' '{print $1}')
28+
author=$(echo "$blame" | awk 'NR==2' | awk -F 'author ' '{print $2}')
29+
30+
# append final JSON objects to the new report
31+
jq -n \
32+
--arg desc "$description" \
33+
--arg file "$file" \
34+
--arg line_no "$start_line" \
35+
--arg url "${REMOTE_PATH_TO_GIT_REPO}/-/blob/${BRANCH_NAME}/${file}#L${start_line}" \
36+
--arg type "$secret_type" \
37+
--arg commit "$commit_id" \
38+
--arg author "$author" \
39+
'{"Description": $desc, "File": $file, "Line No.": $line_no, "Link": $url, "Secret Type": $type, "Commit": $commit, "Author": $author}' >> ./gitleaks-report.json
40+
41+
echo "," >> ./gitleaks-report.json
42+
done
43+
head -n $(($(wc -l < ./gitleaks-report.json) - 1)) ./gitleaks-report.json > ./temp.json && mv ./temp.json ./gitleaks-report.json
44+
echo "]" >> ./gitleaks-report.json
45+
cat ./gitleaks-report.json | jq > ./temp.json && mv ./temp.json ./gitleaks-report.json
46+
else
47+
echo "[]" > ./gitleaks-report.json
48+
fi
4549

4650
echo "Script Execution Completed!"

0 commit comments

Comments
 (0)