public void Should_TestConsoleApp1_Warning_ConfigureAs_Warning()
        {
            var checkRunConfiguration = new CheckRunConfiguration()
            {
                Rules = new LogAnalyzerRule[]
                {
                    new LogAnalyzerRule()
                    {
                        Code     = "CS0219",
                        ReportAs = ReportAs.Warning
                    }
                }
            };

            var cloneRoot = @"C:\projects\testconsoleapp1\";
            var logData   = ProcessLog("testconsoleapp1-1warning.binlog", cloneRoot, Faker.Internet.UserName(), Faker.Random.Word(), Faker.Random.String(10), checkRunConfiguration);

            logData.ErrorCount.Should().Be(0);
            logData.WarningCount.Should().Be(1);
            logData.Annotations.Should().AllBeEquivalentTo(
                new Annotation(
                    "TestConsoleApp1/Program.cs",
                    13,
                    13,
                    AnnotationLevel.Warning,
                    "The variable 'hello' is assigned but its value is never used")
            {
                Title = "CS0219: TestConsoleApp1/Program.cs(13)"
            }
                );

            logData.Report.Should().NotBeNullOrWhiteSpace();
        }
        private CheckRunConfiguration LoadCheckRunConfiguration(IFileSystem fileSystem, string configurationFile)
        {
            CheckRunConfiguration configuration = null;

            if (configurationFile != null)
            {
                if (!fileSystem.File.Exists(configurationFile))
                {
                    throw new InvalidOperationException($"Configuration file `{configurationFile}` does not exist.");
                }

                var configurationString = fileSystem.File.ReadAllText(configurationFile);
                if (string.IsNullOrWhiteSpace(configurationString))
                {
                    throw new InvalidOperationException(
                              $"Content of configuration file `{configurationFile}` is null or empty.");
                }

                configuration = JsonConvert.DeserializeObject <CheckRunConfiguration>(configurationString,
                                                                                      new JsonSerializerSettings
                {
                    Formatting       = Formatting.None,
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Converters       = new List <JsonConverter>
                    {
                        new StringEnumConverter {
                            NamingStrategy = new CamelCaseNamingStrategy()
                        }
                    },
                    MissingMemberHandling = MissingMemberHandling.Error
                });
            }

            return(configuration);
        }
        public LogDataBuilder(Parameters parameters, CheckRunConfiguration configuration)
        {
            _parameters     = parameters;
            _ruleDictionary =
                configuration?.Rules?.ToDictionary(rule => rule.Code, rule => rule.ReportAs);

            _annotations = new List <Annotation>();
            _report      = new StringBuilder();
        }
        internal void Initialize(IFileSystem fileSystem, IEventSource eventSource,
                                 IEnvironmentProvider environmentProvider, ISubmissionService submissionService,
                                 IParameterParser parameterParser, ILogDataBuilderFactory logDataBuilderFactory)
        {
            _environmentProvider = environmentProvider;
            _parameters          = parameterParser.Parse(Parameters);

            if (string.IsNullOrWhiteSpace(_parameters.Token))
            {
                _environmentProvider.WriteLine("BuildCrossCheck Token is not present");
                return;
            }

            if (string.IsNullOrWhiteSpace(_parameters.CloneRoot))
            {
                _environmentProvider.WriteLine("BuildCrossCheck CloneRoot is not present");
                return;
            }

            if (string.IsNullOrWhiteSpace(_parameters.Owner))
            {
                _environmentProvider.WriteLine("BuildCrossCheck Owner is not present");
                return;
            }

            if (string.IsNullOrWhiteSpace(_parameters.Repo))
            {
                _environmentProvider.WriteLine("BuildCrossCheck Repo is not present");
                return;
            }

            if (string.IsNullOrWhiteSpace(_parameters.Hash))
            {
                _environmentProvider.WriteLine("BuildCrossCheck Hash is not present");
                return;
            }

            if (!_parameters.PullRequestNumber.HasValue)
            {
                _environmentProvider.WriteLine("BuildCrossCheck PullRequestNumber is not present");
                return;
            }

            _environmentProvider.WriteLine("BuildCrossCheck Enabled");

            _configuration  = LoadCheckRunConfiguration(fileSystem, _parameters.ConfigurationFile);
            _logDataBuilder = logDataBuilderFactory.BuildLogDataBuilder(_parameters, _configuration);

            _submissionService = submissionService;

            eventSource.BuildStarted  += EventSourceOnBuildStarted;
            eventSource.BuildFinished += EventSourceOnBuildFinished;
            eventSource.WarningRaised += EventSourceOnWarningRaised;
            eventSource.ErrorRaised   += EventSourceOnErrorRaised;
        }
