Exemplo n.º 1
0
        protected async Task <RepositoryOrigin> GetRepositoryOrigin(UriString uri)
        {
            if (uri == null)
            {
                return(RepositoryOrigin.Other);
            }

            Debug.Assert(apiFactory != null, "apiFactory cannot be null. Did you call the right constructor?");
            SimpleApiClient = await apiFactory.Create(uri);

            var isdotcom = HostAddress.IsGitHubDotComUri(uri.ToRepositoryUrl());

            if (isdotcom)
            {
                return(RepositoryOrigin.DotCom);
            }
            else
            {
                var repo = await SimpleApiClient.GetRepository();

                if ((repo.FullName == ActiveRepoName || repo.Id == 0) && await SimpleApiClient.IsEnterprise())
                {
                    return(RepositoryOrigin.Enterprise);
                }
            }

            return(RepositoryOrigin.Other);
        }
    static GitHubPaneViewModel CreateTarget(
        IViewViewModelFactory viewModelFactory   = null,
        ISimpleApiClientFactory apiClientFactory = null,
        IConnectionManager connectionManager     = null,
        ITeamExplorerContext teamExplorerContext = null,
        IVisualStudioBrowser browser             = null,
        IUsageTracker usageTracker     = null,
        INavigationViewModel navigator = null,
        ILoggedOutViewModel loggedOut  = null,
        INotAGitHubRepositoryViewModel notAGitHubRepository = null,
        INotAGitRepositoryViewModel notAGitRepository       = null,
        ILoginFailedViewModel loginFailed = null)
    {
        viewModelFactory     = viewModelFactory ?? Substitute.For <IViewViewModelFactory>();
        connectionManager    = connectionManager ?? Substitute.For <IConnectionManager>();
        teamExplorerContext  = teamExplorerContext ?? CreateTeamExplorerContext(ValidGitHubRepo);
        browser              = browser ?? Substitute.For <IVisualStudioBrowser>();
        usageTracker         = usageTracker ?? Substitute.For <IUsageTracker>();
        loggedOut            = loggedOut ?? Substitute.For <ILoggedOutViewModel>();
        notAGitHubRepository = notAGitHubRepository ?? Substitute.For <INotAGitHubRepositoryViewModel>();
        notAGitRepository    = notAGitRepository ?? Substitute.For <INotAGitRepositoryViewModel>();
        loginFailed          = loginFailed ?? Substitute.For <ILoginFailedViewModel>();

        if (navigator == null)
        {
            navigator = CreateNavigator();
            navigator.Content.Returns((IPanePageViewModel)null);
        }

        if (apiClientFactory == null)
        {
            var validGitHubRepoClient     = Substitute.For <ISimpleApiClient>();
            var validEnterpriseRepoClient = Substitute.For <ISimpleApiClient>();
            var invalidRepoClient         = Substitute.For <ISimpleApiClient>();

            validGitHubRepoClient.GetRepository().Returns(new Octokit.Repository(1));
            validEnterpriseRepoClient.GetRepository().Returns(new Octokit.Repository(1));
            validEnterpriseRepoClient.IsEnterprise().Returns(true);

            apiClientFactory = Substitute.For <ISimpleApiClientFactory>();
            apiClientFactory.Create(null).ReturnsForAnyArgs(invalidRepoClient);
            apiClientFactory.Create(ValidGitHubRepo).Returns(validGitHubRepoClient);
            apiClientFactory.Create(ValidEnterpriseRepo).Returns(validEnterpriseRepoClient);
        }

        return(new GitHubPaneViewModel(
                   viewModelFactory,
                   apiClientFactory,
                   connectionManager,
                   teamExplorerContext,
                   browser,
                   usageTracker,
                   navigator,
                   loggedOut,
                   notAGitHubRepository,
                   notAGitRepository,
                   loginFailed));
    }
Exemplo n.º 3
0
        async Task UpdateContent(ILocalRepositoryModel repository)
        {
            log.Debug("UpdateContent called with {CloneUrl}", repository?.CloneUrl);

            LocalRepository = repository;
            Connection      = null;
            Content         = null;
            navigator.Clear();

            if (repository == null)
            {
                log.Debug("Not a git repository: {CloneUrl}", repository?.CloneUrl);
                Content = notAGitRepository;
                return;
            }
            else if (string.IsNullOrWhiteSpace(repository.CloneUrl))
            {
                log.Debug("Not a GitHub repository: {CloneUrl}", repository?.CloneUrl);
                Content = notAGitHubRepository;
                return;
            }

            var repositoryUrl = repository.CloneUrl.ToRepositoryUrl();
            var isDotCom      = HostAddress.IsGitHubDotComUri(repositoryUrl);
            var client        = await apiClientFactory.Create(repository.CloneUrl);

            var isEnterprise = isDotCom ? false : await client.IsEnterprise();

            if ((isDotCom || isEnterprise) && await IsValidRepository(client))
            {
                var hostAddress = HostAddress.Create(repository.CloneUrl);

                Connection = await connectionManager.GetConnection(hostAddress);

                if (Connection?.IsLoggedIn == true)
                {
                    log.Debug("Found a GitHub repository: {CloneUrl}", repository?.CloneUrl);
                    Content = navigator;
                    await ShowDefaultPage();
                }
                else
                {
                    log.Debug("Found a a GitHub repository but not logged in: {CloneUrl}", repository?.CloneUrl);
                    Content = loggedOut;
                }
            }
            else
            {
                log.Debug("Not a GitHub repository: {CloneUrl}", repository?.CloneUrl);
                Content = notAGitHubRepository;
            }
        }
