Example #1
0
        /// <summary>
        /// Loads the view model from octokit models.
        /// </summary>
        /// <param name="pullRequest">The pull request model.</param>
        /// <param name="files">The pull request's changed files.</param>
        public async Task Load(IPullRequestModel pullRequest)
        {
            Model = pullRequest;
            SourceBranchDisplayName = GetBranchDisplayName(pullRequest.Head?.Label);
            TargetBranchDisplayName = GetBranchDisplayName(pullRequest.Base.Label);
            Body = !string.IsNullOrWhiteSpace(pullRequest.Body) ? pullRequest.Body : "*No description provided.*";

            ChangedFilesTree.Clear();
            ChangedFilesList.Clear();

            // WPF doesn't support AddRange here so iterate through the changes.
            foreach (var change in CreateChangedFilesList(pullRequest.ChangedFiles))
            {
                ChangedFilesList.Add(change);
            }

            foreach (var change in CreateChangedFilesTree(ChangedFilesList).Children)
            {
                ChangedFilesTree.Add(change);
            }

            var localBranches = await pullRequestsService.GetLocalBranches(repository, pullRequest).ToList();

            if (localBranches.Contains(repository.CurrentBranch))
            {
                var divergence = await pullRequestsService.CalculateHistoryDivergence(repository, Model.Number);

                if (divergence.BehindBy == null)
                {
                    CheckoutMode = CheckoutMode.InvalidState;
                }
                else if (divergence.AheadBy > 0)
                {
                    CheckoutMode = pullRequestsService.IsPullRequestFromFork(repository, pullRequest) ?
                                   CheckoutMode.InvalidState : CheckoutMode.Push;
                }
                else if (divergence.BehindBy == 0)
                {
                    CheckoutMode = CheckoutMode.UpToDate;
                }
                else
                {
                    CheckoutMode  = CheckoutMode.NeedsPull;
                    CommitsBehind = divergence.BehindBy.Value;
                }
            }
            else if (localBranches.Count > 0)
            {
                CheckoutMode = CheckoutMode.Switch;
            }
            else
            {
                CheckoutMode = CheckoutMode.Fetch;
            }

            var clean = await pullRequestsService.IsCleanForCheckout(repository);

            CheckoutDisabledMessage = (!clean && CheckoutMode != CheckoutMode.UpToDate && CheckoutMode != CheckoutMode.Push) ?
                                      $"Cannot {GetCheckoutModeDescription(CheckoutMode)} as your working directory has uncommitted changes." :
                                      null;

            IsBusy = false;
        }