Skip to content

Commit

Permalink
feat: use ast-grep as lint engine
Browse files Browse the repository at this point in the history
- replace placeholders in action.yml
- add sgconfig.yml and teatdata
- add to-rdjsonl.jq and fix script.sh

Signed-off-by: Rintaro Okamura <[email protected]>
  • Loading branch information
rinx committed Aug 22, 2024
1 parent 474e53f commit 1a06abb
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 47 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/depup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 4 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ on:
branches:
- main
pull_request:
# TODO: replace `<linter-name>` with yours
jobs:
test-check:
name: runner / <linter-name> (github-check)
name: runner / ast-grep (github-check)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -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 / <linter-name> (github-pr-check)
name: runner / ast-grep (github-pr-check)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -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 / <linter-name> (github-pr-review)
name: runner / ast-grep (github-pr-review)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -44,19 +40,17 @@ 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
if: ${{ !success() }}
run: echo 'The previous step should fail' && exit 1

test-sarif:
name: runner / <linter-name> (sarif)
name: runner / ast-grep (sarif)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./
with:
reporter: sarif
locale: "US"
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# action-composite-template

<!-- TODO: replace reviewdog/action-composite-template with your repo name -->
[![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)
Expand All @@ -26,7 +25,7 @@ This repo contains a sample action to run [misspell](https://github.com/client9/

## Input

<!-- TODO: replace `<linter-name>` with yours -->
<!-- TODO: replace `ast-grep` with yours -->
```yaml
inputs:
github_token:
Expand All @@ -38,7 +37,7 @@ inputs:
### Flags for reviewdog ###
tool_name:
description: 'Tool name to use for reviewdog reporter.'
default: '<linter-name>'
default: 'ast-grep'
level:
description: 'Report level for reviewdog [info,warning,error].'
default: 'error'
Expand All @@ -58,26 +57,26 @@ inputs:
reviewdog_flags:
description: 'Additional reviewdog flags.'
default: ''
### Flags for <linter-name> ###
### Flags for ast-grep ###
locale:
description: '-locale flag of misspell. (US/UK)'
default: ''
```
## Usage
<!-- TODO: replace reviewdog/action-composite-template with your repo name -->
<!-- TODO: replace reviewdog/action-ast-grep with your repo name -->
```yaml
name: reviewdog
on: [pull_request]
jobs:
# TODO: replace `linter_name` and `<linter-name>` with yours
# TODO: replace `linter_name` and `ast-grep` with yours
linter_name:
name: runner / <linter-name>
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].
Expand Down
24 changes: 14 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# TODO: replace `<linter-name>` and `<your-name>` with yours
name: 'Run <linter-name> with reviewdog'
description: '🐶 Run <linter-name> with reviewdog on pull requests to improve code review experience.'
author: '<your-name>'
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'
Expand All @@ -12,7 +11,7 @@ inputs:
### Flags for reviewdog ###
tool_name:
description: 'Tool name to use for reviewdog reporter.'
default: '<linter-name>'
default: 'ast-grep'
level:
description: 'Report level for reviewdog [info,warning,error].'
default: 'error'
Expand All @@ -35,10 +34,14 @@ inputs:
output_dir:
description: 'Output directory of reviewdog result. Useful for -reporter=sarif'
default: '../reviewdog-results'
### Flags for <linter-name> ###
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:
Expand All @@ -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'
21 changes: 16 additions & 5 deletions script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,16 +17,23 @@ 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"

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" \
${SG_PATH}/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}" \
Expand Down
2 changes: 2 additions & 0 deletions sgconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruleDirs:
- ./testdata/rules
18 changes: 18 additions & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
11 changes: 11 additions & 0 deletions testdata/rules/avoid-use-deprecated-function.yml
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions testdata/rules/no-context-todo.yml
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions testdata/rules/no-fixme-comment.yml
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 0 additions & 2 deletions testdata/subdir/text.md

This file was deleted.

5 changes: 0 additions & 5 deletions testdata/text.md

This file was deleted.

46 changes: 46 additions & 0 deletions to-rdjsonl.jq
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1a06abb

Please sign in to comment.