33import java .util .List ;
44
55import neutra1 .linter .helper .LintContext ;
6+ import neutra1 .linter .models .enums .OutputFormat ;
67import neutra1 .linter .models .records .Violation ;
78
89import java .io .IOException ;
@@ -33,16 +34,16 @@ public void report(Violation violation) {
3334 violationList .add (violation );
3435 }
3536
36- public void outputDiagnostics (int disabledRuleCount , int disabledRelevantRuleCount , int totalRuleCount , boolean quietMode ) {
37- StringBuilder diagnosis = getDiagnosis ();
37+ public void outputDiagnostics (int disabledRuleCount , int disabledRelevantRuleCount , int totalRuleCount , boolean quietMode , OutputFormat outputFormat ) {
38+ StringBuilder diagnosis = getDiagnosis (outputFormat );
3839 System .out .println (diagnosis .toString ());
3940 if (!quietMode ){
4041 printInformation (totalRuleCount , disabledRelevantRuleCount , disabledRuleCount );
4142 }
4243 }
4344
44- public void outputDiagnostics (String outputFile , int disabledRuleCount , int disabledRelevantRuleCount , int totalRuleCount , boolean override , boolean quietMode ){
45- StringBuilder diagnosis = getDiagnosis ();
45+ public void outputDiagnostics (String outputFile , int disabledRuleCount , int disabledRelevantRuleCount , int totalRuleCount , boolean override , boolean quietMode , OutputFormat outputFormat ){
46+ StringBuilder diagnosis = getDiagnosis (outputFormat );
4647 Path currentDir = Paths .get (System .getProperty ("user.dir" ));
4748 Path outputPath = Paths .get (outputFile );
4849 if (!outputPath .isAbsolute ()){
@@ -60,27 +61,39 @@ public void outputDiagnostics(String outputFile, int disabledRuleCount, int disa
6061 System .out .println ("WARNING: writing to " + outputPath .toString () + " not successful." + "\n " +
6162 "Output defaults to stdout. You can add flag \" --override\" to overwrite it.\n " +
6263 "If this message still shows up after adding that flag, check path validity and/or write access.\n " );
63- outputDiagnostics (disabledRuleCount , disabledRelevantRuleCount , totalRuleCount , quietMode );
64+ outputDiagnostics (disabledRuleCount , disabledRelevantRuleCount , totalRuleCount , quietMode , outputFormat );
6465 }
6566 if (!quietMode ){
6667 printInformation (totalRuleCount , disabledRelevantRuleCount , disabledRuleCount );
6768 }
6869 }
6970
70- private StringBuilder getDiagnosis () {
71+ private StringBuilder getDiagnosis (OutputFormat outputFormat ) {
7172 StringBuilder diagnosis = new StringBuilder ();
7273 violationList .sort (Comparator .comparingInt (Violation ::lineNumber ));
7374 for (Violation v : violationList ) {
74- if (v .representativeMadr ().isEmpty ()){
75- diagnosis .append (LintContext .USER_PATH + ":" + v .lineNumber () + " " + "[" + v .ruleId () + "]" + " " + v .description () + "\n " );
75+ String file = v .representativeMadr ().orElse (LintContext .USER_PATH );
76+ if (outputFormat == OutputFormat .GITHUB_ACTIONS ){
77+ diagnosis .append ("::error file=" + escapeGithubProperty (file ) + ",line=" + v .lineNumber () +
78+ ",title=" + escapeGithubProperty (v .ruleId ()) + "::" + escapeGithubData (v .description ()) + "\n " );
7679 }
7780 else {
78- diagnosis .append (v . representativeMadr (). get () + ":" + v .lineNumber () + " " + "[" + v .ruleId () + "]" + " " + v .description () + "\n " );
81+ diagnosis .append (file + ":" + v .lineNumber () + " " + "[" + v .ruleId () + "]" + " " + v .description () + "\n " );
7982 }
8083 }
8184 return diagnosis ;
8285 }
8386
87+ // Escaping mandated by GitHub Actions workflow commands, see
88+ // https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands
89+ private String escapeGithubData (String value ){
90+ return value .replace ("%" , "%25" ).replace ("\r " , "%0D" ).replace ("\n " , "%0A" );
91+ }
92+
93+ private String escapeGithubProperty (String value ){
94+ return escapeGithubData (value ).replace (":" , "%3A" ).replace ("," , "%2C" );
95+ }
96+
8497 private void printInformation (int totalRuleCount , int disabledRelevantRuleCount , int disabledRuleCount ){
8598 StringBuilder info = new StringBuilder ();
8699 int violationCount = violationList .size ();
0 commit comments