private async Task <Repo> TryGetCloudRepoAsync(
            string url,
            string projectId,
            Dictionary <string, IList <Repo> > projectReposMap)
        {
            IList <Repo> cloudRepos;

            Debug.WriteLine($"Check project id {projectId}");
            if (!projectReposMap.TryGetValue(projectId, out cloudRepos))
            {
                try
                {
                    cloudRepos = await CsrUtils.GetCloudReposAsync(projectId);

                    projectReposMap.Add(projectId, cloudRepos);
                }
                catch (DataSourceException)
                {
                    projectReposMap.Add(projectId, null);
                    throw;
                }
            }

            if (cloudRepos == null || !cloudRepos.Any())
            {
                Debug.WriteLine($"{projectId} has no repos found");
                return(null);
            }

            return(cloudRepos.FirstOrDefault(
                       x => String.Compare(x.Url, url, StringComparison.OrdinalIgnoreCase) == 0));
        }
        private async Task CreateRepoAsync()
        {
            var csrDatasource = CsrUtils.CreateCsrDataSource(_project.ProjectId);

            IsReady = false;
            try
            {
                var watch = Stopwatch.StartNew();
                // No null check. By the time user gets here, csrDatasource won't be null.
                Result = await csrDatasource.CreateRepoAsync(RepositoryName.Trim());

                EventsReporterWrapper.ReportEvent(
                    CsrCreatedEvent.Create(CommandStatus.Success, duration: watch.Elapsed));
                _owner.Close();
            }
            catch (Exception)
            {
                EventsReporterWrapper.ReportEvent(CsrCreatedEvent.Create(CommandStatus.Failure));
                throw;
            }
            finally
            {
                IsReady = true;
            }
        }
        /// <summary>
        /// projectRepos is used to cache the list of 'cloud repos' of the project-id.
        /// </summary>
        private async Task AddLocalReposAsync(IList <GitRepository> localRepos, IList <Project> projects)
        {
            List <string> dataSourceErrorProjects = new List <string>();
            Dictionary <string, IList <Repo> > projectRepos
                = new Dictionary <string, IList <Repo> >(StringComparer.OrdinalIgnoreCase);

            foreach (var localGitRepo in localRepos)
            {
                IList <string> remoteUrls = await localGitRepo.GetRemotesUrls();

                foreach (var url in remoteUrls)
                {
                    string projectId = CsrUtils.ParseProjectId(url);
                    var    project   = projects.FirstOrDefault(x => x.ProjectId == projectId);
                    if (String.IsNullOrWhiteSpace(projectId) || project == null)
                    {
                        continue;
                    }

                    try
                    {
                        var cloudRepo = await TryGetCloudRepoAsync(url, projectId, projectRepos);

                        if (cloudRepo == null)
                        {
                            Debug.WriteLine($"{projectId} repos does not contain {url}");
                            continue;
                        }
                        Repositories.Add(new RepoItemViewModel(cloudRepo, localGitRepo.Root));
                        break;
                    }
                    catch (DataSourceException)
                    {
                        dataSourceErrorProjects.Add(project.Name);
                    }
                }
            }

            if (dataSourceErrorProjects.Any())
            {
                UserPromptUtils.Default.ErrorPrompt(
                    message: String.Format(
                        Resources.CsrFetchReposErrorMessage, String.Join(", ", dataSourceErrorProjects)),
                    title: Resources.CsrConnectSectionTitle);
            }
        }