forked from celinekurpershoek/link-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·70 lines (58 loc) · 1.98 KB
/
entrypoint.sh
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
#!/bin/bash
# Fail when any task exits with a non zero error;
set -e
NC='\033[0m' # No Color
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
PURPLE='\033[0;34m'
# Install the broken-link-checker module globally on the docker instance
npm i -g broken-link-checker -s
echo -e "$PURPLE=== BROKEN LINK CHECKER ===$NC"
echo -e "Running broken link checker on url: $GREEN $1 $NC"
# Create exclude and settings strings based on configuration
EXCLUDE=""
SET_FOLLOW=""
if [ -z "$1" ] || [ "$1" == 'https://github.com/celinekurpershoek/github-actions-link-checker' ]
then
echo -e "$YELLOW Warning: Running test on default url, please provide an url in you action yml.$NC"
fi
# Set arguments for blc
[ "$2" == false ] && SET_FOLLOW="--follow"
for PATTERN in ${3//,/ }; do
EXCLUDE+="--exclude $PATTERN "
done
# Echo settings if any are set
echo -e "Configuration: Honor robot exclusions: $GREEN$2$NC, Exclude urls that match: $GREEN$3$NC \n"
# Create command and remove extra quotes
# Put result in variable to be able to iterate on it later
OUTPUT="$(blc "$1" "$EXCLUDE" $SET_FOLLOW -v | sed 's/"//g')"
# Count lines of output
TOTAL_COUNT="$(wc -l <<< "$OUTPUT")"
# Count 'BROKEN' lines of result or return 0
if grep -q 'BROKEN' <<< "$OUTPUT"
then
BROKEN="$(grep -q 'BROKEN' <<< "$OUTPUT")"
BROKEN_COUNT="$(wc -l <<< "$BROKEN")"
else
BROKEN_COUNT=0
fi
# Return results
if [ "$BROKEN_COUNT" -gt 0 ]
then
RESULT="$BROKEN_COUNT broken url(s) found ($TOTAL_COUNT total)"
echo -e "$RED Failed $RESULT: $NC"
grep -E 'BROKEN' <<< "$OUTPUT" | awk '{print "[✗] " $2 "\n" }'
echo -e "$PURPLE ============================== $NC"
echo ::set-output name=result::"$RESULT"
exit 1
elif [ "$TOTAL_COUNT" == 0 ]
then
echo -e "Did'nt find any links to check"
else
RESULT="✓ Checked $TOTAL_COUNT link(s), no broken links found!"
echo -e "$GREEN $RESULT $NC"
echo ::set-output name=result::"$RESULT"
echo -e "$PURPLE ============================== $NC"
fi
exit 0