Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Create customized logger to log warnings and errors on committe…
…d files As we are using StyleCop Anyalyzer to lint C# code, we want to show warnings and errors in the last committed files for every PR request. We create a customized logger "SALogger" to log warnings and errors in last committed files instead of all files in the whole project. After we get the logs, we send it back to PR as comments. Meanwhile, we added a RingloggerParser to parse log file (log.bin) to txt format which can be readable.
- Loading branch information
1 parent
0e070a5
commit 2de39d2
Showing
13 changed files
with
577 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,9 @@ wireguard.server.base64.config | |
|
||
#vim | ||
*.swp | ||
|
||
#logger | ||
test/logger/packages | ||
|
||
#comment | ||
test/comment/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const bot = require("circle-github-bot").create(); | ||
const fs = require('fs'); | ||
|
||
const path = '../result/Output.log' | ||
fs.readFile(path, (err, data) => { | ||
if (err) { | ||
console.log(`Fail to read file: ${path}, err: ${err.message}`) | ||
} | ||
const regex = /(?<errors>\d*) Error\(s\), (?<warnings>\d*) Warning\(s\)/gm; | ||
const { errors, warnings } = regex.exec(data.toString()).groups | ||
bot.comment(process.env.GH_AUTH_TOKEN, `<h3>Errors: ${errors}, Warnings: ${warnings}</h3> | ||
Details: <strong>${bot.artifactLink('test/result/Output.log', 'details')}</strong> | ||
`); | ||
}) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "comment", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "comment.js", | ||
"dependencies": { | ||
"circle-github-bot": "^2.0.1" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.IO; | ||
using System.Management.Automation; | ||
using System.Security; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace SALogger | ||
{ | ||
// This logger will derive from the Microsoft.Build.Utilities.Logger class. | ||
public class SALogger : Logger | ||
{ | ||
/// <summary> | ||
/// Initialize is guaranteed to be called by MSBuild at the start of the build | ||
/// before any events are raised. | ||
/// </summary> | ||
public override void Initialize(IEventSource eventSource) | ||
{ | ||
// The name of the log file should be passed as the first item in the | ||
// "parameters" specification in the /logger switch. It is required | ||
// to pass a log file to this logger. Other loggers may have zero or more than | ||
// one parameters. | ||
if (null == Parameters) | ||
{ | ||
throw new LoggerException("Log file was not set."); | ||
} | ||
string[] parameters = Parameters.Split(';'); | ||
|
||
string logFile = parameters[0]; | ||
if (String.IsNullOrEmpty(logFile)) | ||
{ | ||
throw new LoggerException("Log file was not set."); | ||
} | ||
|
||
if (parameters.Length > 1) | ||
{ | ||
throw new LoggerException("Too many parameters passed."); | ||
} | ||
|
||
try | ||
{ | ||
// Open the file | ||
this.streamWriter = new StreamWriter(logFile); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if | ||
( | ||
ex is UnauthorizedAccessException | ||
|| ex is ArgumentNullException | ||
|| ex is PathTooLongException | ||
|| ex is DirectoryNotFoundException | ||
|| ex is NotSupportedException | ||
|| ex is ArgumentException | ||
|| ex is SecurityException | ||
|| ex is IOException | ||
) | ||
{ | ||
throw new LoggerException("Failed to create log file: " + ex.Message); | ||
} | ||
else | ||
{ | ||
// Unexpected failure | ||
throw; | ||
} | ||
} | ||
// Get the files that had changes in the last commit | ||
GetLastCommitChanges(); | ||
// For brevity, we'll only register for certain event types. Loggers can also | ||
// register to handle TargetStarted/Finished and other events. | ||
eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised); | ||
eventSource.ErrorRaised += new BuildErrorEventHandler(eventSource_ErrorRaised); | ||
} | ||
|
||
void eventSource_ErrorRaised(object sender, BuildErrorEventArgs e) | ||
{ | ||
// BuildErrorEventArgs adds LineNumber, ColumnNumber, File, amongst other parameters | ||
this.numOfErrors++; | ||
string line = String.Format(": ERROR {0}({1},{2}): ", e.File, e.LineNumber, e.ColumnNumber); | ||
WriteLineWithSenderAndMessage(line, e); | ||
} | ||
|
||
void eventSource_WarningRaised(object sender, BuildWarningEventArgs e) | ||
{ | ||
foreach(PSObject file in this.results) | ||
{ | ||
string lastCommitFile = file.ToString().Replace("/", @"\"); | ||
|
||
if(lastCommitFile.Contains(e.File)) | ||
{ | ||
// BuildWarningEventArgs adds LineNumber, ColumnNumber, File, amongst other parameters | ||
this.numOfWarnings++; | ||
string line = String.Format(": Warning {0}({1},{2}): ", e.File, e.LineNumber, e.ColumnNumber); | ||
WriteLineWithSenderAndMessage(line, e); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Write a line to the log, adding the SenderName and Message | ||
/// (these parameters are on all MSBuild event argument objects) | ||
/// </summary> | ||
private void WriteLineWithSenderAndMessage(string line, BuildEventArgs e) | ||
{ | ||
WriteLine(e.SenderName + ": " + line, e); | ||
} | ||
|
||
/// <summary> | ||
/// Just write a line to the log | ||
/// </summary> | ||
private void WriteLine(string line, BuildEventArgs e) | ||
{ | ||
streamWriter.WriteLine(line + e.Message); | ||
} | ||
|
||
/// <summary> | ||
/// Get a list of changed files' names from last commit. | ||
/// </summary> | ||
private void GetLastCommitChanges() | ||
{ | ||
using (PowerShell powerShell = PowerShell.Create()) | ||
{ | ||
powerShell.AddScript("cd .."); | ||
powerShell.AddScript(@"git diff-tree --no-commit-id --name-only -r HEAD"); | ||
results = powerShell.Invoke(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Shutdown() is guaranteed to be called by MSBuild at the end of the build, after all | ||
/// events have been raised. | ||
/// </summary> | ||
public override void Shutdown() | ||
{ | ||
// Done logging, let go of the file | ||
streamWriter.WriteLine(this.numOfErrors + " Error(s), " + this.numOfWarnings + " Warning(s)"); | ||
streamWriter.Close(); | ||
} | ||
|
||
private StreamWriter streamWriter; | ||
private Collection<PSObject> results; | ||
private int numOfWarnings = 0; | ||
private int numOfErrors = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Microsoft.Build.Utilities.Core" version="16.3.0" targetFramework="net472" /> | ||
<package id="Microsoft.Build.Framework" version="16.3.0" targetFramework="net472" /> | ||
<package id="Microsoft.PowerShell.5.ReferenceAssemblies" version="1.1.0"/> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/> | ||
</startup> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("RingloggerParser")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("RingloggerParser")] | ||
[assembly: AssemblyCopyright("Copyright © 2019")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("f12db613-6ee9-46f0-aeee-d8aa701c5acd")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
Oops, something went wrong.
2de39d2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Errors: 0, Warnings: 0
Details: details