-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5499d30
commit a3b7587
Showing
7 changed files
with
296 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package luacheck | ||
|
||
import ( | ||
"bytes" | ||
"github.com/google/go-github/v57/github" | ||
"github.com/qiniu/x/xlog" | ||
"github.com/reviewbot/config" | ||
"github.com/reviewbot/internal/linters" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
var lintName = "luacheck" | ||
|
||
func init() { | ||
linters.RegisterCodeReviewHandler(lintName, luaCheckHandler) | ||
} | ||
|
||
func luaCheckHandler(log *xlog.Logger, linterConfig config.Linter, _ linters.Agent, _ github.PullRequestEvent) (map[string][]linters.LinterOutput, error) { | ||
executor, err := NewLuaCheckExecutor(linterConfig.WorkDir) | ||
if err != nil { | ||
log.Errorf("init luacheck executor failed: %v", err) | ||
return nil, err | ||
} | ||
|
||
if linters.IsEmpty(linterConfig.Args...) { | ||
linterConfig.Args = append([]string{}, ".") | ||
} | ||
|
||
output, err := executor.Run(log, linterConfig.Args...) | ||
if err != nil { | ||
log.Errorf("luacheck run failed: %v", err) | ||
return nil, err | ||
} | ||
|
||
parsedOutput, err := executor.Parse(log, output) | ||
if err != nil { | ||
log.Errorf("luacheck parse output failed: %v", err) | ||
return nil, err | ||
} | ||
|
||
return parsedOutput, nil | ||
} | ||
|
||
// luacheck is an executor that knows how to execute luacheck commands. | ||
type luacheck struct { | ||
// dir is the location of this repo. | ||
dir string | ||
// luacheck is the path to the luacheck binary. | ||
luacheck string | ||
} | ||
|
||
func NewLuaCheckExecutor(dir string) (linters.Linter, error) { | ||
g, err := exec.LookPath("luacheck") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &luacheck{ | ||
dir: dir, | ||
luacheck: g, | ||
}, nil | ||
} | ||
|
||
func (e *luacheck) Run(log *xlog.Logger, args ...string) ([]byte, error) { | ||
c := exec.Command(e.luacheck, args...) | ||
c.Dir = e.dir | ||
var out bytes.Buffer | ||
c.Stdout = &out | ||
c.Stderr = &out | ||
err := c.Run() | ||
if err != nil { | ||
log.Errorf("luacheck run with status: %v, mark and continue", err) | ||
} else { | ||
log.Infof("luacheck succeeded") | ||
} | ||
|
||
return out.Bytes(), nil | ||
} | ||
|
||
func (e *luacheck) Parse(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) { | ||
return linters.FormatLinterOutput(log, output, formatLuaCheckLine) | ||
} | ||
|
||
func formatLuaCheckLine(line string) (*linters.LinterOutput, error) { | ||
|
||
lineResult, err := linters.FormatLinterLine(line) | ||
if err != nil { | ||
return nil, err | ||
|
||
} | ||
return &linters.LinterOutput{ | ||
File: strings.TrimLeft(lineResult.File, " "), | ||
Line: lineResult.Line, | ||
Column: lineResult.Column, | ||
Message: strings.ReplaceAll(strings.ReplaceAll(lineResult.Message, "\x1b[0m\x1b[1m", ""), "\x1b[0m", ""), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright 2024 Qiniu Cloud (qiniu.com). | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package luacheck | ||
|
||
import ( | ||
"github.com/reviewbot/internal/linters" | ||
"testing" | ||
) | ||
|
||
func TestFormatLuaCheckLine(t *testing.T) { | ||
tc := []struct { | ||
input string | ||
expected *linters.LinterOutput | ||
}{ | ||
{" video/mp4/libs/mp4lib.lua:184:11: value assigned to variable mem_data is overwritten on line 202 before use", &linters.LinterOutput{ | ||
File: " video/mp4/libs/mp4lib.lua:184:11", | ||
Line: 184, | ||
Column: 11, | ||
Message: "value assigned to variable mem_data is overwritten on line 202 before use", | ||
}}, | ||
{" utils/jsonschema.lua:723:121: line is too long (142 > 120)", &linters.LinterOutput{ | ||
File: " utils/jsonschema.lua", | ||
Line: 723, | ||
Column: 121, | ||
Message: "line is too long (142 > 120)", | ||
}}, | ||
{" utils/httpc/http_simple.lua:24:1: setting read-only global variable _VERSION", &linters.LinterOutput{ | ||
File: " utils/httpc/http_simple.lua", | ||
Line: 24, | ||
Column: 1, | ||
Message: "setting read-only global variable _VERSION", | ||
}}, | ||
{" test/qtest_access.lua:1220:1: inconsistent indentation (SPACE followed by TAB)", &linters.LinterOutput{ | ||
File: " test/qtest_access.lua", | ||
Line: 1220, | ||
Column: 1, | ||
Message: "inconsistent indentation (SPACE followed by TAB)", | ||
}}, | ||
{"Checking test/qtest_mgrconf.lua", nil}, | ||
} | ||
|
||
for _, c := range tc { | ||
output, err := formatLuaCheckLine(c.input) | ||
if c.expected == nil && output != nil { | ||
t.Errorf("expected error, got: %v", output) | ||
} else { | ||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
if output == nil { | ||
if c.expected != nil { | ||
t.Errorf("expected: %v, got: %v", c.expected, output) | ||
} | ||
continue | ||
} | ||
|
||
if output.File != c.expected.File || output.Line != c.expected.Line || output.Column != c.expected.Column || output.Message != c.expected.Message { | ||
t.Errorf("expected: %v, got: %v", c.expected, output) | ||
} | ||
} | ||
} |
Oops, something went wrong.