public async Task InitializeAsync(
            ILocalRepositoryModel localRepository,
            IConnection connection,
            string owner,
            string repo,
            int pullRequestNumber)
        {
            if (repo != localRepository.Name)
            {
                throw new NotSupportedException();
            }

            IsLoading = true;

            try
            {
                LocalRepository       = localRepository;
                RemoteRepositoryOwner = owner;
                session = await sessionManager.GetSession(owner, repo, pullRequestNumber);
                await Load(session.PullRequest);
            }
            finally
            {
                IsLoading = false;
            }
        }
Пример #2
0
        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="pullRequest">The pull request.</param>
        /// <returns>A task that tracks the execution of the command.</returns>
        public override async Task Execute(IPullRequestModel pullRequest)
        {
            if (pullRequest == null)
            {
                return;
            }

            var package = (Microsoft.VisualStudio.Shell.Package)Package;
            var window  = (PullRequestCommentsPane)package.FindToolWindow(
                typeof(PullRequestCommentsPane), pullRequest.Number, true);

            if (window?.Frame == null)
            {
                throw new NotSupportedException("Cannot create Pull Request Comments tool window");
            }

            var session = await sessionManager.GetSession(pullRequest);

            var address   = HostAddress.Create(session.LocalRepository.CloneUrl);
            var apiClient = await apiClientFactory.Create(address);

            await window.Initialize(session, apiClient);

            var windowFrame = (IVsWindowFrame)window.Frame;

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }
        async Task Load(IPullRequestModel pullRequest)
        {
            try
            {
                session = await sessionManager.GetSession(pullRequest);

                PullRequestModel = pullRequest;

                Model = pullRequest.Reviews.FirstOrDefault(x =>
                                                           x.State == PullRequestReviewState.Pending && x.User.Login == session.User.Login) ??
                        new PullRequestReviewModel
                {
                    Body  = string.Empty,
                    User  = session.User,
                    State = PullRequestReviewState.Pending,
                };

                Body = Model.Body;

                sessionSubscription?.Dispose();
                await UpdateFileComments();

                sessionSubscription = session.PullRequestChanged.Subscribe(_ => UpdateFileComments().Forget());
            }
            finally
            {
                IsBusy = false;
            }
        }
        /// <inheritdoc/>
        async Task Load(IAccount author, IPullRequestModel pullRequest)
        {
            IsBusy = true;

            try
            {
                session = await sessionManager.GetSession(pullRequest);

                User             = author;
                PullRequestTitle = pullRequest.Title;

                var reviews = new List <IPullRequestReviewViewModel>();
                var isFirst = true;

                foreach (var review in pullRequest.Reviews.OrderByDescending(x => x.SubmittedAt))
                {
                    if (review.User.Login == author.Login &&
                        review.State != PullRequestReviewState.Pending)
                    {
                        var vm = new PullRequestReviewViewModel(editorService, session, pullRequest, review);
                        vm.IsExpanded = isFirst;
                        reviews.Add(vm);
                        isFirst = false;
                    }
                }

                Reviews = reviews;
            }
            finally
            {
                IsBusy = false;
            }
        }
        public async Task InitializeAsync(
            ILocalRepositoryModel localRepository,
            IConnection connection,
            string owner,
            string repo,
            int pullRequestNumber)
        {
            if (repo != localRepository.Name)
            {
                throw new NotSupportedException();
            }

            IsLoading = true;

            try
            {
                LocalRepository       = localRepository;
                RemoteRepositoryOwner = owner;
                session = await sessionManager.GetSession(owner, repo, pullRequestNumber).ConfigureAwait(true);
                await Load(session.PullRequest).ConfigureAwait(true);

                if (LocalRepository?.CloneUrl != null)
                {
                    var key = GetDraftKey();

                    if (string.IsNullOrEmpty(Body))
                    {
                        var draft = await draftStore.GetDraft <PullRequestReviewDraft>(key, string.Empty)
                                    .ConfigureAwait(true);

                        Body = draft?.Body;
                    }

                    this.WhenAnyValue(x => x.Body)
                    .Throttle(TimeSpan.FromSeconds(1), timerScheduler)
                    .Select(x => new PullRequestReviewDraft {
                        Body = x
                    })
                    .Subscribe(x => draftStore.UpdateDraft(key, string.Empty, x));
                }
            }
            finally
            {
                IsLoading = false;
            }
        }
