internal RunVersionOne CreateRunVersionOne(Run v2Run)
        {
            RunVersionOne run = null;

            if (v2Run != null)
            {
                if (v2Run.TryGetProperty("sarifv1/run", out run))
                {
                    return(run);
                }
                else
                {
                    _currentV2Run = v2Run;

                    // We need to create the run before we start working on children
                    // because some of them will need to refer to _currentRun
                    run         = new RunVersionOne();
                    _currentRun = run;

                    CreateFileKeyIndexMappings(v2Run.Artifacts, out _v1FileKeyToV2IndexMap, out _v2FileIndexToV1KeyMap);
                    _v2RuleIndexToV1KeyMap = CreateV2RuleIndexToV1KeyMapping(v2Run.Tool.Driver.RuleDescriptors);

                    run.BaselineId   = v2Run.BaselineInstanceGuid;
                    run.Files        = CreateFileDataVersionOneDictionary();
                    run.Id           = v2Run.Id?.InstanceGuid;
                    run.AutomationId = v2Run.AggregateIds?.FirstOrDefault()?.InstanceId;

                    run.StableId = v2Run.Id?.InstanceIdLogicalComponent();

                    run.Invocation       = CreateInvocationVersionOne(v2Run.Invocations?[0]);
                    run.LogicalLocations = CreateLogicalLocationVersionOneDictionary(v2Run.LogicalLocations);
                    run.Properties       = v2Run.Properties;
                    run.Results          = new List <ResultVersionOne>();

                    run.Rules = ConvertRulesArrayToDictionary(_currentV2Run.Tool.Driver.RuleDescriptors, _v2RuleIndexToV1KeyMap);
                    run.Tool  = CreateToolVersionOne(v2Run.Tool);

                    foreach (Result v2Result in v2Run.Results)
                    {
                        run.Results.Add(CreateResultVersionOne(v2Result));
                    }

                    // Stash the entire v2 run in this v1 run's property bag
                    if (EmbedVersionTwoContentInPropertyBag)
                    {
                        run.SetProperty($"{FromPropertyBagPrefix}/run", v2Run);
                    }
                }
            }

            return(run);
        }
