Skip to content

Commit e178cfa

Browse files
authored
feat: use ast-grep as lint engine (#2)
- 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]>
1 parent 474e53f commit e178cfa

13 files changed

+169
-63
lines changed

.github/workflows/depup.yml

+10
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ jobs:
1717
version_name: reviewdog_version
1818
repo: reviewdog/reviewdog
1919
labels: "bump:minor"
20+
ast-grep:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
- uses: reviewdog/action-depup/with-pr@v1
25+
with:
26+
file: action.yml
27+
version_name: DEFAULT_SG_VERSION
28+
repo: ast-grep/ast-grep
29+
labels: "bump:minor"

.github/workflows/test.yml

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ on:
44
branches:
55
- main
66
pull_request:
7-
# TODO: replace `<linter-name>` with yours
87
jobs:
98
test-check:
10-
name: runner / <linter-name> (github-check)
9+
name: runner / ast-grep (github-check)
1110
runs-on: ubuntu-latest
1211
steps:
1312
- uses: actions/checkout@v4
@@ -16,11 +15,10 @@ jobs:
1615
github_token: ${{ secrets.github_token }}
1716
reporter: github-check
1817
level: info
19-
locale: "US"
2018

2119
test-pr-check:
2220
if: github.event_name == 'pull_request'
23-
name: runner / <linter-name> (github-pr-check)
21+
name: runner / ast-grep (github-pr-check)
2422
runs-on: ubuntu-latest
2523
steps:
2624
- uses: actions/checkout@v4
@@ -29,12 +27,10 @@ jobs:
2927
github_token: ${{ secrets.github_token }}
3028
reporter: github-pr-check
3129
level: warning
32-
locale: "US"
33-
workdir: ./testdata/subdir/
3430

3531
test-pr-review:
3632
if: github.event_name == 'pull_request'
37-
name: runner / <linter-name> (github-pr-review)
33+
name: runner / ast-grep (github-pr-review)
3834
runs-on: ubuntu-latest
3935
steps:
4036
- uses: actions/checkout@v4
@@ -44,19 +40,17 @@ jobs:
4440
github_token: ${{ secrets.github_token }}
4541
reporter: github-pr-review
4642
level: error
47-
locale: "US"
4843
filter_mode: file
4944
fail_on_error: true
5045
- name: check the exit code
5146
if: ${{ !success() }}
5247
run: echo 'The previous step should fail' && exit 1
5348

5449
test-sarif:
55-
name: runner / <linter-name> (sarif)
50+
name: runner / ast-grep (sarif)
5651
runs-on: ubuntu-latest
5752
steps:
5853
- uses: actions/checkout@v4
5954
- uses: ./
6055
with:
6156
reporter: sarif
62-
locale: "US"

README.md

