public GitDiagnosticsTab()

in git-server-tc/src/main/java/jetbrains/buildServer/buildTriggers/vcs/git/GitDiagnosticsTab.java [72:241]


  public GitDiagnosticsTab(@NotNull PagePlaces pagePlaces,
                           @NotNull WebControllerManager controllerManager,
                           @NotNull PluginDescriptor pluginDescriptor,
                           @NotNull GitVcsSupport vcsSupport,
                           @NotNull GitRepoOperations gitOperations,
                           @NotNull GitMainConfigProcessor mainConfigProcessor,
                           @NotNull ProjectManager projectManager,
                           @NotNull ExecutorServices executorServices,
                           @NotNull TeamCityNodes nodes,
                           @NotNull ServerPaths serverPaths,
                           @NotNull BuildServerEx server,
                           AuditLogFactory auditLogFactory) {
    super(pagePlaces, "gitStatus", "Git");
    myVcsSupport = vcsSupport;
    myOperations = gitOperations;
    myMainConfigProcessor = mainConfigProcessor;
    myProjectManager = projectManager;
    myExecutors = executorServices;
    myNodes = nodes;
    myServerResponsibility = server.getServerResponsibility();
    myTestConnectionResultsFolder = serverPaths.getCacheDirectory("git/testConnectionResults");
    myAuditLogFactory = auditLogFactory;

    setPermission(Permission.MANAGE_SERVER_INSTALLATION);
    setIncludeUrl(pluginDescriptor.getPluginResourcesPath("gitStatusTab.jsp"));
    register();
    controllerManager.registerController("/admin/diagnostic/nativeGitStatus.html", new BaseController() {
      @Nullable
      @Override
      protected ModelAndView doHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws Exception {
        checkPermissions(request);
        if (isGet(request)) {
          final Map<String, Object> model = new HashMap<>();
          model.put("projectGitRoots", getProjectGitRoots(request));
          return new ModelAndView(pluginDescriptor.getPluginResourcesPath("vcsRootsContainer.jsp"), model);
        }
        CSRFFilter.setSessionAttribute(request.getSession(true));
        if (request.getParameter("switch") == null) {
          // validate parameters
          final ActionErrors errors = new ActionErrors();
          final String projectExternalId = request.getParameter("testConnectionProject");
          final String vcsRoots = request.getParameter("testConnectionVcsRoots");
          final SProject project = myProjectManager.findProjectByExternalId(projectExternalId);
          if (StringUtil.isEmpty(projectExternalId)) {
            errors.addError("testConnectionProject", "Must not be empty");
          } else if (project == null) {
            errors.addError("testConnectionProject", "No project found");
          } else if (StringUtil.isEmpty(vcsRoots)) {
            errors.addError("testConnectionVcsRoots", "Must not be empty");
          }
          if (!errors.hasErrors()) {
            // test connection
            if ("ALL".equals(vcsRoots)) {
              // on page load we first try to load saved previous results for root project
              if (project.isRootProject() && "true".equals(request.getParameter("loadStored"))) {
                final File storedFile = getExitingStoredTestConnectionErrorsFile();
                if (storedFile == null) {
                  // this request tries to load previous results for root project, but there is nothing found
                  return null;
                } else {
                  final Map<VcsRootLink, List<TestConnectionError>> testConnectionErrors = getStoredTestConnectionErrors(storedFile);
                  if (testConnectionErrors == null) {
                    // this request tries to load previous results for root project, but we failed to read from file
                    return null;
                  } else {
                    final Map<String, Object> model = new HashMap<>();
                    model.put("testConnectionErrors", testConnectionErrors);
                    model.put("testConnectionProject", project);
                    model.put("testConnectionTimestamp", getTimestampFromStoredFile(storedFile).toString());
                    model.put("testConnectionInProgress", false);
                    model.put("testConnectionStopped", false);
                    model.put("testConnectionStatus", testConnectionErrors.isEmpty() ? CONNECTION_SUCCESSFUL : "");
                    return new ModelAndView(pluginDescriptor.getPluginResourcesPath("testConnectionErrors.jsp"), model);
                  }
                }
              }

              Map<VcsRootLink, List<TestConnectionError>> testConnectionErrors = null;
              boolean testConnectionInProgress = false;
              boolean testConnectionStopped = Boolean.parseBoolean(request.getParameter("stopTestConnection"));
              String testConnectionStatus = "";
              Date timestamp = Dates.now();

              final Lock lock = myLocks.get(projectExternalId);
              lock.lock();
              try {
                TestConnectionTask task = myTestConnectionsFinished.get(projectExternalId);
                if (task == null) {
                  testConnectionInProgress = !testConnectionStopped;
                  task = myTestConnectionsInProgress.get(projectExternalId);
                  if (task == null) {
                    if (testConnectionInProgress) {
                      final List<VcsRootInstance> vcsRootInstances = project.getVcsRootInstances();
                      task = new TestConnectionTask(timestamp, vcsRootInstances.size());
                      myTestConnectionsInProgress.put(projectExternalId, task);

                      executorServices.getLowPriorityExecutorService().execute(() -> runTestConnectionForAllProjectRoots(project, vcsRootInstances, Dates.now()));
                    }
                  } else if (testConnectionStopped) {
                    task.markCanceled();
                    finishTask(projectExternalId);
                  }
                }
                if (task != null) {
                  testConnectionErrors = new LinkedHashMap<>(task.getErrors());
                  testConnectionStatus = task.getStatusString();
                  timestamp = task.getTimestamp();
                  testConnectionStopped = task.isCanceled();
                }
              } finally {
                lock.unlock();
              }

              final Map<String, Object> model = new HashMap<>();
              model.put("testConnectionErrors", testConnectionErrors);
              model.put("testConnectionProject", project);
              model.put("testConnectionTimestamp", timestamp.toString());
              model.put("testConnectionInProgress", testConnectionInProgress);
              model.put("testConnectionStopped", testConnectionStopped);
              model.put("testConnectionStatus", testConnectionStatus);
              return new ModelAndView(pluginDescriptor.getPluginResourcesPath("testConnectionErrors.jsp"), model);
            } else {
              if (myProjectManager.findVcsRootByExternalId(vcsRoots) == null) {
                errors.addError("testConnectionVcsRoots", "No VCS root found");
              } else {
                try {
                  project.getVcsRootInstances().stream().filter(ri -> isGitRoot(ri) && vcsRoots.equals(ri.getParent().getExternalId())).forEach(ri -> {
                    try {
                      IOGuard.allowNetworkAndCommandLine(() -> myVcsSupport.getRemoteRefs(ri, true));
                    } catch (Throwable e) {
                      throw new TestConnectionException(e, ri.getUsages().keySet());
                    }
                  });
                } catch (TestConnectionException e) {
                  final Map<String, Object> model = new HashMap<>();
                  model.put("testConnectionError", e.getCause().getMessage());
                  model.put("affectedBuildTypes", e.getAffectedBuildTypes());
                  return new ModelAndView(pluginDescriptor.getPluginResourcesPath("testConnectionError.jsp"), model);
                }
              }
            }
          }
          new AjaxRequestProcessor().processRequest(request, response, new AjaxRequestProcessor.RequestHandler() {
            @Override
            public void handleRequest(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Element xmlResponse) {
              if (errors.hasErrors()) {
                errors.serialize(xmlResponse);
              } else {
                XmlResponseUtil.writeTestResult(xmlResponse, "");
              }
            }
          });
        } else {
          new AjaxRequestProcessor().processRequest(request, response, new AjaxRequestProcessor.RequestHandler() {
            @Override
            public void handleRequest(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Element xmlResponse) {
              boolean nativeGitOperationsEnabled = "enable".equalsIgnoreCase(request.getParameter("switchNativeGit"));
              final boolean enabled = myMainConfigProcessor.setNativeGitOperationsEnabled(nativeGitOperationsEnabled);
              xmlResponse.addContent(new Element("nativeGitOperationsEnabled").addContent(String.valueOf(enabled)));
              if (enabled)
                myAuditLogFactory.createForServer().log(ActionType.NATIVE_GIT_ENABLED, null, null, SessionUser.getUser(request));
              else
                myAuditLogFactory.createForServer().log(ActionType.NATIVE_GIT_DISABLED, null, null, SessionUser.getUser(request));
            }
          });
        }
        return null;
      }
    });
  }