From 2e646542c4f21d64f493ef1e1b893a4489a2ecf0 Mon Sep 17 00:00:00 2001 From: Wes Haggard Date: Thu, 26 Sep 2024 09:24:45 -0700 Subject: [PATCH] Improve DevOps logging for code owners linter (#9040) * Improve DevOps logging for code owners linter * Fix missing brace * Update Program.cs * Update Program.cs * Update Program.cs Add a comment as to why we had to replace the newline with an encoded newline. --------- Co-authored-by: James Suplizio --- .../Program.cs | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tools/codeowners-utils/Azure.Sdk.Tools.CodeownersLinter/Program.cs b/tools/codeowners-utils/Azure.Sdk.Tools.CodeownersLinter/Program.cs index 55967bc9638..7a7cd10f86d 100644 --- a/tools/codeowners-utils/Azure.Sdk.Tools.CodeownersLinter/Program.cs +++ b/tools/codeowners-utils/Azure.Sdk.Tools.CodeownersLinter/Program.cs @@ -269,13 +269,23 @@ static int LintCodeownersFile(string teamUserBlobStorageUri, errors = baselineUtils.FilterErrorsUsingBaseline(errors); } } - + bool loggingInDevOps = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECTID")); int returnCode = 0; // If there are errors, and this isn't a baseline generation, ensure the returnCode is non-zero and output the errors. if ((errors.Count > 0) && !generateBaseline) { returnCode = 1; + // DevOps only adds the first 4 errors to the github checks list so lets always add the generic one first and then as many of the individual ones as can be found afterwards + if (loggingInDevOps) + { + Console.WriteLine($"##vso[task.logissue type=error;]There are linter errors. Please visit {linterErrorsHelpLink} for guidance on how to handle them."); + } + else + { + Console.WriteLine($"There are linter errors. Please visit {linterErrorsHelpLink} for guidance on how to handle them."); + } + // Output the errors sorted ascending by line number and by type. If there's a block // error with the same starting line number as a single line error, the block error // should be output first. @@ -283,10 +293,16 @@ static int LintCodeownersFile(string teamUserBlobStorageUri, foreach (var error in errorsByLineAndType) { - Console.WriteLine(error + Environment.NewLine); + if (loggingInDevOps) + { + // Environment.NewLine needs to be replaced by an encoded NewLine "%0D%0A" in order to display on GitHub and DevOps checks + Console.WriteLine($"##vso[task.logissue type=error;sourcepath={codeownersFileFullPath};linenumber={error.LineNumber};columnnumber=1;]{error.ToString().Replace(Environment.NewLine,"%0D%0A")}"); + } + else + { + Console.WriteLine(error + Environment.NewLine); + } } - - Console.WriteLine($"There were linter errors. Please visit {linterErrorsHelpLink} for guidance on how to handle them."); } return returnCode; }