+22-31
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
# action-composite-template
2-
3-
<!-- TODO: replace reviewdog/action-composite-template with your repo name -->
4-
[![Test](https://github.com/reviewdog/action-composite-template/workflows/Test/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3ATest)
5-
[![reviewdog](https://github.com/reviewdog/action-composite-template/workflows/reviewdog/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3Areviewdog)
6-
[![depup](https://github.com/reviewdog/action-composite-template/workflows/depup/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3Adepup)
7-
[![release](https://github.com/reviewdog/action-composite-template/workflows/release/badge.svg)](https://github.com/reviewdog/action-composite-template/actions?query=workflow%3Arelease)
8-
[![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)
9-
[![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)
1+
# action-ast-grep
102

11-
![github-pr-review demo](https://user-images.githubusercontent.com/3797062/73162963-4b8e2b00-4132-11ea-9a3f-f9c6f624c79f.png)
12-
![github-pr-check demo](https://user-images.githubusercontent.com/3797062/73163032-70829e00-4132-11ea-8481-f213a37db354.png)
3+
[![Test](https://github.com/reviewdog/action-ast-grep/workflows/Test/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3ATest)
4+
[![reviewdog](https://github.com/reviewdog/action-ast-grep/workflows/reviewdog/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3Areviewdog)
5+
[![depup](https://github.com/reviewdog/action-ast-grep/workflows/depup/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3Adepup)
6+
[![release](https://github.com/reviewdog/action-ast-grep/workflows/release/badge.svg)](https://github.com/reviewdog/action-ast-grep/actions?query=workflow%3Arelease)
7+
[![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)
8+
[![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)
139

14-
<!-- TODO: outline your action here -->
15-
This is a template repository for
16-
[reviewdog](https://github.com/reviewdog/reviewdog) action with release
17-
automation based on [action composition](https://docs.github.com/en/actions/creating-actions/creating-a-composite-action).
18-
Click `Use this template` button to create your reviewdog action :dog:!
10+
![demo1](https://github.com/user-attachments/assets/1c767bc0-43c4-4a60-ab97-b8b8e916ddc8)
11+
![demo2](https://github.com/user-attachments/assets/3c341c29-536c-4032-b5f4-f9ec06731dfe)
1912

20-
If you want to create your own reviewdog action from scratch without using this
21-
template, please check and copy release automation flow.
22-
It's important to manage release workflow and sync reviewdog version for all
23-
reviewdog actions.
13+
This is an action to run [ast-grep][ast-grep] with [reviewdog][reviewdog].
2414

25-
This repo contains a sample action to run [misspell](https://github.com/client9/misspell).
15+
[ast-grep]: https://github.com/ast-grep/ast-grep
16+
[reviewdog]: https://github.com/reviewdog/reviewdog
2617

2718
## Input
2819

29-
<!-- TODO: replace `<linter-name>` with yours -->
3020
```yaml
3121
inputs:
3222
github_token:
@@ -38,7 +28,7 @@ inputs:
3828
### Flags for reviewdog ###
3929
tool_name:
4030
description: 'Tool name to use for reviewdog reporter.'
41-
default: '<linter-name>'
31+
default: 'ast-grep'
4232
level:
4333
description: 'Report level for reviewdog [info,warning,error].'
4434
default: 'error'
@@ -58,26 +48,27 @@ inputs:
5848
reviewdog_flags:
5949
description: 'Additional reviewdog flags.'
6050
default: ''
61-
### Flags for <linter-name> ###
62-
locale:
63-
description: '-locale flag of misspell. (US/UK)'
51+
### Flags for ast-grep ###
52+
sg_version:
53+
description: 'ast-grep version.'
6454
default: ''
55+
sg_config:
56+
description: 'path to the ast-grep config file'
57+
default: 'sgconfig.yml'
6558
```
6659
6760
## Usage
68-
<!-- TODO: replace reviewdog/action-composite-template with your repo name -->
6961
7062
```yaml
7163
name: reviewdog
7264
on: [pull_request]
7365
jobs:
74-
# TODO: replace `linter_name` and `<linter-name>` with yours
75-
linter_name:
76-
name: runner / <linter-name>
66+
ast-grep:
67+
name: runner / ast-grep
7768
runs-on: ubuntu-latest
7869
steps:
7970
- uses: actions/checkout@v4
80-
- uses: reviewdog/action-composite-template@v1
71+
- uses: reviewdog/action-ast-grep@v1
8172
with:
8273
github_token: ${{ secrets.github_token }}
8374
# Change reviewdog reporter if you need [github-check,github-pr-review,github-pr-check].

action.yml

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# TODO: replace `<linter-name>` and `<your-name>` with yours
2-
name: 'Run <linter-name> with reviewdog'
3-
description: '🐶 Run <linter-name> with reviewdog on pull requests to improve code review experience.'
4-
author: '<your-name>'
1+
name: 'Run ast-grep with reviewdog'
2+
description: '🐶 Run ast-grep with reviewdog on pull requests to improve code review experience.'
3+
author: 'rinx'
54
inputs:
65
github_token:
76
description: 'GITHUB_TOKEN'
@@ -12,7 +11,7 @@ inputs:
1211
### Flags for reviewdog ###
1312
tool_name:
1413
description: 'Tool name to use for reviewdog reporter.'
15-
default: '<linter-name>'
14+
default: 'ast-grep'
1615
level:
1716
description: 'Report level for reviewdog [info,warning,error].'
1817
default: 'error'
@@ -35,10 +34,14 @@ inputs:
3534
output_dir:
3635
description: 'Output directory of reviewdog result. Useful for -reporter=sarif'
3736
default: '../reviewdog-results'
38-
### Flags for <linter-name> ###
39-
locale:
40-
description: '-locale flag of misspell. (US/UK)'
37+
### Flags for ast-grep ###
38+
sg_version:
39+
description: 'ast-grep version.'
4140
default: ''
41+
sg_config:
42+
description: 'path to the ast-grep config file'
43+
default: 'sgconfig.yml'
44+
4245
runs:
4346
using: 'composite'
4447
steps:
@@ -59,14 +62,15 @@ runs:
5962
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
6063
INPUT_REVIEWDOG_FLAGS: ${{ inputs.reviewdog_flags }}
6164
INPUT_OUTPUT_DIR: ${{ inputs.output_dir }}
62-
INPUT_LOCALE: ${{ inputs.locale }}
65+
INPUT_SG_VERSION: ${{ inputs.sg_version }}
66+
INPUT_SG_CONFIG: ${{ inputs.sg_config }}
67+
DEFAULT_SG_VERSION: 0.26.2
6368
- if: inputs.reporter == 'sarif'
6469
uses: github/codeql-action/upload-sarif@v3
6570
with:
6671
sarif_file: ${{ inputs.output_dir }}
6772

6873
# Ref: https://haya14busa.github.io/github-action-brandings/
69-
# TODO: update branding if you want.
7074
branding:
7175
icon: 'check'
7276
color: 'blue'

script.sh

+20-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ if [ -n "${GITHUB_WORKSPACE}" ]; then
55
cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
66
fi
77

8+
if [ -z "${INPUT_SG_VERSION}" ]; then
9+
INPUT_SG_VERSION="${DEFAULT_SG_VERSION}"
10+
fi
11+
812
mkdir -p "${INPUT_OUTPUT_DIR}"
913
OUTPUT_FILE_NAME="reviewdog-${INPUT_TOOL_NAME}"
1014
if [[ "${INPUT_REPORTER}" == "sarif" ]]; then
@@ -13,16 +17,27 @@ fi
1317

1418
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
1519

16-
echo '::group::🐶 Installing misspell ... https://github.com/client9/misspell'
20+
echo '::group::🐶 Installing ast-grep ... https://github.com/ast-grep/ast-grep'
1721
TEMP_PATH="$(mktemp -d)"
1822
PATH="${TEMP_PATH}:$PATH"
19-
wget -O - -q https://git.io/misspell | sh -s -- -b "${TEMP_PATH}"
23+
24+
curl --silent --show-error --fail \
25+
--location "https://github.com/ast-grep/ast-grep/releases/download/${INPUT_SG_VERSION}/app-x86_64-unknown-linux-gnu.zip" \
26+
--output "${TEMP_PATH}/sg.zip"
27+
28+
unzip -u "${TEMP_PATH}/sg.zip" -d "${TEMP_PATH}/temp-sg"
29+
install "${TEMP_PATH}/temp-sg/ast-grep" "${TEMP_PATH}"
30+
rm -rf "${TEMP_PATH}/sg.zip" "${TEMP_PATH}/temp-sg"
31+
2032
echo '::endgroup::'
2133

22-
echo '::group:: Running misspell with reviewdog 🐶 ...'
34+
echo '::group:: Running ast-grep with reviewdog 🐶 ...'
35+
2336
# shellcheck disable=SC2086
24-
misspell -locale="${INPUT_LOCALE}" . |
25-
reviewdog -efm="%f:%l:%c: %m" \
37+
ast-grep scan --config="${INPUT_SG_CONFIG}" --json=compact |
38+
jq -f "to-rdjsonl.jq" -c |
39+
reviewdog \
40+
-f=rdjsonl \
2641
-name="${INPUT_TOOL_NAME}" \
2742
-reporter="${INPUT_REPORTER}" \
2843
-filter-mode="${INPUT_FILTER_MODE}" \

sgconfig.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ruleDirs:
2+
- ./testdata/rules

testdata/main.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
_ = context.TODO()
10+
11+
fmt.Println("hello world")
12+
}
13+
14+
// Deprecated: deprecated function
15+
func New() {
16+
// FIXME: implement me
17+
fmt.Println("new function")
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json
2+
3+
id: avoid-use-deprecated-function
4+
message: Avoid to use deprecated function
5+
severity: warning # error, warning, info, hint
6+
language: Go
7+
rule:
8+
kind: comment
9+
regex: "//\\s*[dD]eprecated:.*"
10+
# utils: Extract repeated rule as local utility here.
11+
# note: Add detailed explanation for the rule.

testdata/rules/no-context-todo.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json
2+
3+
id: no-context-todo
4+
message: Do not use context.TODO in production code
5+
severity: error # error, warning, info, hint
6+
language: Go
7+
rule:
8+
pattern: context.TODO()
9+
# utils: Extract repeated rule as local utility here.
10+
note: context.TODO() is just a temporary thing
11+
fix: context.Background()

testdata/rules/no-fixme-comment.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json
2+
3+
id: no-fixme-comment
4+
message: Do not leave fixme comment
5+
severity: info
6+
language: Go
7+
rule:
8+
kind: comment
9+
regex: "//\\s*FIXME:.*"
10+
# utils: Extract repeated rule as local utility here.
11+
# note: Add detailed explanation for the rule.

testdata/subdir/text.md

-2
This file was deleted.

testdata/text.md

-5
This file was deleted.

to-rdjsonl.jq

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.[] |
2+
. as $item |
3+
{
4+
"message": (if .note then (.message + " (Note: " + .note + ")") else .message end),
5+
"severity": .severity | ascii_upcase,
6+
"location": {
7+
"path": .file,
8+
"range": {
9+
"start": {
10+
"line": (.range.start.line + 1),
11+
"column": (.range.start.column + 1)
12+
},
13+
"end": {
14+
"line": (.range.end.line + 1),
15+
"column": (.range.end.column + 1)
16+
}
17+
}
18+
},
19+
"code": {
20+
"value": .ruleId,
21+
"url": "https://github.com/ast-grep/ast-grep"
22+
},
23+
"source": {
24+
"name": "ast-grep",
25+
"url": "https://github.com/ast-grep/ast-grep"
26+
},
27+
"original_output": . | tostring
28+
} + if .replacement then
29+
{
30+
"suggestions": [
31+
{
32+
"range": {
33+
"start": {
34+
"line": (.range.start.line + 1),
35+
"column": (.range.start.column + 1)
36+
},
37+
"end": {
38+
"line": (.range.end.line + 1),
39+
"column": (.range.end.column + 1)
40+
}
41+
},
42+
"text": .replacement
43+
}
44+
]
45+
}
46+
else {} end

0 commit comments

Comments
 (0)