public virtual void FileWorkItems(SarifLog sarifLog)
        {
            sarifLog = sarifLog ?? throw new ArgumentNullException(nameof(sarifLog));

            this.FilingClient.Connect(this.FilingContext.PersonalAccessToken).Wait();

            OptionallyEmittedData optionallyEmittedData = this.FilingContext.DataToRemove;

            if (optionallyEmittedData != OptionallyEmittedData.None)
            {
                var dataRemovingVisitor = new RemoveOptionalDataVisitor(optionallyEmittedData);
                dataRemovingVisitor.Visit(sarifLog);
            }

            optionallyEmittedData = this.FilingContext.DataToInsert;
            if (optionallyEmittedData != OptionallyEmittedData.None)
            {
                var dataInsertingVisitor = new InsertOptionalDataVisitor(optionallyEmittedData);
                dataInsertingVisitor.Visit(sarifLog);
            }

            SplittingStrategy splittingStrategy = this.FilingContext.SplittingStrategy;

            if (splittingStrategy == SplittingStrategy.None)
            {
                FileWorkItemsHelper(sarifLog, this.FilingContext, this.FilingClient);
                return;
            }

            for (int runIndex = 0; runIndex < sarifLog.Runs?.Count; ++runIndex)
            {
                if (sarifLog.Runs[runIndex]?.Results?.Count > 0)
                {
                    IList <SarifLog> logsToProcess = new List <SarifLog>(new SarifLog[] { sarifLog });

                    if (splittingStrategy != SplittingStrategy.PerRun)
                    {
                        SplittingVisitor visitor;
                        switch (splittingStrategy)
                        {
                        case SplittingStrategy.PerRunPerRule:
                            visitor = new PerRunPerRuleSplittingVisitor();
                            break;

                        case SplittingStrategy.PerRunPerTarget:
                            visitor = new PerRunPerTargetSplittingVisitor();
                            break;

                        case SplittingStrategy.PerRunPerTargetPerRule:
                            visitor = new PerRunPerTargetPerRuleSplittingVisitor();
                            break;

                        // TODO: Implement PerResult and PerRun splittings strategies
                        //
                        //       https://github.com/microsoft/sarif-sdk/issues/1763
                        //       https://github.com/microsoft/sarif-sdk/issues/1762
                        //
                        case SplittingStrategy.PerResult:
                        case SplittingStrategy.PerRun:
                        default:
                            throw new ArgumentOutOfRangeException($"SplittingStrategy: {splittingStrategy}");
                        }

                        visitor.VisitRun(sarifLog.Runs[runIndex]);
                        logsToProcess = visitor.SplitSarifLogs;
                    }

                    for (int splitFileIndex = 0; splitFileIndex < logsToProcess.Count; splitFileIndex++)
                    {
                        SarifLog splitLog = logsToProcess[splitFileIndex];
                        FileWorkItemsHelper(splitLog, this.FilingContext, this.FilingClient);
                    }
                }
            }
        }
Example #2
0
        public int Run(FileWorkItemsOptions options, IFileSystem fileSystem)
        {
            if (!ValidateOptions(options, fileSystem))
            {
                return(FAILURE);
            }

            // For unit tests: allow us to just validate the options and return.
            if (s_validateOptionsOnly)
            {
                return(SUCCESS);
            }

            string projectName = options.ProjectUri.GetProjectName();

            string logFileContents = fileSystem.ReadAllText(options.InputFilePath);

            EnsureValidSarifLogFile(logFileContents, options.InputFilePath);

            FilingTarget filingTarget = FilingTargetFactory.CreateFilingTarget(options.ProjectUriString);
            var          filer        = new WorkItemFiler(filingTarget);

            SarifLog sarifLog = JsonConvert.DeserializeObject <SarifLog>(logFileContents);

            if (options.DataToRemove != null)
            {
                var dataRemovingVisitor = new RemoveOptionalDataVisitor(options.DataToRemove.ToFlags());
                dataRemovingVisitor.Visit(sarifLog);
            }

            for (int runIndex = 0; runIndex < sarifLog.Runs.Count; ++runIndex)
            {
                if (sarifLog.Runs[runIndex]?.Results?.Count > 0)
                {
                    IList <SarifLog> logsToProcess = new List <SarifLog>(new SarifLog[] { sarifLog });

                    if (options.GroupingStrategy != GroupingStrategy.PerRun)
                    {
                        SplittingVisitor visitor;
                        switch (options.GroupingStrategy)
                        {
                        case GroupingStrategy.PerRunPerRule:
                            visitor = new PerRunPerRuleSplittingVisitor();
                            break;

                        case GroupingStrategy.PerRunPerTarget:
                            visitor = new PerRunPerTargetSplittingVisitor();
                            break;

                        case GroupingStrategy.PerRunPerTargetPerRule:
                            visitor = new PerRunPerTargetPerRuleSplittingVisitor();
                            break;

                        default:
                            throw new ArgumentOutOfRangeException($"GroupingStrategy: {options.GroupingStrategy}");
                        }

                        visitor.VisitRun(sarifLog.Runs[runIndex]);
                        logsToProcess = visitor.SplitSarifLogs;
                    }

                    IList <WorkItemFilingMetadata> workItemMetadata = new List <WorkItemFilingMetadata>(logsToProcess.Count);

                    for (int splitFileIndex = 0; splitFileIndex < logsToProcess.Count; splitFileIndex++)
                    {
                        SarifLog splitLog = logsToProcess[splitFileIndex];
                        workItemMetadata.Add(splitLog.CreateWorkItemFilingMetadata(projectName, options.TemplateFilePath, options.GroupingStrategy));
                    }

                    try
                    {
                        IEnumerable <WorkItemFilingMetadata> filedWorkItems = filer.FileWorkItems(options.ProjectUri, workItemMetadata, options.PersonalAccessToken).Result;

                        Console.WriteLine($"Created {filedWorkItems.Count()} work items for run {runIndex}.");
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine(ex);
                    }
                }
            }

            Console.WriteLine($"Writing log with work item Ids to {options.OutputFilePath}.");
            WriteSarifFile <SarifLog>(fileSystem, sarifLog, options.OutputFilePath, (options.PrettyPrint ? Formatting.Indented : Formatting.None));

            return(SUCCESS);
        }