Example #1
0
        public int Run(FileWorkItemsOptions options, IFileSystem fileSystem)
        {
            var filingContext = new SarifWorkItemContext();

            if (!string.IsNullOrEmpty(options.ConfigurationFilePath))
            {
                filingContext.LoadFromXml(options.ConfigurationFilePath);
            }

            if (!ValidateOptions(options, filingContext, fileSystem))
            {
                return(FAILURE);
            }

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

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

            EnsureValidSarifLogFile(logFileContents, options.InputFilePath);

            if (options.SplittingStrategy != SplittingStrategy.None)
            {
                filingContext.SplittingStrategy = options.SplittingStrategy;
            }

            if (options.DataToRemove.ToFlags() != OptionallyEmittedData.None)
            {
                filingContext.DataToRemove = options.DataToRemove.ToFlags();
            }

            if (options.DataToInsert.ToFlags() != OptionallyEmittedData.None)
            {
                filingContext.DataToInsert = options.DataToInsert.ToFlags();
            }

            var filer = new SarifWorkItemFiler(filingContext);

            filer.FileWorkItems(logFileContents);

            // TODO: We need to process updated work item models to persist filing
            //       details back to the input SARIF file, if that was specified.
            //       The SarifWorkItemFiler should either return or persist the updated
            //       models via a property, so that we can do this work.
            //
            //       This information should be inlined to the input file, if configured,
            //       or persisted to a new SARIF file, if configured. If neither of
            //       those options is specified, there is no work to do.
            //
            //       https://github.com/microsoft/sarif-sdk/issues/1774

            return(SUCCESS);
        }
        public int Run(FileWorkItemsOptions options, IFileSystem fileSystem)
        {
            using (var filingContext = new SarifWorkItemContext())
            {
                if (!string.IsNullOrEmpty(options.ConfigurationFilePath))
                {
                    filingContext.LoadFromXml(options.ConfigurationFilePath);
                }

                if (!ValidateOptions(options, filingContext, fileSystem))
                {
                    return(FAILURE);
                }

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

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

                if (!options.DoNotValidate)
                {
                    EnsureValidSarifLogFile(logFileContents, options.InputFilePath);
                }

                if (options.SplittingStrategy != SplittingStrategy.None)
                {
                    filingContext.SplittingStrategy = options.SplittingStrategy;
                }

                if (options.SyncWorkItemMetadata != null)
                {
                    filingContext.SyncWorkItemMetadata = options.SyncWorkItemMetadata.Value;
                }

                if (options.ShouldFileUnchanged != null)
                {
                    filingContext.ShouldFileUnchanged = options.ShouldFileUnchanged.Value;
                }

                if (options.DataToRemove.ToFlags() != OptionallyEmittedData.None)
                {
                    filingContext.DataToRemove = options.DataToRemove.ToFlags();
                }

                if (options.DataToInsert.ToFlags() != OptionallyEmittedData.None)
                {
                    filingContext.DataToInsert = options.DataToInsert.ToFlags();
                }

                SarifLog sarifLog = null;
                using (var filer = new SarifWorkItemFiler(filingContext.HostUri, filingContext))
                {
                    sarifLog = filer.FileWorkItems(logFileContents);
                }

                // By the time we're here, we have updated options.OutputFilePath with the
                // options.InputFilePath argument (in the presence of --inline) and validated
                // that we can write to this location with one exception: we do not currently
                // handle inlining to a read-only location.
                string outputFilePath = options.OutputFilePath;
                if (!string.IsNullOrEmpty(outputFilePath))
                {
                    string sarifLogText = JsonConvert.SerializeObject(sarifLog, options.Formatting);

                    fileSystem.FileWriteAllText(outputFilePath, sarifLogText);
                }
            }

            return(SUCCESS);
        }