From 51218f583983df09d9246fdee7e7c703c75c4a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Kl=C3=A4rner?= Date: Fri, 16 Aug 2024 20:53:35 +0200 Subject: [PATCH] feat(output): support output format github-actions (closes #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 - https://github.com/actions/toolkit/issues/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) --- cmd/editorconfig-checker/main.go | 4 ++-- pkg/error/error.go | 20 +++++++++++++++++++- pkg/error/error_test.go | 3 +++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/editorconfig-checker/main.go b/cmd/editorconfig-checker/main.go index 72961fd7..ad4eb3ac 100644 --- a/cmd/editorconfig-checker/main.go +++ b/cmd/editorconfig-checker/main.go @@ -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") diff --git a/pkg/error/error.go b/pkg/error/error.go index be62878b..d1e7f8a7 100644 --- a/pkg/error/error.go +++ b/pkg/error/error.go @@ -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 { @@ -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)) diff --git a/pkg/error/error_test.go b/pkg/error/error_test.go index 79e38d5a..d5b254fa 100644 --- a/pkg/error/error_test.go +++ b/pkg/error/error_test.go @@ -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) }