public ServerContext createContextFromTfvcServerUrl()

in plugin/src/com/microsoft/alm/plugin/context/ServerContextManager.java [387:446]


    public ServerContext createContextFromTfvcServerUrl(
            @NotNull URI tfvcServerUrl,
            String teamProjectName,
            boolean prompt) {
        String tfvcServerUrlString = tfvcServerUrl.toString();

        // Get matching context from manager
        ServerContext context = get(tfvcServerUrlString);
        logger.info("createContextFromTfvcServerUrl context exists: " + (context != null));
        if (context == null || context.getServerUri() == null ||
                context.getTeamProjectCollectionReference() == null ||
                context.getTeamProjectCollectionReference().getName() == null ||
                context.getTeamProjectReference() == null ||
                context.getTeamProjectReference().getId() == null ||
                !StringUtils.equalsIgnoreCase(context.getTeamProjectReference().getName(), teamProjectName)) {
            context = null;
            logger.info("createContextFromTfvcServerUrl context fully populated: " + (context != null));
        }

        if (context == null) {
            // Manager didn't have a matching context, so try to look up the auth info
            final AuthenticationInfo authenticationInfo = getAuthenticationInfo(tfvcServerUrl, prompt);
            if (authenticationInfo != null) {
                final ServerContext.Type type = UrlHelper.isTeamServicesUrl(tfvcServerUrlString)
                        ? ServerContext.Type.VSO
                        : ServerContext.Type.TFS;

                final ServerContext contextToValidate = new ServerContextBuilder()
                        .type(type).uri(tfvcServerUrl).authentication(authenticationInfo)
                        .teamProject(teamProjectName).build();
                logger.info("type = " + type + ", uri = " + tfvcServerUrl + ", projectName = " + teamProjectName);
                try {
                    context = validateServerConnection(contextToValidate);
                } catch (NotAuthorizedException e) {
                    logger.warn("createContextFromTfvcServerUrl: NotAuthorizedException occurred");
                    if (prompt) {
                        // refreshing creds
                        logger.info("createContextFromTfvcServerUrl: refreshing creds");
                        updateAuthenticationInfo(tfvcServerUrlString);
                        final AuthenticationInfo authenticationInfoRefreshed = getAuthenticationInfo(tfvcServerUrl, prompt);
                        final ServerContext contextToValidateRefreshed = new ServerContextBuilder()
                                .type(type).uri(tfvcServerUrl).authentication(authenticationInfoRefreshed)
                                .teamProject(teamProjectName).build();
                        context = validateServerConnection(contextToValidateRefreshed);
                    } else {
                        logger.info("createContextFromTfvcServerUrl: Stale creds detected but feature does not allow prompting for refresh");
                    }
                }
            } else {
                logger.warn("createContextFromTfvcServerUrl: authentication info returned null");
            }
        }

        if (context != null && context.getUserId() == null) {
            //validate the context and save it with userId
            context = validateServerConnection(context);
        }

        return context;
    }