Skip to content

Commit

Permalink
Merge pull request #335 from carstencodes/contrib/carstencodes/sarif-…
Browse files Browse the repository at this point in the history
…levels

feat: Configurable levels
  • Loading branch information
yoheimuta authored Jun 13, 2023
2 parents f8f2182 + b3a1368 commit 4dd3e2d
Show file tree
Hide file tree
Showing 103 changed files with 1,320 additions and 544 deletions.
1 change: 1 addition & 0 deletions .github/install_dep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ go get -u github.com/gordonklaus/ineffassign
go get -u github.com/opennota/check/cmd/varcheck
go get -u github.com/opennota/check/cmd/aligncheck
go get -u github.com/mdempsky/unconvert
go get -u github.com/chavacava/garif
11 changes: 9 additions & 2 deletions _example/plugin/customrules/enumNamesLowerSnakeCaseRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"github.com/yoheimuta/go-protoparser/v4/parser"

"github.com/yoheimuta/protolint/linter/report"
"github.com/yoheimuta/protolint/linter/rule"
"github.com/yoheimuta/protolint/linter/strs"
"github.com/yoheimuta/protolint/linter/visitor"
)

// EnumNamesLowerSnakeCaseRule verifies that all enum names are LowerSnakeCase.
type EnumNamesLowerSnakeCaseRule struct{}
type EnumNamesLowerSnakeCaseRule struct {
}

// NewEnumNamesLowerSnakeCaseRule creates a new EnumNamesLowerSnakeCaseRule.
func NewEnumNamesLowerSnakeCaseRule() EnumNamesLowerSnakeCaseRule {
Expand All @@ -31,10 +33,15 @@ func (r EnumNamesLowerSnakeCaseRule) IsOfficial() bool {
return true
}

// Severity gets the severity of the rule
func (r EnumNamesLowerSnakeCaseRule) Severity() rule.Severity {
return rule.SeverityWarning
}

// Apply applies the rule to the proto.
func (r EnumNamesLowerSnakeCaseRule) Apply(proto *parser.Proto) ([]report.Failure, error) {
v := &enumNamesLowerSnakeCaseVisitor{
BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID()),
BaseAddVisitor: visitor.NewBaseAddVisitor(r.ID(), string(r.Severity())),
}
return visitor.RunVisitor(v, proto, r.ID())
}
Expand Down
17 changes: 13 additions & 4 deletions _example/plugin/customrules/simpleRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import (
"github.com/yoheimuta/go-protoparser/v4/parser/meta"

"github.com/yoheimuta/protolint/linter/report"
"github.com/yoheimuta/protolint/linter/rule"
)

// SimpleRule verifies that all enum names are LowerSnakeCase.
type SimpleRule struct {
verbose bool
fixMode bool
verbose bool
fixMode bool
severity rule.Severity
}

// NewSimpleRule creates a new SimpleRule.
func NewSimpleRule(
verbose bool,
fixMode bool,
severity rule.Severity,
) SimpleRule {
return SimpleRule{
verbose: verbose,
fixMode: fixMode,
verbose: verbose,
fixMode: fixMode,
severity: severity,
}
}

Expand All @@ -39,6 +43,11 @@ func (r SimpleRule) IsOfficial() bool {
return true
}

// Severity gets the rule severity
func (r SimpleRule) Severity() rule.Severity {
return r.severity
}

// Apply applies the rule to the proto.
func (r SimpleRule) Apply(proto *parser.Proto) ([]report.Failure, error) {
return []report.Failure{
Expand Down
4 changes: 2 additions & 2 deletions _example/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

plugin.RegisterCustomRules(
// The purpose of this line just illustrates that you can implement the same as internal linter rules.
rules.NewEnumsHaveCommentRule(*goStyle),
rules.NewEnumsHaveCommentRule(rule.SeverityWarning, *goStyle),

// A common custom rule example. It's simple.
customrules.NewEnumNamesLowerSnakeCaseRule(),
Expand All @@ -28,7 +28,7 @@ func main() {
verbose bool,
fixMode bool,
) rule.Rule {
return customrules.NewSimpleRule(verbose, fixMode)
return customrules.NewSimpleRule(verbose, fixMode, rule.SeverityError)
}),
)
}
11 changes: 11 additions & 0 deletions _proto/plugin.proto
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
syntax = "proto3";
package proto;

