diff --git a/.github/workflows/depup.yml b/.github/workflows/depup.yml index 8f898e2..dfe3e01 100644 --- a/.github/workflows/depup.yml +++ b/.github/workflows/depup.yml @@ -17,3 +17,13 @@ jobs: version_name: reviewdog_version repo: reviewdog/reviewdog labels: "bump:minor" + ast-grep: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: reviewdog/action-depup/with-pr@v1 + with: + file: action.yml + version_name: DEFAULT_SG_VERSION + repo: ast-grep/ast-grep + labels: "bump:minor" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f26e8c2..8486aa3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,10 +4,9 @@ on: branches: - main pull_request: -# TODO: replace `` with yours jobs: test-check: - name: runner / (github-check) + name: runner / ast-grep (github-check) runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -16,11 +15,10 @@ jobs: github_token: ${{ secrets.github_token }} reporter: github-check level: info - locale: "US" test-pr-check: if: github.event_name == 'pull_request' - name: runner / (github-pr-check) + name: runner / ast-grep (github-pr-check) runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -29,12 +27,10 @@ jobs: github_token: ${{ secrets.github_token }} reporter: github-pr-check level: warning - locale: "US" - workdir: ./testdata/subdir/ test-pr-review: if: github.event_name == 'pull_request' - name: runner / (github-pr-review) + name: runner / ast-grep (github-pr-review) runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -44,7 +40,6 @@ jobs: github_token: ${{ secrets.github_token }} reporter: github-pr-review level: error - locale: "US" filter_mode: file fail_on_error: true - name: check the exit code @@ -52,11 +47,10 @@ jobs: run: echo 'The previous step should fail' && exit 1 test-sarif: - name: runner / (sarif) + name: runner / ast-grep (sarif) runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ./ with: reporter: sarif - locale: "US" diff --git a/README.md b/README.md index 720a859..48fe37b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ -# action-composite-template - - -[![Test](https://github.com/reviewdog/action-composite-template/workflows/Test/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3ATest) -[![reviewdog](https://github.com/reviewdog/action-composite-template/workflows/reviewdog/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3Areviewdog) -[![depup](https://github.com/reviewdog/action-composite-template/workflows/depup/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3Adepup) -[![release](https://github.com/reviewdog/action-composite-template/workflows/release/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3Arelease) -[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/reviewdog/action-composite-template?logo=github&sort=semver)](https://github.com/reviewdog/action-composite-template/releases) +# action-ast-grep + +[![Test](https://github.com/reviewdog/action-ast-grep/workflows/Test/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3ATest) +[![reviewdog](https://github.com/reviewdog/action-ast-grep/workflows/reviewdog/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3Areviewdog) +[![depup](https://github.com/reviewdog/action-ast-grep/workflows/depup/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3Adepup) +[![release](https://github.com/reviewdog/action-ast-grep/workflows/release/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3Arelease) +[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/reviewdog/action-ast-grep?logo=github&sort=semver)](https://github.com/reviewdog/action-ast-grep/releases) [![action-bumpr supported](https://img.shields.io/badge/bumpr-supported-ff69b4?logo=github&link=https://github.com/haya14busa/action-bumpr)](https://github.com/haya14busa/action-bumpr) ![github-pr-review demo](https://user-images.githubusercontent.com/3797062/73162963-4b8e2b00-4132-11ea-9a3f-f9c6f624c79f.png) @@ -26,7 +25,7 @@ This repo contains a sample action to run [misspell](https://github.com/client9/ ## Input - + ```yaml inputs: github_token: @@ -38,7 +37,7 @@ inputs: ### Flags for reviewdog ### tool_name: description: 'Tool name to use for reviewdog reporter.' - default: '' + default: 'ast-grep' level: description: 'Report level for reviewdog [info,warning,error].' default: 'error' @@ -58,26 +57,26 @@ inputs: reviewdog_flags: description: 'Additional reviewdog flags.' default: '' - ### Flags for ### + ### Flags for ast-grep ### locale: description: '-locale flag of misspell. (US/UK)' default: '' ``` ## Usage - + ```yaml name: reviewdog on: [pull_request] jobs: - # TODO: replace `linter_name` and `` with yours + # TODO: replace `linter_name` and `ast-grep` with yours linter_name: - name: runner / + name: runner / ast-grep runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: reviewdog/action-composite-template@v1 + - uses: reviewdog/action-ast-grep@v1 with: github_token: ${{ secrets.github_token }} # Change reviewdog reporter if you need [github-check,github-pr-review,github-pr-check]. diff --git a/action.yml b/action.yml index b5243b0..b08c7cb 100644 --- a/action.yml +++ b/action.yml @@ -1,7 +1,6 @@ -# TODO: replace `` and `` with yours -name: 'Run with reviewdog' -description: '🐶 Run with reviewdog on pull requests to improve code review experience.' -author: '' +name: 'Run ast-grep with reviewdog' +description: '🐶 Run ast-grep with reviewdog on pull requests to improve code review experience.' +author: 'rinx' inputs: github_token: description: 'GITHUB_TOKEN' @@ -12,7 +11,7 @@ inputs: ### Flags for reviewdog ### tool_name: description: 'Tool name to use for reviewdog reporter.' - default: '' + default: 'ast-grep' level: description: 'Report level for reviewdog [info,warning,error].' default: 'error' @@ -35,10 +34,14 @@ inputs: output_dir: description: 'Output directory of reviewdog result. Useful for -reporter=sarif' default: '../reviewdog-results' - ### Flags for ### - locale: - description: '-locale flag of misspell. (US/UK)' + ### Flags for ast-grep ### + sg_version: + description: 'ast-grep version.' default: '' + sg_config: + description: 'path to the ast-grep config file' + default: 'sgconfig.yml' + runs: using: 'composite' steps: @@ -59,14 +62,15 @@ runs: INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }} INPUT_REVIEWDOG_FLAGS: ${{ inputs.reviewdog_flags }} INPUT_OUTPUT_DIR: ${{ inputs.output_dir }} - INPUT_LOCALE: ${{ inputs.locale }} + INPUT_SG_VERSION: ${{ inputs.sg_version }} + INPUT_SG_CONFIG: ${{ inputs.sg_config }} + DEFAULT_SG_VERSION: 0.26.2 - if: inputs.reporter == 'sarif' uses: github/codeql-action/upload-sarif@v3 with: sarif_file: ${{ inputs.output_dir }} # Ref: https://haya14busa.github.io/github-action-brandings/ -# TODO: update branding if you want. branding: icon: 'check' color: 'blue' diff --git a/script.sh b/script.sh index 379ac88..d2c4104 100755 --- a/script.sh +++ b/script.sh @@ -5,6 +5,10 @@ if [ -n "${GITHUB_WORKSPACE}" ]; then cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit fi +if [ -z "${INPUT_SG_VERSION}" ]; then + INPUT_SG_VERSION="${DEFAULT_SG_VERSION}" +fi + mkdir -p "${INPUT_OUTPUT_DIR}" OUTPUT_FILE_NAME="reviewdog-${INPUT_TOOL_NAME}" if [[ "${INPUT_REPORTER}" == "sarif" ]]; then @@ -13,16 +17,27 @@ fi export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}" -echo '::group::🐶 Installing misspell ... https://github.com/client9/misspell' +echo '::group::🐶 Installing ast-grep ... https://github.com/ast-grep/ast-grep' TEMP_PATH="$(mktemp -d)" PATH="${TEMP_PATH}:$PATH" -wget -O - -q https://git.io/misspell | sh -s -- -b "${TEMP_PATH}" + +curl --silent --show-error --fail \ + --location "https://github.com/ast-grep/ast-grep/releases/download/${INPUT_SG_VERSION}/app-x86_64-unknown-linux-gnu.zip" \ + --output "${TEMP_PATH}/sg.zip" + +unzip -u "${TEMP_PATH}/sg.zip" -d "${TEMP_PATH}/temp-sg" +install "${TEMP_PATH}/temp-sg/ast-grep" "${TEMP_PATH}" +rm -rf "${TEMP_PATH}/sg.zip" "${TEMP_PATH}/temp-sg" + echo '::endgroup::' -echo '::group:: Running misspell with reviewdog 🐶 ...' +echo '::group:: Running ast-grep with reviewdog 🐶 ...' + # shellcheck disable=SC2086 -misspell -locale="${INPUT_LOCALE}" . | - reviewdog -efm="%f:%l:%c: %m" \ +ast-grep scan --config="${INPUT_SG_CONFIG}" --json=compact | + jq -f "to-rdjsonl.jq" -c | + reviewdog \ + -f=rdjsonl \ -name="${INPUT_TOOL_NAME}" \ -reporter="${INPUT_REPORTER}" \ -filter-mode="${INPUT_FILTER_MODE}" \ diff --git a/sgconfig.yml b/sgconfig.yml new file mode 100644 index 0000000..a414286 --- /dev/null +++ b/sgconfig.yml @@ -0,0 +1,2 @@ +ruleDirs: +- ./testdata/rules diff --git a/testdata/main.go b/testdata/main.go new file mode 100644 index 0000000..06e914e --- /dev/null +++ b/testdata/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "context" + "fmt" +) + +func main() { + _ = context.TODO() + + fmt.Println("hello world") +} + +// Deprecated: deprecated function +func New() { + // FIXME: implement me + fmt.Println("new function") +} diff --git a/testdata/rules/avoid-use-deprecated-function.yml b/testdata/rules/avoid-use-deprecated-function.yml new file mode 100644 index 0000000..b80715a --- /dev/null +++ b/testdata/rules/avoid-use-deprecated-function.yml @@ -0,0 +1,11 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json + +id: avoid-use-deprecated-function +message: Avoid to use deprecated function +severity: warning # error, warning, info, hint +language: Go +rule: + kind: comment + regex: "//\\s*[dD]eprecated:.*" +# utils: Extract repeated rule as local utility here. +# note: Add detailed explanation for the rule. diff --git a/testdata/rules/no-context-todo.yml b/testdata/rules/no-context-todo.yml new file mode 100644 index 0000000..9579eb6 --- /dev/null +++ b/testdata/rules/no-context-todo.yml @@ -0,0 +1,11 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json + +id: no-context-todo +message: Do not use context.TODO in production code +severity: error # error, warning, info, hint +language: Go +rule: + pattern: context.TODO() +# utils: Extract repeated rule as local utility here. +note: context.TODO() is just a temporary thing +fix: context.Background() diff --git a/testdata/rules/no-fixme-comment.yml b/testdata/rules/no-fixme-comment.yml new file mode 100644 index 0000000..7f90f69 --- /dev/null +++ b/testdata/rules/no-fixme-comment.yml @@ -0,0 +1,11 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json + +id: no-fixme-comment +message: Do not leave fixme comment +severity: info +language: Go +rule: + kind: comment + regex: "//\\s*FIXME:.*" +# utils: Extract repeated rule as local utility here. +# note: Add detailed explanation for the rule. diff --git a/testdata/subdir/text.md b/testdata/subdir/text.md deleted file mode 100644 index 5b33346..0000000 --- a/testdata/subdir/text.md +++ /dev/null @@ -1,2 +0,0 @@ -Determinisitic result is important! - diff --git a/testdata/text.md b/testdata/text.md deleted file mode 100644 index 5025db8..0000000 --- a/testdata/text.md +++ /dev/null @@ -1,5 +0,0 @@ -Determinisitic result is important. - -colour # <= Check -locale - -langauge diff --git a/to-rdjsonl.jq b/to-rdjsonl.jq new file mode 100644 index 0000000..b0941ba --- /dev/null +++ b/to-rdjsonl.jq @@ -0,0 +1,46 @@ +.[] | +. as $item | +{ + "message": (if .note then (.message + " (Note: " + .note + ")") else .message end), + "severity": .severity | ascii_upcase, + "location": { + "path": .file, + "range": { + "start": { + "line": (.range.start.line + 1), + "column": (.range.start.column + 1) + }, + "end": { + "line": (.range.end.line + 1), + "column": (.range.end.column + 1) + } + } + }, + "code": { + "value": .ruleId, + "url": "https://github.com/ast-grep/ast-grep" + }, + "source": { + "name": "ast-grep", + "url": "https://github.com/ast-grep/ast-grep" + }, + "original_output": . | tostring +} + if .replacement then +{ + "suggestions": [ + { + "range": { + "start": { + "line": (.range.start.line + 1), + "column": (.range.start.column + 1) + }, + "end": { + "line": (.range.end.line + 1), + "column": (.range.end.column + 1) + } + }, + "text": .replacement + } + ] +} +else {} end