Exemple #1
0
        // API change detection for PR will pull artifact from devops artifact
        public async Task DetectApiChanges(string buildId, string artifactName, string originalFileName, string commitSha, string repoName, string packageName, int prNumber, string codeFileName = "")
        {
            var requestTelemetry = new RequestTelemetry {
                Name = "Detecting API changes for PR: " + prNumber
            };
            var operation = _telemetryClient.StartOperation(requestTelemetry);

            try
            {
                string[] repoInfo         = repoName.Split("/");
                var      pullRequestModel = await _pullRequestsRepository.GetPullRequestAsync(prNumber, repoName, packageName);

                if (pullRequestModel == null)
                {
                    var issue = await _githubClient.Issue.Get(repoInfo[0], repoInfo[1], prNumber);

                    pullRequestModel = new PullRequestModel()
                    {
                        RepoName          = repoName,
                        PullRequestNumber = prNumber,
                        FilePath          = originalFileName,
                        Author            = issue.User.Login,
                        PackageName       = packageName
                    };
                }
                else
                {
                    if (pullRequestModel.Commits.Any(c => c == commitSha))
                    {
                        // PR commit is already processed. No need to reprocess it again.
                        return;
                    }
                }
                pullRequestModel.Commits.Add(commitSha);
                await AssertPullRequestCreatorPermission(pullRequestModel);

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

                if (codeFile != null)
                {
                    var apiDiff = await GetApiDiffFromAutomaticReview(codeFile, prNumber, originalFileName, memoryStream, pullRequestModel);

                    if (apiDiff != "")
                    {
                        await _githubClient.Issue.Comment.Create(repoInfo[0], repoInfo[1], prNumber, apiDiff);
                    }
                }
                else
                {
                    _telemetryClient.TrackTrace("Failed to download artifact. Please recheck build id and artifact path values in API change detection request.");
                }
            }
            catch (Exception ex)
            {
                _telemetryClient.TrackException(ex);
            }
            finally
            {
                _telemetryClient.StopOperation(operation);
            }
        }
Exemple #2
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));
        }