Skip to content

Commit

Permalink
feat(output): support output format github-actions (closes editorconf…
Browse files Browse the repository at this point in the history
…ig-checker#317)

Now one can request `--format github-actions` and get the errors
returned in a format that should be parsable by Github Actions.

I decided against implementing the grouping feature (so errors would be
additionally grouped per file), since nested groups are not yet
supported - actions/toolkit#1001
Doing so would then break the use cases where a linter collection
like mega-linter or super-linter use groups themselves (e.g. to group
errors by linter)
  • Loading branch information
klaernie committed Aug 16, 2024
1 parent 1674117 commit ee8085b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/editorconfig-checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func init() {
flag.BoolVar(&c.ShowVersion, "version", false, "print the version number")
flag.BoolVar(&c.Help, "help", false, "print the help")
flag.BoolVar(&c.Help, "h", false, "print the help")
flag.StringVar(&c.Format, "format", "default", "specify the output format: default, gcc")
flag.StringVar(&c.Format, "f", "default", "specify the output format: default, gcc")
flag.StringVar(&c.Format, "format", "default", "specify the output format: default, gcc, github-actions")
flag.StringVar(&c.Format, "f", "default", "specify the output format: default, gcc, github-actions")
flag.BoolVar(&c.Verbose, "verbose", false, "print debugging information")
flag.BoolVar(&c.Verbose, "v", false, "print debugging information")
flag.BoolVar(&c.Debug, "debug", false, "print debugging information")
Expand Down
20 changes: 19 additions & 1 deletion pkg/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func PrintErrors(errors []ValidationErrors, config config.Config) {
continue
}

// for these formats the errors need to be consolidated first.
if config.Format == "default" || config.Format == "github-actions" {
fileErrors.Errors = ConsolidateErrors(fileErrors.Errors, config)
}

if config.Format == "gcc" {
// gcc: A format mimicking the error format from GCC.
for _, singleError := range fileErrors.Errors {
Expand All @@ -90,10 +95,23 @@ func PrintErrors(errors []ValidationErrors, config config.Config) {
}
config.Logger.Error(fmt.Sprintf("%s:%d:%d: %s: %s", relativeFilePath, lineNo, 0, "error", singleError.Message))
}
} else if config.Format == "github-actions" {
// github-actions: A format dedicated for usage in Github Actions
for _, singleError := range fileErrors.Errors {
if singleError.LineNumber != -1 {
if singleError.ConsecuitiveCount != 0 {
config.Logger.Error(fmt.Sprintf("::error file=%s,line=%d,endLine=%d::%s", relativeFilePath, singleError.LineNumber, singleError.LineNumber+singleError.ConsecuitiveCount, singleError.Message))
} else {
config.Logger.Error(fmt.Sprintf("::error file=%s,line=%d::%s", relativeFilePath, singleError.LineNumber, singleError.Message))
}
} else {
config.Logger.Error(fmt.Sprintf("::error file=%s::%s", relativeFilePath, singleError.Message))
}
}
} else {
// default: A human readable text format.
config.Logger.Warning(fmt.Sprintf("%s:", relativeFilePath))
for _, singleError := range ConsolidateErrors(fileErrors.Errors, config) {
for _, singleError := range fileErrors.Errors {
if singleError.LineNumber != -1 {
if singleError.ConsecuitiveCount != 0 {
config.Logger.Error(fmt.Sprintf("\t%d-%d: %s", singleError.LineNumber, singleError.LineNumber+singleError.ConsecuitiveCount, singleError.Message))
Expand Down
3 changes: 3 additions & 0 deletions pkg/error/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,7 @@ func TestPrintErrors(t *testing.T) {

config2 := config.Config{Format: "gcc"}
PrintErrors(input, config2)

config3 := config.Config{Format: "github-actions"}
PrintErrors(input, config3)
}

0 comments on commit ee8085b

Please sign in to comment.