Skip to content

Commit 824aa45

Browse files
committed
add ci
0 parents  commit 824aa45

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

sql-review.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
3+
# Get parameters
4+
for i in "$@"
5+
do
6+
case $i in
7+
--files=*)
8+
FILES="${i#*=}"
9+
shift
10+
;;
11+
--api=*)
12+
API_URL="${i#*=}"
13+
shift
14+
;;
15+
*) # unknown option
16+
;;
17+
esac
18+
done
19+
20+
echo "SQL review start"
21+
echo "Changed files: $FILES"
22+
23+
DOC_URL=https://www.bytebase.com/docs/reference/error-code/advisor
24+
NUM_REGEX='^[0-9]+$'
25+
26+
# junit xml format: https://llg.cubic.org/docs/junit/
27+
xml=""
28+
result=0
29+
30+
for FILE in $FILES; do
31+
echo "Check file $FILE"
32+
33+
if ! [[ $FILE =~ \.sql$ ]]; then
34+
continue
35+
fi
36+
37+
echo "Start check statement in file $FILE"
38+
39+
statement=`cat $FILE`
40+
if [[ $? != 0 ]]; then
41+
echo "::error::Cannot open file $FILE"
42+
exit 1
43+
fi
44+
45+
request_body=$(jq -n \
46+
--arg filePath "$FILE" \
47+
--arg statement "$statement" \
48+
'$ARGS.named')
49+
response=$(curl -s -w "%{http_code}" -X POST $API_URL \
50+
-H "X-Platform: GitLab" \
51+
-H "X-Repository: $CI_PROJECT_DIR" \
52+
-H "X-Actor: $GITLAB_USER_LOGIN" \
53+
-H "X-Source: action" \
54+
-H "Content-Type: application/json" \
55+
-d "$request_body")
56+
http_code=$(tail -n1 <<< "$response")
57+
body=$(sed '$ d' <<< "$response")
58+
echo "::debug::response code: $http_code, response body: $body"
59+
60+
if [[ $http_code != 200 ]]; then
61+
echo ":error::Failed to check SQL with response code $http_code and body $body"
62+
exit 1
63+
fi
64+
65+
testcase=""
66+
index=0
67+
68+
while read code; do
69+
content=`echo $body | jq -r ".[$index].content"`
70+
status=`echo $body | jq -r ".[$index].status"`
71+
title=`echo $body | jq -r ".[$index].title"`
72+
line=`echo $body | jq -r ".[$index].line"`
73+
(( index++ ))
74+
75+
echo "::debug::status:$status, code:$code, title:$title, line:$line, content:$content"
76+
77+
if [[ -z "$content" ]]; then
78+
# The content cannot be empty. Otherwise action cannot output the error message in files.
79+
content=$title
80+
fi
81+
82+
if [[ $code != 0 ]]; then
83+
title="$title ($code)"
84+
85+
if ! [[ $line =~ $NUM_REGEX ]] ; then
86+
line=1
87+
fi
88+
if [[ $line -le 0 ]];then
89+
line=1
90+
fi
91+
92+
content="
93+
File: $FILE
94+
Line: $line
95+
Level: $status
96+
Rule: $title
97+
Error: $content
98+
Doc: $DOC_URL#$code"
99+
100+
testcase="$testcase<testcase name=\"$title\" classname=\"$FILE\" file=\"$FILE#L$line\"><failure>$content</failure></testcase>"
101+
102+
if [[ $status == 'ERROR' ]]; then
103+
result=$code
104+
fi
105+
fi
106+
done <<< "$(echo $body | jq -r '.[]' | jq '.code')"
107+
108+
if [[ ! -z $testcase ]]; then
109+
xml="$xml<testsuite name=\"$FILE\">$testcase</testsuite>"
110+
fi
111+
done
112+
113+
if [[ ! -z $xml ]]; then
114+
echo "output the xml"
115+
echo $xml
116+
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><testsuites name=\"SQL Review\">$xml</testsuites>" > sql-review.xml
117+
fi
118+
119+
exit $result

sql-review.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
sql-review:
2+
# https://docs.gitlab.com/ee/ci/jobs/job_control.html#use-onlychanges-with-merge-request-pipelines
3+
only:
4+
refs:
5+
- merge_requests
6+
image: docker:stable
7+
variables:
8+
SQL_REVIEW_API: "" # This is the API for SQL reivew.
9+
before_script:
10+
- apk update && apk add git
11+
- apk update && apk add bash
12+
- apk update && apk add curl
13+
- apk update && apk add jq
14+
- git --version
15+
script:
16+
- ls
17+
- echo "Hello, $GITLAB_USER_LOGIN!"
18+
- echo "The commit hash is $CI_COMMIT_SHA, project $CI_PROJECT_DIR"
19+
- echo "The project path $CI_PROJECT_PATH"
20+
- echo "The project url $CI_PROJECT_URL"
21+
- echo "get changed files:"
22+
- git fetch origin ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
23+
# get changed files in GitLab merge request: https://forum.gitlab.com/t/ci-cd-pipeline-get-list-of-changed-files/26847/3
24+
- FILES=$(git diff-tree --no-commit-id --diff-filter=AM --name-only -r origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME -r $CI_COMMIT_SHA)
25+
- echo $FILES
26+
# https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2672
27+
- bash $CI_PROJECT_DIR/.gitlab/sql-review.sh --files="$FILES" --api="$SQL_REVIEW_API"
28+
artifacts:
29+
when: always
30+
reports:
31+
junit:
32+
- sql-review.xml

0 commit comments

Comments
 (0)