option go_package =
"github.com/yoheimuta/protolint/internal/addon/plugin/proto";

service RuleSetService {
rpc ListRules(ListRulesRequest) returns (ListRulesResponse);
rpc Apply(ApplyRequest) returns (ApplyResponse);
}

enum RuleSeverity {
RULE_SEVERITY_UNSPECIFIED = 0;
RULE_SEVERITY_NOTE = 1;
RULE_SEVERITY_WARNING = 2;
RULE_SEVERITY_ERROR = 3;
}

message ListRulesRequest {
bool verbose = 1;
bool fix_mode = 2;
Expand All @@ -15,6 +25,7 @@ message ListRulesResponse {
message Rule {
string id = 1;
string purpose = 2;
RuleSeverity severity = 3;
}
repeated Rule rules = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
)

require (
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 // indirect
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 h1:W9o46d2kbNL06lq7UNDPV0zYLzkrde/bjIqO02eoll0=
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8/go.mod h1:gakxgyXaaPkxvLw1XQxNGK4I37ys9iBRzNUx/B7pUCo=
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab h1:5JxePczlyGAtj6R1MUEFZ/UFud6FfsOejq7xLC2ZIb0=
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -91,6 +93,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yoheimuta/go-protoparser/v4 v4.7.0 h1:80LGfVM25sCoNDD08hv9O0ShQMjoTrIE76j5ON+gq3U=
github.com/yoheimuta/go-protoparser/v4 v4.7.0/go.mod h1:AHNNnSWnb0UoL4QgHPiOAg2BniQceFscPI5X/BZNHl8=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
Expand Down
25 changes: 17 additions & 8 deletions internal/addon/plugin/externalRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ import (

"github.com/yoheimuta/protolint/internal/addon/plugin/proto"
"github.com/yoheimuta/protolint/linter/report"
"github.com/yoheimuta/protolint/linter/rule"
)

// externalRule represents a customized rule that works as a plugin.
type externalRule struct {
id string
purpose string
client shared.RuleSet
id string
purpose string
client shared.RuleSet
severity rule.Severity
}

func newExternalRule(
id string,
purpose string,
client shared.RuleSet,
severity rule.Severity,
) externalRule {
return externalRule{
id: id,
purpose: purpose,
client: client,
id: id,
purpose: purpose,
client: client,
severity: severity,
}
}

Expand All @@ -46,6 +50,11 @@ func (r externalRule) IsOfficial() bool {
return true
}

// Severity returns the severity of a rule (note, warning, error)
func (r externalRule) Severity() rule.Severity {
return r.severity
}

// Apply applies the rule to the proto.
func (r externalRule) Apply(p *parser.Proto) ([]report.Failure, error) {
relPath := p.Meta.Filename
Expand All @@ -64,12 +73,12 @@ func (r externalRule) Apply(p *parser.Proto) ([]report.Failure, error) {

var fs []report.Failure
for _, f := range resp.Failures {
fs = append(fs, report.Failuref(meta.Position{
fs = append(fs, report.FailureWithSeverityf(meta.Position{
Filename: relPath,
Offset: int(f.Pos.Offset),
Line: int(f.Pos.Line),
Column: int(f.Pos.Column),
}, r.id, f.Message))
}, r.id, string(r.severity), f.Message))
}
return fs, nil
}
16 changes: 15 additions & 1 deletion internal/addon/plugin/externalRuleProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,22 @@ func GetExternalRules(
}

for _, r := range resp.Rules {
rs = append(rs, newExternalRule(r.Id, r.Purpose, client))
severity := getSeverity(r.Severity)
rs = append(rs, newExternalRule(r.Id, r.Purpose, client, severity))
}
}
return rs, nil
}

func getSeverity(ruleSeverity proto.RuleSeverity) rule.Severity {
switch ruleSeverity {
case proto.RuleSeverity_RULE_SEVERITY_ERROR:
return rule.SeverityError
case proto.RuleSeverity_RULE_SEVERITY_WARNING:
return rule.SeverityWarning
case proto.RuleSeverity_RULE_SEVERITY_NOTE:
return rule.SeverityNote
}

return rule.SeverityError
}
Loading

0 comments on commit 4dd3e2d

Please sign in to comment.