public override async Task <CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis)
        {
            var tempPath      = Path.GetTempPath();
            var randomSegment = Guid.NewGuid().ToString("N");
            var tempDirectory = Path.Combine(tempPath, "ApiView", randomSegment);

            Directory.CreateDirectory(tempDirectory);
            var originalFilePath = Path.Combine(tempDirectory, originalName);

            var jsonFilePath = Path.ChangeExtension(originalFilePath, ".json");

            using (var file = File.Create(originalFilePath))
            {
                await stream.CopyToAsync(file);
            }

            try
            {
                var apiStubGenPath = GetPythonVirtualEnv(tempDirectory);
                var arguments      = GetProcessorArguments(originalName, tempDirectory, jsonFilePath);
                RunProcess(tempDirectory, apiStubGenPath, arguments);
                using (var codeFileStream = File.OpenRead(jsonFilePath))
                {
                    var codeFile = await CodeFile.DeserializeAsync(codeFileStream);

                    codeFile.VersionString = VersionString;
                    codeFile.Language      = Name;
                    return(codeFile);
                }
            }
            finally
            {
                Directory.Delete(tempDirectory, true);
            }
        }
Example #2
0
        public async Task <CodeFile> GetCodeFile(string repoName, string buildId, string artifactName, string packageName, string originalFileName, string codeFileName, MemoryStream memoryStream)
        {
            Stream   stream   = null;
            CodeFile codeFile = null;

            if (string.IsNullOrEmpty(codeFileName))
            {
                // backward compatibility until all languages moved to sandboxing of codefile to pipeline
                stream = await _devopsArtifactRepository.DownloadPackageArtifact(repoName, buildId, artifactName, originalFileName, "file");

                codeFile = await CreateCodeFile(Path.GetFileName(originalFileName), stream, false, memoryStream);
            }
            else
            {
                stream = await _devopsArtifactRepository.DownloadPackageArtifact(repoName, buildId, artifactName, packageName, "zip");

                var archive = new ZipArchive(stream);
                foreach (var entry in archive.Entries)
                {
                    var fileName = Path.GetFileName(entry.Name);
                    if (fileName == originalFileName)
                    {
                        await entry.Open().CopyToAsync(memoryStream);
                    }
                    else if (fileName == codeFileName)
                    {
                        codeFile = await CodeFile.DeserializeAsync(entry.Open());
                    }
                }
            }

            return(codeFile);
        }
Example #3
0
        public override async Task <CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis)
        {
            var tempPath      = Path.GetTempPath();
            var randomSegment = Guid.NewGuid().ToString("N");
            var tempDirectory = Path.Combine(tempPath, "ApiView", randomSegment);

            Directory.CreateDirectory(tempDirectory);
            var originalFilePath = Path.Combine(tempDirectory, originalName);
            var jsonFilePath     = Path.ChangeExtension(originalFilePath, ".json");

            MemoryStream zipStream = new MemoryStream();
            await stream.CopyToAsync(zipStream);

            zipStream.Position = 0;
            var archive = new ZipArchive(zipStream);

            archive.ExtractToDirectory(tempDirectory);
            var packageRootDirectory = originalFilePath.Replace(Extension, "");

            try
            {
                var arguments        = GetProcessorArguments(packageRootDirectory, tempDirectory, tempDirectory);
                var processStartInfo = new ProcessStartInfo(ProcessName, arguments);
                processStartInfo.WorkingDirectory       = tempDirectory;
                processStartInfo.RedirectStandardError  = true;
                processStartInfo.RedirectStandardOutput = true;

                using (var process = Process.Start(processStartInfo))
                {
                    process.WaitForExit();
                    if (process.ExitCode != 0)
                    {
                        throw new InvalidOperationException(
                                  "Processor failed: " + Environment.NewLine +
                                  "stdout: " + Environment.NewLine +
                                  process.StandardOutput.ReadToEnd() + Environment.NewLine +
                                  "stderr: " + Environment.NewLine +
                                  process.StandardError.ReadToEnd() + Environment.NewLine);
                    }
                }

                using (var codeFileStream = File.OpenRead(jsonFilePath))
                {
                    var codeFile = await CodeFile.DeserializeAsync(codeFileStream);

                    codeFile.VersionString = VersionString;
                    codeFile.Language      = Name;
                    return(codeFile);
                }
            }
            finally
            {
                Directory.Delete(tempDirectory, true);
            }
        }