Exemplo n.º 5
0
        public void Should_Create_CheckRun_With_Configuration()
        {
            var annotations = new[]
            {
                new Annotation(
                    Faker.System.FilePath(),
                    Faker.Random.Int(),
                    Faker.Random.Int(),
                    AnnotationLevel.Warning,
                    Faker.Lorem.Word())
            };

            var expectedCheckRunConfiguration = new CheckRunConfiguration
            {
                Name  = Faker.Lorem.Word(),
                Rules = new[]
                {
                    new LogAnalyzerRule
                    {
                        Code     = Faker.Lorem.Word(),
                        ReportAs = Faker.Random.Enum <ReportAs>()
                    },
                },
            };

            var configurationFile = Faker.System.FilePath();
            var mockFileSystem    = new MockFileSystem();

            mockFileSystem.AddFile(configurationFile, new MockFileData(JsonConvert.SerializeObject(expectedCheckRunConfiguration)));

            var mockBinaryLogProcessor = CreateMockBinaryLogProcessor(annotations, Faker.Lorem.Paragraph(), 1);
            var checkRun = GetCheckRun(mockBinaryLogProcessor, owner: Faker.Internet.UserName(), repo: Faker.Random.Word(), hash: Faker.Random.Guid().ToString(), configurationFile: configurationFile, mockFileSystem: mockFileSystem);

            mockBinaryLogProcessor.Received(1).ProcessLog(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <CheckRunConfiguration>());
            var checkRunConfiguration = mockBinaryLogProcessor.ReceivedCalls().First().GetArguments()[5] as CheckRunConfiguration;

            checkRunConfiguration.Should().BeEquivalentTo(expectedCheckRunConfiguration);

            checkRun.Conclusion.Should().Be(CheckConclusion.Success);
            checkRun.Name.Should().Be(expectedCheckRunConfiguration.Name);
            checkRun.Title.Should().Be("0 errors - 1 warning");
            checkRun.Summary.Should().NotBeNullOrWhiteSpace();
            checkRun.Annotations.Should().BeEquivalentTo <Annotation>(annotations);
        }
        public void Should_TestConsoleApp1_Warning_ConfigureAs_Ignore()
        {
            var checkRunConfiguration = new CheckRunConfiguration()
            {
                Rules = new LogAnalyzerRule[]
                {
                    new LogAnalyzerRule()
                    {
                        Code     = "CS0219",
                        ReportAs = ReportAs.Ignore
                    }
                }
            };

            var cloneRoot = @"C:\projects\testconsoleapp1\";
            var logData   = ProcessLog("testconsoleapp1-1warning.binlog", cloneRoot, Faker.Internet.UserName(), Faker.Random.Word(), Faker.Random.String(10), checkRunConfiguration);

            logData.Annotations.Should().BeEmpty();

            logData.Report.Should().BeNullOrWhiteSpace();
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        public LogData ProcessLog(string binLogPath, string cloneRoot, string owner, string repo, string hash, CheckRunConfiguration configuration = null)
        {
            Logger.LogInformation("ProcessLog binLogPath:{0} cloneRoot:{1}", binLogPath, cloneRoot);

            var ruleDictionary =
                configuration?.Rules?.ToDictionary(rule => rule.Code, rule => rule.ReportAs);

            var reportTotalBytes = 0.0;
            var reportingMaxed   = false;

            var warningCount = 0;
            var errorCount   = 0;
            var annotations  = new List <Annotation>();
            var report       = new StringBuilder();

            foreach (var record in _binaryLogReader.ReadRecords(binLogPath))
            {
                var buildEventArgs = record.Args;
                var buildWarning   = buildEventArgs as BuildWarningEventArgs;
                var buildError     = buildEventArgs as BuildErrorEventArgs;

                if (buildWarning == null && buildError == null)
                {
                    continue;
                }

                AnnotationLevel checkWarningLevel;
                string          buildCode;
                string          projectFile;
                string          file;
                string          message;
                int             lineNumber;
                int             endLineNumber;
                string          code;
                string          recordTypeString;
                if (buildWarning != null)
                {
                    warningCount++;
                    recordTypeString = "Warning";

                    checkWarningLevel = AnnotationLevel.Warning;
                    buildCode         = buildWarning.Code;
                    projectFile       = buildWarning.ProjectFile;
                    file          = buildWarning.File;
                    code          = buildWarning.Code;
                    message       = buildWarning.Message;
                    lineNumber    = buildWarning.LineNumber;
                    endLineNumber = buildWarning.EndLineNumber;
                }
                else
                {
                    errorCount++;
                    recordTypeString = "Error";

                    checkWarningLevel = AnnotationLevel.Failure;
                    buildCode         = buildError.Code;
                    projectFile       = buildError.ProjectFile;
                    file          = buildError.File;
                    code          = buildError.Code;
                    message       = buildError.Message;
                    lineNumber    = buildError.LineNumber;
                    endLineNumber = buildError.EndLineNumber;
                }


                endLineNumber = endLineNumber == 0 ? lineNumber : endLineNumber;

                if (buildCode.StartsWith("MSB"))
                {
                    if (projectFile == null)
                    {
                        projectFile = file;
                    }
                    else
                    {
                        file = projectFile;
                    }
                }

                var filePath = GetFilePath(cloneRoot, projectFile ?? file, file);
                var title    = $"{code}: {filePath}({lineNumber})";

                ReportAs reportAs = ReportAs.AsIs;
                if (ruleDictionary?.TryGetValue(buildCode, out reportAs) ?? false)
                {
                    switch (reportAs)
                    {
                    case ReportAs.Ignore:
                        continue;

                    case ReportAs.AsIs:
                        break;

                    case ReportAs.Notice:
                        checkWarningLevel = AnnotationLevel.Notice;
                        break;

                    case ReportAs.Warning:
                        checkWarningLevel = AnnotationLevel.Warning;
                        break;

                    case ReportAs.Error:
                        checkWarningLevel = AnnotationLevel.Failure;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                annotations.Add(CreateAnnotation(checkWarningLevel,
                                                 cloneRoot,
                                                 title,
                                                 message,
                                                 lineNumber,
                                                 endLineNumber,
                                                 filePath));

                if (!reportingMaxed)
                {
                    var lineReference = lineNumber != endLineNumber ? $"L{lineNumber}-L{endLineNumber}" : $"L{lineNumber}";

                    var line      = $"- [{filePath}({lineNumber})](https://github.com/{owner}/{repo}/tree/{hash}/{filePath}#{lineReference}) **{recordTypeString} - {code}** {message}{Environment.NewLine}";
                    var lineBytes = Encoding.Unicode.GetByteCount(line) / 1024.0;

                    if (reportTotalBytes + lineBytes < 128.0)
                    {
                        report.Append(line);
                        reportTotalBytes += lineBytes;
                    }
                    else
                    {
                        reportingMaxed = true;
                    }
                }
            }

            return(new LogData
            {
                Annotations = annotations.ToArray(),
                WarningCount = warningCount,
                ErrorCount = errorCount,
                Report = report.ToString()
            });
        }
        public void Proces(string inputFile, string outputFile, string cloneRoot, string owner, string repo, string hash, string configurationFile = null)
        {
            if (!_fileSystem.File.Exists(inputFile))
            {
                throw new InvalidOperationException($"Input file `{inputFile}` does not exist.");
            }

            if (_fileSystem.File.Exists(outputFile))
            {
                throw new InvalidOperationException($"Output file `{outputFile}` already exists.");
            }

            CheckRunConfiguration configuration = null;

            if (configurationFile != null)
            {
                if (!_fileSystem.File.Exists(configurationFile))
                {
                    throw new InvalidOperationException($"Configuration file `{configurationFile}` does not exist.");
                }

                var configurationString = _fileSystem.File.ReadAllText(configurationFile);
                if (string.IsNullOrWhiteSpace(configurationString))
                {
                    throw new InvalidOperationException($"Content of configuration file `{configurationFile}` is null or empty.");
                }

                configuration = JsonConvert.DeserializeObject <CheckRunConfiguration>(configurationString, new JsonSerializerSettings
                {
                    Formatting       = Formatting.None,
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Converters       = new List <JsonConverter>
                    {
                        new StringEnumConverter {
                            NamingStrategy = new CamelCaseNamingStrategy()
                        }
                    },
                    MissingMemberHandling = MissingMemberHandling.Error
                });
            }

            var dateTimeOffset = DateTimeOffset.Now;
            var logData        = _binaryLogProcessor.ProcessLog(inputFile, cloneRoot, owner, repo, hash, configuration);

            var hasAnyFailure = logData.Annotations.Any() &&
                                logData.Annotations.Any(annotation => annotation.AnnotationLevel == AnnotationLevel.Failure);

            var stringBuilder = new StringBuilder();

            stringBuilder.Append(logData.ErrorCount.ToString());
            stringBuilder.Append(" ");
            stringBuilder.Append(logData.ErrorCount == 1 ? "error": "errors");
            stringBuilder.Append(" - ");
            stringBuilder.Append(logData.WarningCount.ToString());
            stringBuilder.Append(" ");
            stringBuilder.Append(logData.WarningCount == 1 ? "warning" : "warnings");

            var createCheckRun = new CreateCheckRun
            {
                Annotations = logData.Annotations,
                Conclusion  = !hasAnyFailure ? CheckConclusion.Success : CheckConclusion.Failure,
                StartedAt   = dateTimeOffset,
                CompletedAt = DateTimeOffset.Now,
                Summary     = logData.Report,
                Name        = configuration?.Name ?? "MSBuild Log",
                Title       = stringBuilder.ToString(),
            };

            var contents = createCheckRun.ToJson();

            _fileSystem.File.WriteAllText(outputFile, contents);
        }
Exemplo n.º 9
0
 public ILogDataBuilder BuildLogDataBuilder(Parameters parameters, CheckRunConfiguration configuration)
 {
     return(new LogDataBuilder(parameters, configuration));
 }
        private LogData ProcessLog(string resourceName, string cloneRoot, string userName, string repo, string hash, CheckRunConfiguration checkRunConfiguration = null)
        {
            var resourcePath = TestUtils.GetResourcePath(resourceName);

            File.Exists(resourcePath).Should().BeTrue();

            var binaryLogReader = new BinaryLogReader();
            var logProcessor    = new BinaryLogProcessor(binaryLogReader, TestLogger.Create <BinaryLogProcessor>(_testOutputHelper));

            return(logProcessor.ProcessLog(resourcePath, cloneRoot, userName, repo, hash, checkRunConfiguration));
        }