forked from bytebase/sql-review-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-review.sh
executable file
·118 lines (101 loc) · 2.99 KB
/
sql-review.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
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
#!/bin/bash
# ===========================================================================
# File: sql-review.sh
# Description: usage: ./sql-review.sh --file=[file] --database-type=[database type] --override=[override] --template-id=[template id] --api=[API URL]
# ===========================================================================
# Get parameters
for i in "$@"
do
case $i in
--file=*)
FILE="${i#*=}"
shift
;;
--database-type=*)
DATABASE_TYPE="${i#*=}"
shift
;;
--override=*)
OVERRIDE="${i#*=}"
shift
;;
--template-id=*)
TEMPLATE_ID="${i#*=}"
shift
;;
--api=*)
API_URL="${i#*=}"
shift
;;
*) # unknown option
;;
esac
done
DOC_URL=https://www.bytebase.com/docs/reference/error-code/advisor
NUM_REGEX='^[0-9]+$'
statement=`cat $FILE`
if [ $? != 0 ]; then
echo "::error::Cannot open file $FILE"
exit 1
fi
version=`cat $GITHUB_ACTION_PATH/VERSION`
actor=`echo $GITHUB_ACTOR | tr '[:upper:]' '[:lower:]'`
repository=`echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]'`
request_body=$(jq -n \
--arg statement "$statement" \
--arg override "$OVERRIDE" \
--arg databaseType "$DATABASE_TYPE" \
--arg templateId "$TEMPLATE_ID" \
'$ARGS.named')
response=$(curl -s -w "%{http_code}" -X POST $API_URL \
-H "X-Platform: GitHub" \
-H "X-Repository: $repository" \
-H "X-Actor: $actor" \
-H "X-Version: $version" \
-H "X-Source: action" \
-H "Content-Type: application/json" \
-d "$request_body")
http_code=$(tail -n1 <<< "$response")
body=$(sed '$ d' <<< "$response")
echo "::debug::response code: $http_code, response body: $body"
if [ $http_code != 200 ]; then
echo ":error::Failed to check SQL with response code $http_code and body $body"
exit 1
fi
result=0
index=0
while read code; do
content=`echo $body | jq -r ".[$index].content"`
status=`echo $body | jq -r ".[$index].status"`
title=`echo $body | jq -r ".[$index].title"`
line=`echo $body | jq -r ".[$index].line"`
(( index++ ))
echo "::debug::status:$status, code:$code, title:$title, line:$line, content:$content"
if [ -z "$content" ]; then
# The content cannot be empty. Otherwise action cannot output the error message in files.
content=$title
fi
if [ $code != 0 ]; then
title="$title ($code)"
# To indent the output message
content="$content
Doc: $DOC_URL#$code"
if ! [[ $line =~ $NUM_REGEX ]] ; then
line=1
fi
if [ $line -le 0 ];then
line=1
fi
echo "### [$status] $title" >> $GITHUB_STEP_SUMMARY
echo "$content" >> $GITHUB_STEP_SUMMARY
content="${content//$'\n'/'%0A'}"
error_msg="file=$FILE,line=$line,col=1,endColumn=2,title=$title::$content"
if [ $status == 'WARN' ]; then
echo "::warning $error_msg"
else
result=$code
echo "::error $error_msg"
fi
fi
done <<< "$(echo $body | jq -r '.[]' | jq '.code')"
exit $result