async Task <IPullRequestModel> PushAndCreatePR(IModelService modelService,
                                                       ILocalRepositoryModel sourceRepository, IRepositoryModel targetRepository,
                                                       IBranch sourceBranch, IBranch targetBranch,
                                                       string title, string body)
        {
            // PullRequestModel doesn't keep a reference to repo
            using (var repo = await Task.Run(() => gitService.GetRepository(sourceRepository.LocalPath)))
            {
                var remote = await gitClient.GetHttpRemote(repo, "origin");

                await gitClient.Push(repo, sourceBranch.Name, remote.Name);

                if (!repo.Branches[sourceBranch.Name].IsTracking)
                {
                    await gitClient.SetTrackingBranch(repo, sourceBranch.Name, remote.Name);
                }

                // delay things a bit to avoid a race between pushing a new branch and creating a PR on it
                if (!Splat.ModeDetector.Current.InUnitTestRunner().GetValueOrDefault())
                {
                    await Task.Delay(TimeSpan.FromSeconds(5));
                }

                var ret = await modelService.CreatePullRequest(sourceRepository, targetRepository, sourceBranch, targetBranch, title, body);
                await MarkBranchAsPullRequest(repo, sourceBranch.Name, ret);

                gitExt.RefreshActiveRepositories();
                await usageTracker.IncrementCounter(x => x.NumberOfUpstreamPullRequests);

                return(ret);
            }
        }
Esempio n. 2
0
        public IObservable <Unit> SwitchRemotes(IRepositoryModel destinationRepository, bool updateOrigin, bool addUpstream, bool trackMasterUpstream)
        {
            return(Observable.Defer(() => Observable.Return(new object())
                                    .ObserveOn(RxApp.MainThreadScheduler)
                                    .Select(_ => vsGitServices.GetActiveRepo()))
                   .SelectMany(async activeRepo =>
            {
                using (activeRepo)
                {
                    Uri currentOrigin = null;

                    if (addUpstream)
                    {
                        var remote = await gitClient.GetHttpRemote(activeRepo, "origin");
                        currentOrigin = new Uri(remote.Url);
                    }

                    await SwitchRemotes(activeRepo, updateOrigin ? destinationRepository.CloneUrl.ToUri() : null,
                                        currentOrigin, trackMasterUpstream);
                }

                if (updateOrigin)
                {
                    vsGitExt.RefreshActiveRepositories();

                    var updatedRepository = vsGitExt.ActiveRepositories.FirstOrDefault();
                    log.Assert(updatedRepository?.CloneUrl == destinationRepository.CloneUrl,
                               "CloneUrl is {UpdatedRepository} not {DestinationRepository}", updatedRepository?.CloneUrl ?? "[NULL]", destinationRepository.CloneUrl);
                }

                return Unit.Default;
            }));
        }