feat(parser): unify parsers conf (#74)
Reviewed-on: #74 Co-authored-by: Boming Zhang <bomingzh@sjtu.edu.cn> Co-committed-by: Boming Zhang <bomingzh@sjtu.edu.cn>
This commit is contained in:
parent
cc3b4b0b13
commit
a29fccbfa4
|
@ -14,11 +14,12 @@ type Match struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Conf struct {
|
type Conf struct {
|
||||||
Score int
|
Score int
|
||||||
RootDir string `default:"/w"`
|
RootDir string `default:"/w"`
|
||||||
Matches []Match
|
Matches []Match
|
||||||
Stdout string `default:"stdout"`
|
Stdout string `default:"stdout"`
|
||||||
Stderr string `default:"stderr"`
|
Stderr string `default:"stderr"`
|
||||||
|
ForceQuitOnDeduct bool `default:"false"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClangTidy struct{}
|
type ClangTidy struct{}
|
||||||
|
@ -56,8 +57,13 @@ func (*ClangTidy) Run(results []stage.ExecutorResult, confAny any) (
|
||||||
return nil, true, err
|
return nil, true, err
|
||||||
}
|
}
|
||||||
var res []stage.ParserResult
|
var res []stage.ParserResult
|
||||||
|
forceQuit := false
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
res = append(res, Parse(result, *conf))
|
parseRes := Parse(result, *conf)
|
||||||
|
if conf.ForceQuitOnDeduct && parseRes.Score < conf.Score {
|
||||||
|
forceQuit = true
|
||||||
|
}
|
||||||
|
res = append(res, parseRes)
|
||||||
}
|
}
|
||||||
return res, false, nil
|
return res, forceQuit, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,18 +9,18 @@ import (
|
||||||
"github.com/joint-online-judge/JOJ3/internal/stage"
|
"github.com/joint-online-judge/JOJ3/internal/stage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CppCheck struct{}
|
|
||||||
|
|
||||||
type Match struct {
|
type Match struct {
|
||||||
Severity []string
|
Keywords []string
|
||||||
|
Severity []string // TODO: remove me
|
||||||
Score int
|
Score int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Conf struct {
|
type Conf struct {
|
||||||
Score int
|
Score int
|
||||||
Matches []Match
|
Matches []Match
|
||||||
Stdout string `default:"stdout"`
|
Stdout string `default:"stdout"`
|
||||||
Stderr string `default:"stderr"`
|
Stderr string `default:"stderr"`
|
||||||
|
ForceQuitOnDeduct bool `default:"false"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Record struct {
|
type Record struct {
|
||||||
|
@ -32,6 +32,8 @@ type Record struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CppCheck struct{}
|
||||||
|
|
||||||
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
|
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
|
||||||
// stdout := executorResult.Files[conf.Stdout]
|
// stdout := executorResult.Files[conf.Stdout]
|
||||||
stderr := executorResult.Files[conf.Stderr]
|
stderr := executorResult.Files[conf.Stderr]
|
||||||
|
@ -89,8 +91,13 @@ func (*CppCheck) Run(results []stage.ExecutorResult, confAny any) (
|
||||||
return nil, true, err
|
return nil, true, err
|
||||||
}
|
}
|
||||||
var res []stage.ParserResult
|
var res []stage.ParserResult
|
||||||
|
forceQuit := false
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
res = append(res, Parse(result, *conf))
|
parseRes := Parse(result, *conf)
|
||||||
|
if conf.ForceQuitOnDeduct && parseRes.Score < conf.Score {
|
||||||
|
forceQuit = true
|
||||||
|
}
|
||||||
|
res = append(res, parseRes)
|
||||||
}
|
}
|
||||||
return res, false, nil
|
return res, forceQuit, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cppcheck
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Severity int
|
type Severity int
|
||||||
|
@ -42,28 +43,38 @@ func severityFromString(severityString string) (Severity, error) {
|
||||||
func GetResult(records []Record, conf Conf) (string, int, error) {
|
func GetResult(records []Record, conf Conf) (string, int, error) {
|
||||||
result := "### Test results summary\n\n"
|
result := "### Test results summary\n\n"
|
||||||
var severityCounts [UNKNOWN + 1]int
|
var severityCounts [UNKNOWN + 1]int
|
||||||
var severityScore [UNKNOWN + 1]int
|
|
||||||
score := conf.Score
|
score := conf.Score
|
||||||
|
// TODO: remove me
|
||||||
for _, match := range conf.Matches {
|
if len(conf.Matches) == 0 {
|
||||||
severities := match.Severity
|
var severityScore [UNKNOWN + 1]int
|
||||||
score := match.Score
|
for _, match := range conf.Matches {
|
||||||
for _, severityString := range severities {
|
severities := match.Severity
|
||||||
severity, err := severityFromString(severityString)
|
score := match.Score
|
||||||
if err != nil {
|
for _, severityString := range severities {
|
||||||
return "", 0, err
|
severity, err := severityFromString(severityString)
|
||||||
|
if err != nil {
|
||||||
|
return "", 0, err
|
||||||
|
}
|
||||||
|
severityScore[int(severity)] = score
|
||||||
}
|
}
|
||||||
severityScore[int(severity)] = score
|
}
|
||||||
|
for _, record := range records {
|
||||||
|
severity, err := severityFromString(record.Severity)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("parse severity", "error", err)
|
||||||
|
}
|
||||||
|
severityCounts[int(severity)] += 1
|
||||||
|
score -= severityScore[int(severity)]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
severity, err := severityFromString(record.Severity)
|
for _, match := range conf.Matches {
|
||||||
if err != nil {
|
for _, keyword := range match.Keywords {
|
||||||
slog.Error("parse severity", "error", err)
|
if strings.Contains(record.Id, keyword) {
|
||||||
|
score -= match.Score
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
severityCounts[int(severity)] += 1
|
|
||||||
score -= severityScore[int(severity)]
|
|
||||||
}
|
}
|
||||||
result += fmt.Sprintf("1. error: %d\n", severityCounts[0])
|
result += fmt.Sprintf("1. error: %d\n", severityCounts[0])
|
||||||
result += fmt.Sprintf("2. warning: %d\n", severityCounts[1])
|
result += fmt.Sprintf("2. warning: %d\n", severityCounts[1])
|
||||||
|
|
|
@ -11,18 +11,27 @@ import (
|
||||||
"github.com/joint-online-judge/JOJ3/pkg/utils"
|
"github.com/joint-online-judge/JOJ3/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Match struct {
|
||||||
|
Keywords []string
|
||||||
|
Score int
|
||||||
|
}
|
||||||
|
|
||||||
type Conf struct {
|
type Conf struct {
|
||||||
Score int
|
Score int
|
||||||
|
Matches []Match
|
||||||
|
Stdout string `default:"stdout"`
|
||||||
|
Stderr string `default:"stderr"`
|
||||||
|
ForceQuitOnDeduct bool `default:"false"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Cpplint struct{}
|
type Cpplint struct{}
|
||||||
|
|
||||||
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
|
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
|
||||||
stderr := executorResult.Files["stderr"]
|
stderr := executorResult.Files[conf.Stderr]
|
||||||
pattern := `(.+):(\d+): (.+) \[(.+)\] \[(\d)]\n`
|
pattern := `(.+):(\d+): (.+) \[(.+)\] \[(\d)]\n`
|
||||||
re := regexp.MustCompile(pattern)
|
re := regexp.MustCompile(pattern)
|
||||||
matches := re.FindAllStringSubmatch(stderr, -1)
|
matches := re.FindAllStringSubmatch(stderr, -1)
|
||||||
score := 0
|
score := conf.Score
|
||||||
comment := "### Test results summary\n\n"
|
comment := "### Test results summary\n\n"
|
||||||
categoryCount := map[string]int{}
|
categoryCount := map[string]int{}
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
|
@ -37,15 +46,25 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
|
||||||
// }
|
// }
|
||||||
// message := match[3]
|
// message := match[3]
|
||||||
category := match[4]
|
category := match[4]
|
||||||
confidence, err := strconv.Atoi(match[5])
|
// TODO: remove me
|
||||||
if err != nil {
|
if len(conf.Matches) == 0 {
|
||||||
slog.Error("parse confidence", "error", err)
|
confidence, err := strconv.Atoi(match[5])
|
||||||
return stage.ParserResult{
|
if err != nil {
|
||||||
Score: 0,
|
slog.Error("parse confidence", "error", err)
|
||||||
Comment: fmt.Sprintf("Unexpected parser error: %s.", err),
|
return stage.ParserResult{
|
||||||
|
Score: 0,
|
||||||
|
Comment: fmt.Sprintf("Unexpected parser error: %s.", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
score -= confidence
|
||||||
|
}
|
||||||
|
for _, match := range conf.Matches {
|
||||||
|
for _, keyword := range match.Keywords {
|
||||||
|
if strings.Contains(category, keyword) {
|
||||||
|
score -= match.Score
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
score -= confidence
|
|
||||||
parts := strings.Split(category, "/")
|
parts := strings.Split(category, "/")
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
category := parts[0]
|
category := parts[0]
|
||||||
|
@ -76,8 +95,13 @@ func (*Cpplint) Run(results []stage.ExecutorResult, confAny any) (
|
||||||
return nil, true, err
|
return nil, true, err
|
||||||
}
|
}
|
||||||
var res []stage.ParserResult
|
var res []stage.ParserResult
|
||||||
|
forceQuit := false
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
res = append(res, Parse(result, *conf))
|
parseRes := Parse(result, *conf)
|
||||||
|
if conf.ForceQuitOnDeduct && parseRes.Score < conf.Score {
|
||||||
|
forceQuit = true
|
||||||
|
}
|
||||||
|
res = append(res, parseRes)
|
||||||
}
|
}
|
||||||
return res, false, nil
|
return res, forceQuit, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user