Example #4
0
        public async Task <CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis)
        {
            var tempPath      = Path.GetTempPath();
            var randomSegment = Guid.NewGuid().ToString("N");
            var tempDirectory = Path.Combine(tempPath, "ApiView", randomSegment);

            Directory.CreateDirectory(tempDirectory);
            var originalFilePath = Path.Combine(tempDirectory, originalName);

            var jsonFilePath = Path.ChangeExtension(originalFilePath, ".json");

            using (var file = File.Create(originalFilePath))
            {
                await stream.CopyToAsync(file);
            }

            try
            {
                var jarPath = Path.Combine(
                    Path.GetDirectoryName(typeof(JavaLanguageService).Assembly.Location),
                    JarName);
                var arguments        = $"-jar {jarPath} \"{originalName}\" \"{tempDirectory}\"";
                var processStartInfo = new ProcessStartInfo("java", arguments);
                processStartInfo.WorkingDirectory       = tempDirectory;
                processStartInfo.RedirectStandardError  = true;
                processStartInfo.RedirectStandardOutput = true;

                using (var process = Process.Start(processStartInfo))
                {
                    process.WaitForExit();
                    if (process.ExitCode != 0)
                    {
                        throw new InvalidOperationException(
                                  "Java processor failed: " + Environment.NewLine +
                                  "stdout: " + Environment.NewLine +
                                  process.StandardOutput.ReadToEnd() + Environment.NewLine +
                                  "stderr: " + Environment.NewLine +
                                  process.StandardError.ReadToEnd() + Environment.NewLine);
                    }
                }

                using (var codeFileStream = File.OpenRead(jsonFilePath))
                {
                    var codeFile = await CodeFile.DeserializeAsync(codeFileStream);

                    codeFile.VersionString = JarName;
                    codeFile.Language      = "Java";
                    return(codeFile);
                }
            }
            finally
            {
                Directory.Delete(tempDirectory, true);
            }
        }
        public async Task <RenderedCodeFile> GetCodeFileAsync(string revisionId, string codeFileId)
        {
            var client = GetBlobClient(revisionId, codeFileId, out var key);

            if (_cache.TryGetValue <RenderedCodeFile>(key, out var codeFile))
            {
                return(codeFile);
            }

            var info = await client.DownloadAsync();

            codeFile = new RenderedCodeFile(await CodeFile.DeserializeAsync(info.Value.Content));

            using var _ = _cache.CreateEntry(key)
                          .SetSlidingExpiration(TimeSpan.FromDays(1))
                          .SetValue(codeFile);

            return(codeFile);
        }
        public override async Task <CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis)
        {
            var tempPath = Path.GetTempPath();

            _telemetryClient.TrackEvent("Creating code file for " + originalName);
            var randomSegment = Guid.NewGuid().ToString("N");
            var tempDirectory = Path.Combine(tempPath, "ApiView", randomSegment);

            Directory.CreateDirectory(tempDirectory);
            var originalFilePath = Path.Combine(tempDirectory, originalName);
            var jsonFilePath     = Path.ChangeExtension(originalFilePath, ".json");

            using (var file = File.Create(originalFilePath))
            {
                await stream.CopyToAsync(file);
            }
            try
            {
                var pythonVenvPath = GetPythonVirtualEnv(tempDirectory);
                var arguments      = GetProcessorArguments(originalName, tempDirectory, jsonFilePath);
                RunProcess(tempDirectory, pythonVenvPath, arguments);
                _telemetryClient.TrackEvent("Completed Python process run to parse " + originalName);
                using (var codeFileStream = File.OpenRead(jsonFilePath))
                {
                    var codeFile = await CodeFile.DeserializeAsync(codeFileStream);

                    codeFile.VersionString = VersionString;
                    codeFile.Language      = Name;
                    return(codeFile);
                }
            }
            catch (Exception ex)
            {
                _telemetryClient.TrackException(ex);
                throw ex;
            }
            finally
            {
                Directory.Delete(tempDirectory, true);
            }
        }
        public async Task <CodeFile> GetCodeFileAsync(string revisionId, string codeFileId)
        {
            var info = await GetBlobClient(revisionId, codeFileId).DownloadAsync();

            return(await CodeFile.DeserializeAsync(info.Value.Content));
        }
 public async Task <CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis)
 {
     return(await CodeFile.DeserializeAsync(stream));
 }
 /// <summary>
 /// Generate an ApiView listing for an OpenAPI 2.0 specification in
 /// JSON.
 /// </summary>
 /// <param name="originalName">The name of the swagger file.</param>
 /// <param name="stream">Stream full of JSON.</param>
 /// <param name="runAnalysis">This is unused.</param>
 /// <returns>An ApiView listing.</returns>
 public override async Task <CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis) =>
 await CodeFile.DeserializeAsync(stream);
