JenkinsWebhookManager { public void handleFailedJobEvent(JenkinsEventDto jenkinsEvent) throws ServerException { JenkinsConnector jenkinsConnector = jenkinsConnectorFactory .create(jenkinsEvent.getJenkinsUrl(), jenkinsEvent.getJobName()) .updateUrlWithCredentials(); try { String commitId = jenkinsConnector.getCommitId(jenkinsEvent.getBuildId()); String repositoryUrl = jenkinsEvent.getRepositoryUrl(); Optional<Factory> existingFailedFactory = findExistingFailedFactory(repositoryUrl, commitId); Factory failedFactory = existingFailedFactory.isPresent() ? existingFailedFactory.get() : createFailedFactory(jenkinsConnector.getFactoryId(), repositoryUrl, commitId); jenkinsConnector.addFailedBuildFactoryLink( baseUrl.substring(0, baseUrl.indexOf("/api")) + "/f?id=" + failedFactory.getId()); } catch (IOException | NotFoundException | ConflictException e) { LOG.warn(e.getMessage()); throw new ServerException(e.getMessage()); } } @Inject  JenkinsWebhookManager(
      FactoryManager factoryManager,
      UserManager userManager,
      JenkinsConnectorFactory jenkinsConnectorFactory,
      @Named("che.api") String apiUrl,
      @Named("integration.factory.owner.username") String username); void handleFailedJobEvent(JenkinsEventDto jenkinsEvent);  }
@Test public void shouldAddFailedBuildFactoryLink() throws Exception { manager.handleFailedJobEvent(jenkinsEvent); verify(jenkinsConnector).addFailedBuildFactoryLink(eq("url/f?id=factoryId")); }
@Test public void shouldGenerateFailedBuildFactory() throws Exception { parameters.put("branch", "branch"); when(factory.getName()).thenReturn("name"); manager.handleFailedJobEvent(jenkinsEvent); verify(factoryManager).saveFactory(factory); verify(factory).setName(eq("name_commitId")); assertNull(parameters.get("branch")); assertEquals(parameters.get("commitId"), "commitId"); }
@Test public void shouldNotCreateNewFactoryIfFactoryWithCommitIdIsPresent() throws Exception { parameters.put("commitId", "commitId"); manager.handleFailedJobEvent(jenkinsEvent); verify(factoryManager, never()).saveFactory(factory); }