|
| 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 |
0 commit comments