Example #10
0
        // API change detection for PR will pull artifact from devops artifact
        public async Task <string> DetectApiChanges(string buildId,
                                                    string artifactName,
                                                    string originalFileName,
                                                    string commitSha,
                                                    string repoName,
                                                    string packageName,
                                                    int prNumber,
                                                    string hostName,
                                                    string codeFileName         = null,
                                                    string baselineCodeFileName = null,
                                                    bool commentOnPR            = true,
                                                    string language             = null,
                                                    string project = "public")
        {
            var requestTelemetry = new RequestTelemetry {
                Name = "Detecting API changes for PR: " + prNumber
            };
            var operation = _telemetryClient.StartOperation(requestTelemetry);

            originalFileName = originalFileName ?? codeFileName;
            string[] repoInfo         = repoName.Split("/");
            var      pullRequestModel = await GetPullRequestModel(prNumber, repoName, packageName, originalFileName, language);

            if (pullRequestModel == null)
            {
                return("");
            }
            if (pullRequestModel.Commits.Any(c => c == commitSha))
            {
                // PR commit is already processed. No need to reprocess it again.
                return(!string.IsNullOrEmpty(pullRequestModel.ReviewId)? REVIEW_URL.Replace("{hostName}", hostName)
                       .Replace("{ReviewId}", pullRequestModel.ReviewId) : "");
            }

            pullRequestModel.Commits.Add(commitSha);
            //Check if PR owner is part of Azure//Microsoft org in GitHub
            await AssertPullRequestCreatorPermission(pullRequestModel);

            using var memoryStream   = new MemoryStream();
            using var baselineStream = new MemoryStream();
            var codeFile = await _reviewManager.GetCodeFile(repoName, buildId, artifactName, packageName, originalFileName, codeFileName, memoryStream, baselineCodeFileName : baselineCodeFileName, baselineStream : baselineStream, project : project);

            CodeFile baseLineCodeFile = null;

            if (baselineStream.Length > 0)
            {
                baselineStream.Position = 0;
                baseLineCodeFile        = await CodeFile.DeserializeAsync(baselineStream);
            }

            if (codeFile != null)
            {
                await CreateRevisionIfRequired(codeFile, prNumber, originalFileName, memoryStream, pullRequestModel, baseLineCodeFile, baselineStream, baselineCodeFileName);
            }
            else
            {
                _telemetryClient.TrackTrace("Failed to download artifact. Please recheck build id and artifact path values in API change detection request.");
            }

            //Generate combined single comment to update on PR.
            var prReviews = await _pullRequestsRepository.GetPullRequestsAsync(prNumber, repoName);

            if (commentOnPR)
            {
                await CreateOrUpdateComment(prReviews, repoInfo[0], repoInfo[1], prNumber, hostName);
            }

            // Return review URL created for current package if exists
            var review = prReviews.SingleOrDefault(r => r.PackageName == packageName && (r.Language == null || r.Language == language));

            return(review == null ? "" : REVIEW_URL.Replace("{hostName}", hostName).Replace("{ReviewId}", review.ReviewId));
        }