public void CodeAnalysisResultManager_TryResolveFilePathFromSourceControl_Github()
        {
            string workingDirectory = @"c:\temp\";
            string fileFromLog      = ".github/workflows/dotnet-format.yml";
            Uri    mapToPath        = new Uri("file:///c:/temp/microsoft/sarif-visualstudio-extension/main/");
            Uri    targetFileUri    = new Uri(mapToPath, fileFromLog);

            var versionControlDetail = new VersionControlDetails
            {
                RepositoryUri = new Uri("https://github.com/microsoft/sarif-visualstudio-extension/"),
                RevisionId    = "378c2ee96a7dc1d8e487e2a02ce4dc73f67750e7",
                Branch        = "main",
            };
            var versionControlList = new List <VersionControlDetails>()
            {
                versionControlDetail
            };

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem
            .Setup(fs => fs.FileExists(targetFileUri.LocalPath))
            .Returns(true);

            var resultManager = new CodeAnalysisResultManager(mockFileSystem.Object, promptForResolvedPathDelegate: null);

            resultManager.AddAllowedDownloadHost("raw.githubusercontent.com");
            bool result = resultManager.TryResolveFilePathFromSourceControl(versionControlList, fileFromLog, workingDirectory, mockFileSystem.Object, out string resolvedPath);

            result.Should().BeTrue();
            resolvedPath.Should().BeEquivalentTo(targetFileUri.LocalPath);
        }
コード例 #2
0
        internal static bool TryGetVersionControlParser(VersionControlDetails versionControl, out IVersionControlParser parser)
        {
            if (versionControl?.RepositoryUri != null &&
                versionControl.RepositoryUri.IsHttpScheme() &&
                versionControl.RepositoryUri.Host.Equals(GithubHost, StringComparison.OrdinalIgnoreCase))
            {
                parser = new GithubVersionControlParser(versionControl);
                return(true);
            }

            // not working due to need credential to access ado/vsts files

            /*
             * if (versionControl.RepositoryUri.IsHttpScheme() &&
             *  versionControl.RepositoryUri.Host.Equals(AdoHost, StringComparison.OrdinalIgnoreCase))
             * {
             *  parser = new AdoVersionControlParser(versionControl);
             *  return true;
             * }
             */

            // no corresponding parser found
            parser = null;
            return(false);
        }
        public void CodeAnalysisResultManager_TryResolveFilePathFromSourceControl_WithMappedToUri()
        {
            string workingDirectory = @"c:\temp\";
            string fileFromLog      = "src/Sarif/Baseline/ResultMatching/RemappingCalculators/SarifLogRemapping.cs";
            Uri    mapToPath        = new Uri("file:///C:/repo/sarif-sdk/");
            Uri    targetFileUri    = new Uri(mapToPath, fileFromLog);

            var versionControlDetail = new VersionControlDetails
            {
                RepositoryUri = new Uri("https://example.com"),
                RevisionId    = "1234567879abcedf",
                Branch        = "master",
                MappedTo      = new ArtifactLocation {
                    Uri = mapToPath
                },
            };
            var versionControlList = new List <VersionControlDetails>()
            {
                versionControlDetail
            };

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem
            .Setup(fs => fs.FileExists(targetFileUri.LocalPath))
            .Returns(true);

            var  resultManager = new CodeAnalysisResultManager(mockFileSystem.Object, promptForResolvedPathDelegate: null);
            bool result        = resultManager.TryResolveFilePathFromSourceControl(versionControlList, fileFromLog, workingDirectory, mockFileSystem.Object, out string resolvedPath);

            result.Should().BeTrue();
            resolvedPath.Should().BeEquivalentTo(@"c:\repo\sarif-sdk\src\Sarif\Baseline\ResultMatching\RemappingCalculators\SarifLogRemapping.cs");
        }
コード例 #4
0
        private void Visit(VersionControlDetails versionControlDetails, string versionControlDetailsPointer)
        {
            Analyze(versionControlDetails, versionControlDetailsPointer);

            if (versionControlDetails.MappedTo != null)
            {
                Visit(versionControlDetails.MappedTo, versionControlDetailsPointer.AtProperty(SarifPropertyName.MappedTo));
            }
        }
コード例 #5
0
 protected override void Analyze(VersionControlDetails versionControlDetails, string versionControlDetailsPointer)
 {
     AnalyzeUri(versionControlDetails.RepositoryUri, versionControlDetailsPointer.AtProperty(SarifPropertyName.RepositoryUri));
 }