Exemplo n.º 4
0
        async Task UpdateContent(ILocalRepositoryModel repository)
        {
            LocalRepository = repository;
            Connection      = null;

            Content = null;

            if (repository == null)
            {
                Content = notAGitRepository;
                return;
            }
            else if (string.IsNullOrWhiteSpace(repository.CloneUrl))
            {
                Content = notAGitHubRepository;
                return;
            }

            var repositoryUrl = repository.CloneUrl.ToRepositoryUrl();
            var isDotCom      = HostAddress.IsGitHubDotComUri(repositoryUrl);
            var client        = await apiClientFactory.Create(repository.CloneUrl);

            var isEnterprise = isDotCom ? false : client.IsEnterprise();

            if ((isDotCom || isEnterprise) && await IsValidRepository(client))
            {
                var hostAddress = HostAddress.Create(repository.CloneUrl);

                Connection = await connectionManager.GetConnection(hostAddress);

                if (Connection != null)
                {
                    navigator.Clear();
                    Content = navigator;
                    await ShowDefaultPage();
                }
                else
                {
                    Content = loggedOut;
                }
            }
            else
            {
                Content = notAGitHubRepository;
            }
        }
        protected async Task <bool> IsAGitHubRepo()
        {
            var uri = ActiveRepoUri;

            if (uri == null)
            {
                return(false);
            }

            SimpleApiClient = apiFactory.Create(uri);

            if (!HostAddress.IsGitHubDotComUri(uri.ToRepositoryUrl()))
            {
                var repo = await SimpleApiClient.GetRepository();

                return(repo.FullName == ActiveRepoName && SimpleApiClient.IsEnterprise());
            }
            return(true);
        }
        protected async Task <bool> IsAGitHubRepo()
        {
            var uri = ActiveRepoUri;

            if (uri == null)
            {
                return(false);
            }

            Debug.Assert(apiFactory != null, "apiFactory cannot be null. Did you call the right constructor?");
            SimpleApiClient = apiFactory.Create(uri);

            var isdotcom = HostAddress.IsGitHubDotComUri(uri.ToRepositoryUrl());

            if (!isdotcom)
            {
                var repo = await SimpleApiClient.GetRepository();

                return((repo.FullName == ActiveRepoName || repo.Id == 0) && SimpleApiClient.IsEnterprise());
            }
            return(isdotcom);
        }
Exemplo n.º 7
0
        async Task UpdateContent(LocalRepositoryModel repository)
        {
            log.Debug("UpdateContent called with {CloneUrl}", repository?.CloneUrl);

            LocalRepository = repository;
            connectionSubscription?.Dispose();
            connectionSubscription = null;
            Connection             = null;
            Content = null;
            navigator.Clear();

            if (repository == null)
            {
                log.Debug("Not a git repository: {CloneUrl}", repository?.CloneUrl);
                Content = notAGitRepository;
                return;
            }
            else if (string.IsNullOrWhiteSpace(repository.CloneUrl))
            {
                if (repository.HasRemotesButNoOrigin)
                {
                    log.Debug("No origin remote");
                    Content = noRemoteOrigin;
                }
                else
                {
                    log.Debug("Not a GitHub repository: {CloneUrl}", repository?.CloneUrl);
                    Content = notAGitHubRepository;
                }

                return;
            }

            var repositoryUrl = repository.CloneUrl.ToRepositoryUrl();
            var isDotCom      = HostAddress.IsGitHubDotComUri(repositoryUrl);
            var client        = await apiClientFactory.Create(repository.CloneUrl);

            var isEnterprise = isDotCom ? false : await client.IsEnterprise();

            var notGitHubRepo = true;

            if (isDotCom || isEnterprise)
            {
                var hostAddress = HostAddress.Create(repository.CloneUrl);

                notGitHubRepo = false;

                Connection = await connectionManager.GetConnection(hostAddress);

                Connection?.WhenAnyValue(
                    x => x.IsLoggedIn,
                    x => x.IsLoggingIn,
                    (_, __) => Unit.Default)
                .Skip(1)
                .Throttle(TimeSpan.FromMilliseconds(100))
                .ObserveOn(RxApp.MainThreadScheduler)
                .Subscribe(_ => UpdateContent(LocalRepository).Forget());

                if (Connection?.IsLoggedIn == true)
                {
                    if (await IsValidRepository(client) == true)
                    {
                        log.Debug("Found a GitHub repository: {CloneUrl}", repository?.CloneUrl);
                        Content = navigator;
                        await ShowDefaultPage();
                    }
                    else
                    {
                        notGitHubRepo = true;
                    }
                }
                else if (Connection?.IsLoggingIn == true)
                {
                    log.Debug("Found a GitHub repository: {CloneUrl} and logging in", repository?.CloneUrl);
                    Content = null;
                }
                else if (Connection?.ConnectionError != null)
                {
                    log.Debug("Found a GitHub repository: {CloneUrl} with login error", repository?.CloneUrl);
                    loginFailed.Initialize(Connection.ConnectionError.GetUserFriendlyError(ErrorType.LoginFailed));
                    Content = loginFailed;
                }
                else
                {
                    log.Debug("Found a a GitHub repository but not logged in: {CloneUrl}", repository?.CloneUrl);
                    Content = loggedOut;
                }
            }

            if (notGitHubRepo)
            {
                log.Debug("Not a GitHub repository: {CloneUrl}", repository?.CloneUrl);
                Content = notAGitHubRepository;
            }
        }