Exemple #2
0
        internal RunVersionOne CreateRun(Run v2Run)
        {
            RunVersionOne run = null;

            if (v2Run != null)
            {
                if (v2Run.TryGetProperty("sarifv1/run", out run))
                {
                    return(run);
                }
                else
                {
                    _currentV2Run = v2Run;

                    // We need to create the run before we start working on children
                    // because some of them will need to refer to _currentRun
                    run         = new RunVersionOne();
                    _currentRun = run;

                    run.Architecture     = v2Run.Architecture;
                    run.AutomationId     = v2Run.AutomationLogicalId;
                    run.BaselineId       = v2Run.BaselineInstanceGuid;
                    run.Files            = v2Run.Files?.ToDictionary(v => v.Key, v => CreateFileData(v.Value));
                    run.Id               = v2Run.InstanceGuid;
                    run.Invocation       = CreateInvocation(v2Run.Invocations?[0]);
                    run.LogicalLocations = v2Run.LogicalLocations?.ToDictionary(v => v.Key, v => CreateLogicalLocation(v.Value));
                    run.Properties       = v2Run.Properties;
                    run.Results          = new List <ResultVersionOne>();
                    run.Rules            = v2Run.Resources?.Rules?.ToDictionary(v => v.Key, v => CreateRule(v.Value));
                    run.StableId         = v2Run.LogicalId;
                    run.Tool             = CreateTool(v2Run.Tool);

                    foreach (Result v2Result in v2Run.Results)
                    {
                        run.Results.Add(CreateResult(v2Result));
                    }

                    // Stash the entire v2 run in this v1 run's property bag
                    run.SetProperty($"{FromPropertyBagPrefix}/run", v2Run);
                }
            }

            return(run);
        }
        internal Run CreateRun(RunVersionOne v1Run)
        {
            Run run = null;

            if (v1Run != null)
            {
                if (v1Run.TryGetProperty("sarifv2/run", out run))
                {
                    return(run);
                }
                else
                {
                    _currentV1Run = v1Run;

                    _v1FileKeytoV2IndexMap = CreateFileKeyToIndexMapping(v1Run.Files);
                    _v1RuleKeyToV2IndexMap = CreateRuleKeyToIndexMapping(v1Run.Rules);

                    RunAutomationDetails   id           = null;
                    RunAutomationDetails[] aggregateIds = null;

                    if (v1Run.Id != null || v1Run.StableId != null)
                    {
                        id = new RunAutomationDetails
                        {
                            InstanceGuid = v1Run.Id,
                            InstanceId   = v1Run.StableId != null ? v1Run.StableId + "/" : null
                        };
                    }

                    if (v1Run.AutomationId != null)
                    {
                        aggregateIds = new[] { new RunAutomationDetails {
                                                   InstanceId = v1Run.AutomationId + "/"
                                               } };
                    }

                    run = new Run()
                    {
                        Id                   = id,
                        AggregateIds         = aggregateIds,
                        BaselineInstanceGuid = v1Run.BaselineId,
                        Properties           = v1Run.Properties,
                        Tool                 = CreateTool(v1Run.Tool),
                        ColumnKind           = ColumnKind.Utf16CodeUnits
                    };

                    _currentRun = run;

                    if (v1Run.Rules != null)
                    {
                        run.Tool.Driver.RuleDescriptors = new List <ReportingDescriptor>();

                        foreach (var pair in v1Run.Rules)
                        {
                            run.Tool.Driver.RuleDescriptors.Add(CreateRule(pair.Value));
                        }
                    }

                    if (v1Run.Files != null)
                    {
                        run.Artifacts = new List <Artifact>();

                        foreach (KeyValuePair <string, FileDataVersionOne> pair in v1Run.Files)
                        {
                            FileDataVersionOne fileDataVersionOne = pair.Value;
                            if (fileDataVersionOne.Uri == null)
                            {
                                fileDataVersionOne.Uri = new Uri(pair.Key, UriKind.RelativeOrAbsolute);
                            }

                            run.Artifacts.Add(CreateFileData(fileDataVersionOne, pair.Key));
                        }
                    }

                    if (v1Run.LogicalLocations != null)
                    {
                        // Pass 1 over results. In this phase, we're simply collecting fully qualified names that
                        // may be duplicated in the logical locations dictionary. We're doing this so that we
                        // can properly construct the v2 logical instances in the converted array (i.e., we can't
                        // populate the v2 logicalLocation.FullyQualifiedName property in cases where the
                        // v1 key is a synthesized value and not actually the fully qualified name)
                        var visitor = new VersionOneLogicalLocationKeyToLogicalLocationDataVisitor();
                        visitor.VisitRunVersionOne(v1Run);

                        _v1KeyToFullyQualifiedNameMap           = visitor.LogicalLocationKeyToFullyQualifiedNameMap;
                        _v1LogicalLocationKeyToDecoratedNameMap = visitor.LogicalLocationKeyToDecoratedNameMap;

                        run.LogicalLocations = new List <LogicalLocation>();
                        HashSet <string> populatedKeys = new HashSet <string>();

                        foreach (KeyValuePair <string, LogicalLocationVersionOne> pair in v1Run.LogicalLocations)
                        {
                            PopulateLogicalLocation(
                                run,
                                v1Run.LogicalLocations,
                                _v1LogicalLocationKeyToDecoratedNameMap,
                                _v1KeyToFullyQualifiedNameMap,
                                pair.Key,
                                pair.Value,
                                populatedKeys);
                        }
                    }

                    // Even if there is no v1 invocation, there may be notifications
                    // in which case we will need a v2 invocation to contain them
                    Invocation invocation = CreateInvocation(v1Run.Invocation,
                                                             v1Run.ToolNotifications,
                                                             v1Run.ConfigurationNotifications);

                    if (invocation != null)
                    {
                        run.Invocations = new List <Invocation>()
                        {
                            invocation
                        };
                    }

                    if (v1Run.Results != null)
                    {
                        run.Results = new List <Result>();

                        foreach (ResultVersionOne v1Result in v1Run.Results)
                        {
                            Result result = CreateResult(v1Result);
                            run.Results.Add(result);
                        }
                    }

                    // Stash the entire v1 run in this v2 run's property bag
                    if (EmbedVersionOneContentInPropertyBag)
                    {
                        run.SetProperty($"{FromPropertyBagPrefix}/run", v1Run);
                    }
                }
            }

            _currentRun = null;

            return(run);
        }
        internal Run CreateRun(RunVersionOne v1Run)
        {
            Run run = null;

            if (v1Run != null)
            {
                if (v1Run.TryGetProperty("sarifv2/run", out run))
                {
                    return(run);
                }
                else
                {
                    _currentV1Run = v1Run;

                    run = new Run()
                    {
                        Architecture         = v1Run.Architecture,
                        AutomationLogicalId  = v1Run.AutomationId,
                        BaselineInstanceGuid = v1Run.BaselineId,
                        InstanceGuid         = v1Run.Id,
                        Properties           = v1Run.Properties,
                        Results   = new List <Result>(),
                        LogicalId = v1Run.StableId,
                        Tool      = CreateTool(v1Run.Tool)
                    };

                    _currentRun = run;

                    if (v1Run.Rules != null)
                    {
                        run.Resources = new Resources
                        {
                            Rules = new Dictionary <string, Rule>()
                        };

                        foreach (var pair in v1Run.Rules)
                        {
                            run.Resources.Rules.Add(pair.Key, CreateRule(pair.Value));
                        }
                    }

                    if (v1Run.Files != null)
                    {
                        run.Files = new Dictionary <string, FileData>();

                        foreach (var pair in v1Run.Files)
                        {
                            run.Files.Add(pair.Key, CreateFileData(pair.Value));
                        }
                    }

                    if (v1Run.LogicalLocations != null)
                    {
                        run.LogicalLocations = new Dictionary <string, LogicalLocation>();

                        foreach (var pair in v1Run.LogicalLocations)
                        {
                            run.LogicalLocations.Add(pair.Key, CreateLogicalLocation(pair.Value));
                        }

                        RemoveRedundantLogicalLocationProperties();
                    }

                    // Even if there is no v1 invocation, there may be notifications
                    // in which case we will need a v2 invocation to contain them
                    Invocation invocation = CreateInvocation(v1Run.Invocation,
                                                             v1Run.ToolNotifications,
                                                             v1Run.ConfigurationNotifications);

                    if (invocation != null)
                    {
                        run.Invocations = new List <Invocation>()
                        {
                            invocation
                        };
                    }

                    foreach (ResultVersionOne v1Result in v1Run.Results)
                    {
                        run.Results.Add(CreateResult(v1Result));
                    }

                    // Stash the entire v1 run in this v2 run's property bag
                    run.SetProperty($"{FromPropertyBagPrefix}/run", v1Run);
                }
            }

            return(run);
        }