private FileDataVersionOne CreateFileDataVersionOne(Artifact v2FileData)
        {
            FileDataVersionOne fileData = null;

            if (v2FileData != null)
            {
                int    parentIndex = v2FileData.ParentIndex;
                string parentKey   = parentIndex == -1
                    ? null
                    : _v2FileIndexToV1KeyMap?[parentIndex];

                fileData = new FileDataVersionOne
                {
                    Hashes     = CreateHashVersionOneListFromV2Hashes(v2FileData.Hashes),
                    Length     = v2FileData.Length,
                    MimeType   = v2FileData.MimeType,
                    Offset     = v2FileData.Offset,
                    ParentKey  = parentKey,
                    Properties = v2FileData.Properties,
                    Uri        = v2FileData.Location?.Uri,
                    UriBaseId  = v2FileData.Location?.UriBaseId
                };

                if (v2FileData.Contents != null)
                {
                    fileData.Contents = MimeType.IsTextualMimeType(v2FileData.MimeType) ?
                                        SarifUtilities.GetUtf8Base64String(v2FileData.Contents.Text) :
                                        v2FileData.Contents.Binary;
                }
            }

            return(fileData);
        }
        private IDictionary <string, FileDataVersionOne> CreateFileDataVersionOneDictionary()
        {
            Dictionary <string, FileDataVersionOne> filesVersionOne = null;

            if (_v1FileKeyToV2IndexMap != null)
            {
                filesVersionOne = new Dictionary <string, FileDataVersionOne>();
                foreach (KeyValuePair <string, int> entry in _v1FileKeyToV2IndexMap)
                {
                    string             key                = entry.Key;
                    int                index              = entry.Value;
                    Artifact           v2File             = _currentV2Run.Artifacts[index];
                    FileDataVersionOne fileDataVersionOne = CreateFileDataVersionOne(v2File);

                    // There's no need to repeat the URI in the v1 FileData object
                    // if it matches the dictionary key.
                    if (fileDataVersionOne.Uri.OriginalString.Equals(key))
                    {
                        fileDataVersionOne.Uri = null;
                    }

                    filesVersionOne[key] = fileDataVersionOne;
                }
            }

            return(filesVersionOne);
        }
Exemple #3
0
        internal FileDataVersionOne CreateFileData(FileData v2FileData)
        {
            FileDataVersionOne fileData = null;

            if (v2FileData != null)
            {
                fileData = new FileDataVersionOne
                {
                    Hashes     = v2FileData.Hashes?.Select(CreateHash).ToList(),
                    Length     = v2FileData.Length,
                    MimeType   = v2FileData.MimeType,
                    Offset     = v2FileData.Offset,
                    ParentKey  = v2FileData.ParentKey,
                    Properties = v2FileData.Properties,
                    Uri        = v2FileData.FileLocation?.Uri,
                    UriBaseId  = v2FileData.FileLocation?.UriBaseId
                };

                if (v2FileData.Contents != null)
                {
                    fileData.Contents = MimeType.IsTextualMimeType(v2FileData.MimeType) ?
                                        SarifUtilities.GetUtf8Base64String(v2FileData.Contents.Text) :
                                        v2FileData.Contents.Binary;
                }
            }

            return(fileData);
        }
        internal Artifact CreateFileData(FileDataVersionOne v1FileData, string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            Artifact fileData = null;

            if (v1FileData != null)
            {
                string parentKey   = v1FileData.ParentKey;
                int    parentIndex = parentKey == null
                    ? -1
                    : _v1FileKeytoV2IndexMap[parentKey];

                fileData = new Artifact
                {
                    Hashes      = v1FileData.Hashes?.Select(CreateHash).ToDictionary(p => p.Key, p => p.Value),
                    Length      = v1FileData.Length,
                    MimeType    = v1FileData.MimeType,
                    Offset      = v1FileData.Offset,
                    ParentIndex = parentIndex,
                    Properties  = v1FileData.Properties
                };

                fileData.Location           = ArtifactLocation.CreateFromFilesDictionaryKey(key, parentKey);
                fileData.Location.UriBaseId = v1FileData.UriBaseId;

                if (v1FileData.Contents != null)
                {
                    fileData.Contents = new ArtifactContent();

                    if (MimeType.IsTextualMimeType(v1FileData.MimeType))
                    {
                        fileData.Contents.Text = SarifUtilities.DecodeBase64String(v1FileData.Contents);
                    }
                    else
                    {
                        fileData.Contents.Binary = v1FileData.Contents;
                    }
                }
            }

            return(fileData);
        }
        internal FileData CreateFileData(FileDataVersionOne v1FileData)
        {
            FileData fileData = null;

            if (v1FileData != null)
            {
                fileData = new FileData
                {
                    Hashes     = v1FileData.Hashes?.Select(CreateHash).ToList(),
                    Length     = v1FileData.Length,
                    MimeType   = v1FileData.MimeType,
                    Offset     = v1FileData.Offset,
                    ParentKey  = v1FileData.ParentKey,
                    Properties = v1FileData.Properties
                };

                if (v1FileData.Uri != null)
                {
                    fileData.FileLocation = new FileLocation
                    {
                        Uri       = v1FileData.Uri,
                        UriBaseId = v1FileData.UriBaseId
                    };
                }

                if (v1FileData.Contents != null)
                {
                    fileData.Contents = new FileContent();

                    if (MimeType.IsTextualMimeType(v1FileData.MimeType))
                    {
                        fileData.Contents.Text = SarifUtilities.DecodeBase64String(v1FileData.Contents);
                    }
                    else
                    {
                        fileData.Contents.Binary = v1FileData.Contents;
                    }
                }
            }

            return(fileData);
        }
        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);
        }