├── .github └── workflows │ └── release.yml ├── LICENSE ├── README.md ├── VERSION ├── action.yml ├── assets └── example.webp ├── main.sh └── sql-review.sh /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | paths: 6 | - "VERSION" 7 | 8 | name: Create Release 9 | 10 | jobs: 11 | build: 12 | name: Create Release 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Get Version 17 | id: get-version 18 | run: | 19 | verion=`cat VERSION` 20 | echo "version=$verion" >> $GITHUB_OUTPUT 21 | - name: Create Tag 22 | uses: negz/create-tag@v1 23 | with: 24 | version: ${{ steps.get-version.outputs.version }} 25 | message: "Release ${{ steps.get-version.outputs.version }}" 26 | token: ${{ secrets.GITHUB_TOKEN }} 27 | - name: Create Release 28 | id: create_release 29 | uses: actions/create-release@v1 30 | env: 31 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 32 | with: 33 | tag_name: ${{ steps.get-version.outputs.version }} 34 | release_name: Release ${{ steps.get-version.outputs.version }} 35 | draft: false 36 | prerelease: false 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Bytebase 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deprecate 2 | 3 | Use https://www.bytebase.com/docs/tutorials/github-ci/ instead. 4 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.1.0 -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'SQL Review' 2 | description: 'Parse and check the SQL statement according to the SQL review rules' 3 | inputs: 4 | override-file-path: 5 | description: 'SQL check configuration override file path. Required if the template is not specified.' 6 | required: false 7 | default: "" 8 | database-type: 9 | description: 'The database type. Please check https://github.com/bytebase/sql-review-action/blob/main/README.md#about-parameters for support database engines.' 10 | required: true 11 | template-id: 12 | description: 'The SQL check template id, required if the "override-file-path" is not specified. Should be one of "bb.sql-review.prod", "bb.sql-review.dev".' 13 | required: false 14 | file-pattern: 15 | description: 'The file path pattern for your SQL files. Defaults "^.*\.sql$"' 16 | required: false 17 | default: '^.*\.sql$' 18 | runs: 19 | using: "composite" 20 | steps: 21 | - name: Get changed files 22 | id: changed-files 23 | uses: umani/changed-files@v4.1.0 24 | with: 25 | repo-token: ${{ github.token }} 26 | pattern: "${{ inputs.file-pattern }}" 27 | - name: SQL Review 28 | run: | 29 | $GITHUB_ACTION_PATH/main.sh --files="${{ steps.changed-files.outputs.files_created }} ${{ steps.changed-files.outputs.files_updated }}" --database-type=${{ inputs.database-type }} --override-file="${{ inputs.override-file-path }}" --template-id="${{ inputs.template-id }}" 30 | shell: bash 31 | branding: 32 | icon: "database" 33 | color: "gray-dark" 34 | -------------------------------------------------------------------------------- /assets/example.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bytebase/sql-review-action/c81c062093d1aea24e54b512888b13fbfac2d1f0/assets/example.webp -------------------------------------------------------------------------------- /main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # =========================================================================== 3 | # File: main.sh 4 | # Description: usage: ./main.sh --files=[files] --database-type=[database type] --override-file=[override file path] --template-id=[template id] 5 | # =========================================================================== 6 | 7 | # Get parameters 8 | for i in "$@" 9 | do 10 | case $i in 11 | --files=*) 12 | FILES="${i#*=}" 13 | shift 14 | ;; 15 | --database-type=*) 16 | DATABASE_TYPE="${i#*=}" 17 | shift 18 | ;; 19 | --override-file=*) 20 | OVERRIDE_FILE="${i#*=}" 21 | shift 22 | ;; 23 | --template-id=*) 24 | TEMPLATE_ID="${i#*=}" 25 | shift 26 | ;; 27 | *) # unknown option 28 | ;; 29 | esac 30 | done 31 | 32 | # Use BB_SQL_API from environment as default value. 33 | # Users can deploy their own SQL Service from https://github.com/Bytebase/Bytebase/blob/main/Dockerfile.sql-service, thus they can get their own SQL check API and inject it into the repo's environment 34 | API_URL=$BB_SQL_API 35 | if [ -z $API_URL ] 36 | then 37 | API_URL=https://sql.bytebase.com/v1/advise 38 | fi 39 | 40 | override="" 41 | if [ ! -z $OVERRIDE_FILE ] 42 | then 43 | override=`cat $OVERRIDE_FILE` 44 | 45 | if [ $? != 0 ] 46 | then 47 | echo "::error file=$FILE,line=1,col=5,endColumn=7::Cannot find SQL review config file" 48 | exit 1 49 | fi 50 | fi 51 | 52 | result=0 53 | for FILE in $FILES; do 54 | if [[ $FILE =~ \.sql$ ]]; then 55 | echo "Start check statement in file $FILE" 56 | $GITHUB_ACTION_PATH/sql-review.sh --file=$FILE --database-type=$DATABASE_TYPE --override="$override" --template-id="$TEMPLATE_ID" --api=$API_URL 57 | if [ $? != 0 ]; then 58 | result=1 59 | fi 60 | fi 61 | done 62 | 63 | exit $result 64 | -------------------------------------------------------------------------------- /sql-review.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # =========================================================================== 3 | # File: sql-review.sh 4 | # Description: usage: ./sql-review.sh --file=[file] --database-type=[database type] --override=[override] --template-id=[template id] --api=[API URL] 5 | # =========================================================================== 6 | 7 | # Get parameters 8 | for i in "$@" 9 | do 10 | case $i in 11 | --file=*) 12 | FILE="${i#*=}" 13 | shift 14 | ;; 15 | --database-type=*) 16 | DATABASE_TYPE="${i#*=}" 17 | shift 18 | ;; 19 | --override=*) 20 | OVERRIDE="${i#*=}" 21 | shift 22 | ;; 23 | --template-id=*) 24 | TEMPLATE_ID="${i#*=}" 25 | shift 26 | ;; 27 | --api=*) 28 | API_URL="${i#*=}" 29 | shift 30 | ;; 31 | *) # unknown option 32 | ;; 33 | esac 34 | done 35 | 36 | DOC_URL=https://www.bytebase.com/docs/reference/error-code/advisor 37 | NUM_REGEX='^[0-9]+$' 38 | 39 | statement=`cat $FILE` 40 | if [ $? != 0 ]; then 41 | echo "::error::Cannot open file $FILE" 42 | exit 1 43 | fi 44 | 45 | version=`cat $GITHUB_ACTION_PATH/VERSION` 46 | actor=`echo $GITHUB_ACTOR | tr '[:upper:]' '[:lower:]'` 47 | repository=`echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]'` 48 | 49 | request_body=$(jq -n \ 50 | --arg statement "$statement" \ 51 | --arg override "$OVERRIDE" \ 52 | --arg databaseType "$DATABASE_TYPE" \ 53 | --arg templateId "$TEMPLATE_ID" \ 54 | '$ARGS.named') 55 | response=$(curl -s -w "%{http_code}" -X POST $API_URL \ 56 | -H "X-Platform: GitHub" \ 57 | -H "X-Repository: $repository" \ 58 | -H "X-Actor: $actor" \ 59 | -H "X-Version: $version" \ 60 | -H "X-Source: action" \ 61 | -H "Content-Type: application/json" \ 62 | -d "$request_body") 63 | http_code=$(tail -n1 <<< "$response") 64 | body=$(sed '$ d' <<< "$response") 65 | 66 | echo "::debug::response code: $http_code, response body: $body" 67 | 68 | if [ $http_code != 200 ]; then 69 | echo ":error::Failed to check SQL with response code $http_code and body $body" 70 | exit 1 71 | fi 72 | 73 | result=0 74 | index=0 75 | 76 | while read code; do 77 | content=`echo $body | jq -r ".[$index].content"` 78 | status=`echo $body | jq -r ".[$index].status"` 79 | title=`echo $body | jq -r ".[$index].title"` 80 | line=`echo $body | jq -r ".[$index].line"` 81 | (( index++ )) 82 | 83 | echo "::debug::status:$status, code:$code, title:$title, line:$line, content:$content" 84 | 85 | if [ -z "$content" ]; then 86 | # The content cannot be empty. Otherwise action cannot output the error message in files. 87 | content=$title 88 | fi 89 | 90 | if [ $code != 0 ]; then 91 | title="$title ($code)" 92 | # To indent the output message 93 | content="$content 94 | Doc: $DOC_URL#$code" 95 | 96 | if ! [[ $line =~ $NUM_REGEX ]] ; then 97 | line=1 98 | fi 99 | if [ $line -le 0 ];then 100 | line=1 101 | fi 102 | 103 | echo "### [$status] $title" >> $GITHUB_STEP_SUMMARY 104 | echo "$content" >> $GITHUB_STEP_SUMMARY 105 | 106 | content="${content//$'\n'/'%0A'}" 107 | error_msg="file=$FILE,line=$line,col=1,endColumn=2,title=$title::$content" 108 | 109 | if [ $status == 'WARN' ]; then 110 | echo "::warning $error_msg" 111 | else 112 | result=$code 113 | echo "::error $error_msg" 114 | fi 115 | fi 116 | done <<< "$(echo $body | jq -r '.[]' | jq '.code')" 117 | 118 | exit $result 119 | --------------------------------------------------------------------------------