Пример #6
0
        /// <inheritdoc/>
        public async Task InitializeAsync(
            LocalRepositoryModel localRepository,
            IConnection connection,
            string owner,
            string repo,
            int number)
        {
            IsLoading = true;

            try
            {
                if (!string.Equals(repo, localRepository.Name, StringComparison.OrdinalIgnoreCase))
                {
                    throw new NotSupportedException("Showing pull requests from other repositories not yet supported.");
                }

                LocalRepository       = localRepository;
                RemoteRepositoryOwner = owner;
                Number       = number;
                WebUrl       = localRepository.CloneUrl.ToRepositoryUrl(owner).Append("pull/" + number);
                modelService = await modelServiceFactory.CreateAsync(connection);

                Session = await sessionManager.GetSession(owner, repo, number);
                await Load(Session.PullRequest);

                teamExplorerContext.StatusChanged += RefreshIfActive;
            }
            catch (Exception ex)
            {
                Error = ex;
            }
            finally
            {
                IsLoading = false;
            }
        }
Пример #7
0
        static Tuple <PullRequestDetailViewModel, IPullRequestService> CreateTargetAndService(
            string currentBranch    = "master",
            string existingPrBranch = null,
            bool prFromFork         = false,
            bool dirty   = false,
            int aheadBy  = 0,
            int behindBy = 0,
            IPullRequestSessionManager sessionManager = null)
        {
            var repository = new LocalRepositoryModel
            {
                CloneUrl  = new UriString(Uri.ToString()),
                LocalPath = @"C:\projects\ThisRepo",
                Name      = "repo"
            };

            var currentBranchModel = new BranchModel(currentBranch, repository);
            var gitService         = Substitute.For <IGitService>();

            gitService.GetBranch(repository).Returns(currentBranchModel);

            var pullRequestService = Substitute.For <IPullRequestService>();

            if (existingPrBranch != null)
            {
                var existingBranchModel = new BranchModel(existingPrBranch, repository);
                pullRequestService.GetLocalBranches(repository, Arg.Any <PullRequestDetailModel>())
                .Returns(Observable.Return(existingBranchModel));
            }
            else
            {
                pullRequestService.GetLocalBranches(repository, Arg.Any <PullRequestDetailModel>())
                .Returns(Observable.Empty <BranchModel>());
            }

            pullRequestService.Checkout(repository, Arg.Any <PullRequestDetailModel>(), Arg.Any <string>()).Returns(x => Throws("Checkout threw"));
            pullRequestService.GetDefaultLocalBranchName(repository, Arg.Any <int>(), Arg.Any <string>()).Returns(x => Observable.Return($"pr/{x[1]}"));
            pullRequestService.IsPullRequestFromRepository(repository, Arg.Any <PullRequestDetailModel>()).Returns(!prFromFork);
            pullRequestService.IsWorkingDirectoryClean(repository).Returns(Observable.Return(!dirty));
            pullRequestService.Pull(repository).Returns(x => Throws("Pull threw"));
            pullRequestService.Push(repository).Returns(x => Throws("Push threw"));
            pullRequestService.SwitchToBranch(repository, Arg.Any <PullRequestDetailModel>())
            .Returns(
                x => Throws("Switch threw"),
                _ => Observable.Return(Unit.Default));

            var divergence = Substitute.For <BranchTrackingDetails>();

            divergence.AheadBy.Returns(aheadBy);
            divergence.BehindBy.Returns(behindBy);
            pullRequestService.CalculateHistoryDivergence(repository, Arg.Any <int>())
            .Returns(Observable.Return(divergence));

            if (sessionManager == null)
            {
                var currentSession = Substitute.For <IPullRequestSession>();
                currentSession.PullRequest.Returns(CreatePullRequestModel());
                currentSession.User.Returns(new ActorModel {
                    Login = "******"
                });

                sessionManager = Substitute.For <IPullRequestSessionManager>();
                sessionManager.CurrentSession.Returns(currentSession);
                sessionManager.GetSession("owner", "repo", 1).ReturnsForAnyArgs(currentSession);
            }

            var vm = new PullRequestDetailViewModel(
                pullRequestService,
                sessionManager,
                Substitute.For <IModelServiceFactory>(),
                Substitute.For <IUsageTracker>(),
                Substitute.For <ITeamExplorerContext>(),
                Substitute.For <IPullRequestFilesViewModel>(),
                Substitute.For <ISyncSubmodulesCommand>(),
                Substitute.For <IViewViewModelFactory>(),
                gitService,
                Substitute.For <IOpenIssueishDocumentCommand>(),
                new JoinableTaskContext());

            vm.InitializeAsync(repository, Substitute.For <IConnection>(), "owner", "repo", 1).Wait();

            return(Tuple.Create(vm, pullRequestService));
        }