コード例 #6
0
        protected override string ConstructTestOutputFromInputResource(string inputResourceName, object parameter)
        {
            SarifLog actualLog = PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(
                GetResourceText(inputResourceName),
                formatting: Formatting.Indented,
                updatedLog: out _);

            // Some of the tests operate on SARIF files that mention the absolute path of the file
            // that was "analyzed" (InsertOptionalDataVisitor.txt). That path depends on the repo
            // root, and so can vary depending on the machine where the tests are run. To avoid
            // this problem, both the input files and the expected output files contain a fixed
            // string "REPLACED_AT_TEST_RUNTIME" in place of the directory portion of the path. But some
            // of the tests must read the contents of the analyzed file (for instance, when the
            // test requires snippets or file hashes to be inserted). Those test require the actual
            // path. Therefore we replace the fixed string with the actual path, execute the
            // visitor, and then restore the fixed string so the actual output can be compared
            // to the expected output.
            string enlistmentRoot = GitHelper.Default.GetRepositoryRoot(Environment.CurrentDirectory, useCache: false);

            if (inputResourceName == "Inputs.CoreTests-Relative.sarif")
            {
                Uri originalUri = actualLog.Runs[0].OriginalUriBaseIds["TESTROOT"].Uri;
                string uriString = originalUri.ToString();

                uriString = uriString.Replace(EnlistmentRoot, enlistmentRoot);

                actualLog.Runs[0].OriginalUriBaseIds["TESTROOT"] = new ArtifactLocation { Uri = new Uri(uriString, UriKind.Absolute) };

                var visitor = new InsertOptionalDataVisitor(_currentOptionallyEmittedData);
                visitor.Visit(actualLog.Runs[0]);

                // Restore the remanufactured URI so that file diffing succeeds.
                actualLog.Runs[0].OriginalUriBaseIds["TESTROOT"] = new ArtifactLocation { Uri = originalUri };

                // In some of the tests, the visitor added an originalUriBaseId for the repo root.
                // Adjust that one, too.
                string repoRootUriBaseId = InsertOptionalDataVisitor.GetUriBaseId(0);
                if (actualLog.Runs[0].OriginalUriBaseIds.TryGetValue(repoRootUriBaseId, out ArtifactLocation artifactLocation))
                {
                    Uri repoRootUri = artifactLocation.Uri;
                    string repoRootString = repoRootUri.ToString();
                    repoRootString = repoRootString.Replace(enlistmentRoot.Replace(@"\", "/"), EnlistmentRoot);

                    actualLog.Runs[0].OriginalUriBaseIds[repoRootUriBaseId] = new ArtifactLocation { Uri = new Uri(repoRootString, UriKind.Absolute) };
                }
            }
            else if (inputResourceName == "Inputs.CoreTests-Absolute.sarif")
            {
                Uri originalUri = actualLog.Runs[0].Artifacts[0].Location.Uri;
                string uriString = originalUri.ToString();

                uriString = uriString.Replace(EnlistmentRoot, enlistmentRoot);

                actualLog.Runs[0].Artifacts[0].Location = new ArtifactLocation { Uri = new Uri(uriString, UriKind.Absolute) };

                var visitor = new InsertOptionalDataVisitor(_currentOptionallyEmittedData);
                visitor.Visit(actualLog.Runs[0]);

                // Restore the remanufactured URI so that file diffing matches
                actualLog.Runs[0].Artifacts[0].Location = new ArtifactLocation { Uri = originalUri };
            }
            else
            {
                var visitor = new InsertOptionalDataVisitor(_currentOptionallyEmittedData);
                visitor.Visit(actualLog.Runs[0]);
            }

            // Verify and remove Guids, because they'll vary with every run and can't be compared to a fixed expected output.
            if (_currentOptionallyEmittedData.HasFlag(OptionallyEmittedData.Guids))
            {
                for (int i = 0; i < actualLog.Runs[0].Results.Count; ++i)
                {
                    Result result = actualLog.Runs[0].Results[i];
                    result.Guid.Should().NotBeNullOrEmpty(because: "OptionallyEmittedData.Guids flag was set");

                    result.Guid = null;
                }
            }

            if (_currentOptionallyEmittedData.HasFlag(OptionallyEmittedData.VersionControlDetails))
            {
                VersionControlDetails versionControlDetails = actualLog.Runs[0].VersionControlProvenance[0];

                // Verify and replace the mapped directory (enlistment root), because it varies
                // from machine to machine.
                var mappedUri = new Uri(enlistmentRoot + @"\", UriKind.Absolute);
                versionControlDetails.MappedTo.Uri.Should().Be(mappedUri);
                versionControlDetails.MappedTo.Uri = new Uri($"file:///{EnlistmentRoot}/");

                // When OptionallyEmittedData includes any file-related content, the visitor inserts
                // an artifact that points to the enlistment root. So we have to verify and adjust
                // that as well.
                IList<Artifact> artifacts = actualLog.Runs[0].Artifacts;
                if (artifacts.Count >= 2)
                {
                    artifacts[1].Location.Uri.Should().Be(enlistmentRoot);
                    artifacts[1].Location.Uri = new Uri($"file:///{EnlistmentRoot}/");
                }

                // Verify and replace the remote repo URI, because it would be different in a fork.
                var gitHelper = new GitHelper();
                Uri remoteUri = gitHelper.GetRemoteUri(enlistmentRoot);

                versionControlDetails.RepositoryUri.Should().Be(remoteUri);
                versionControlDetails.RepositoryUri = new Uri("https://REMOTE_URI");

                // Verify and remove branch and revision id, because they vary from run to run.
                versionControlDetails.Branch.Should().NotBeNullOrEmpty(because: "OptionallyEmittedData.VersionControlInformation flag was set");
                versionControlDetails.Branch = null;

                versionControlDetails.RevisionId.Should().NotBeNullOrEmpty(because: "OptionallyEmittedData.VersionControlInformation flag was set");
                versionControlDetails.RevisionId = null;
            }

            return JsonConvert.SerializeObject(actualLog, Formatting.Indented);
        }
コード例 #7
0
 private void Visit(VersionControlDetails versionControlDetails, string versionControlDetailsPointer)
 {
     Analyze(versionControlDetails, versionControlDetailsPointer);
 }
コード例 #8
0
 protected virtual void Analyze(VersionControlDetails versionControlDetails, string versionControlDetailsPointer)
 {
 }
 internal GithubVersionControlParser(VersionControlDetails versionControl)
 {
     this.details = versionControl;
 }