Search in sources :

Example 1 with JunitTestResultsProvider

use of com.microfocus.octane.gitlab.testresults.JunitTestResultsProvider in project octane-gitlab-service by MicroFocus.

the class OctaneServices method getTestsResult.

@Override
public InputStream getTestsResult(String jobFullName, String buildNumber) {
    TestsResult result = dtoFactory.newDTO(TestsResult.class);
    try {
        ParsedPath project = new ParsedPath(ParsedPath.cutLastPartOfPath(jobFullName), gitLabApi, PathType.PROJECT);
        Job job = gitLabApi.getJobApi().getJob(project.getPathWithNameSpace(), Integer.parseInt(buildNumber));
        // report gherkin test results
        File mqmTestResultsFile = TestResultsHelper.getMQMTestResultsFilePath(project.getId(), job.getId(), applicationSettings.getConfig().getTestResultsOutputFolderPath());
        InputStream output = null;
        if (mqmTestResultsFile.exists() && mqmTestResultsFile.length() > 0) {
            log.info(String.format("get Gherkin Tests Result of %s from  %s, file exist=%s", project.getDisplayName(), mqmTestResultsFile.getAbsolutePath(), mqmTestResultsFile.exists()));
            try {
                output = mqmTestResultsFile.exists() && mqmTestResultsFile.length() > 0 ? new FileInputStream(mqmTestResultsFile.getAbsolutePath()) : null;
            } catch (IOException e) {
                log.error("failed to get gherkin test results for  " + project.getDisplayName() + " #" + job.getId() + " from " + mqmTestResultsFile.getAbsolutePath());
            }
            return output;
        }
        // if there is no test results for gherkin - report other test results
        BuildContext buildContext = dtoFactory.newDTO(BuildContext.class).setJobId(project.getFullPathOfProjectWithBranch().toLowerCase()).setJobName(project.getPathWithNameSpace()).setBuildId(job.getId().toString()).setBuildName(job.getId().toString()).setServerId(applicationSettings.getConfig().getCiServerIdentity());
        result = result.setBuildContext(buildContext);
        JunitTestResultsProvider junitTestResultsProvider = JunitTestResultsProvider.getInstance(applicationSettings);
        InputStream artifactFiles = gitLabApi.getJobApi().downloadArtifactsFile(project.getId(), job.getId());
        List<TestRun> tests = junitTestResultsProvider.createAndGetTestList(artifactFiles);
        if (tests != null && !tests.isEmpty()) {
            result.setTestRuns(tests);
        } else {
            log.warn("Unable to extract test results from files defined by the %s pattern. Check the pattern correctness");
        }
    } catch (Exception e) {
        log.warn("Failed to return test results", e);
    }
    return dtoFactory.dtoToXmlStream(result);
}
Also used : JunitTestResultsProvider(com.microfocus.octane.gitlab.testresults.JunitTestResultsProvider) Job(org.gitlab4j.api.models.Job) PermissionException(com.hp.octane.integrations.exceptions.PermissionException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 2 with JunitTestResultsProvider

use of com.microfocus.octane.gitlab.testresults.JunitTestResultsProvider in project octane-gitlab-service by MicroFocus.

the class EventListener method handleEvent.

private Response handleEvent(JSONObject event) {
    log.traceEntry();
    try {
        if (isMergeRequestEvent(event)) {
            return handleMergeRequestEvent(event);
        }
        CIEventType eventType = getEventType(event);
        if (eventType == CIEventType.UNDEFINED || eventType == CIEventType.QUEUED)
            return Response.ok().build();
        List<CIEvent> eventList = getCIEvents(event);
        eventList.forEach(ciEvent -> {
            if (ciEvent.getResult() == null) {
                ciEvent.setResult(CIBuildResult.UNAVAILABLE);
            }
            try {
                log.trace(new ObjectMapper().writeValueAsString(ciEvent));
            } catch (Exception e) {
                log.debug("Failed to trace an incoming event", e);
            }
            if (eventType == CIEventType.FINISHED || eventType == CIEventType.STARTED) {
                ParsedPath parsedPath = null;
                if (ciEvent.getProject().endsWith("/build")) {
                    parsedPath = new ParsedPath(ciEvent.getProject().substring(0, ciEvent.getProject().length() - 6), gitLabApi, PathType.PROJECT);
                    if (parsedPath.isMultiBranch()) {
                        ciEvent.setSkipValidation(true);
                    }
                }
                if (ciEvent.getProject().contains(ParsedPath.PIPELINE_JOB_CI_ID_PREFIX)) {
                    parsedPath = new ParsedPath(ciEvent.getProject(), gitLabApi, PathType.PIPELINE);
                    if (parsedPath.isMultiBranch()) {
                        String projectDisplayName = parsedPath.getNameWithNameSpaceForDisplayName() != null ? parsedPath.getNameWithNameSpaceForDisplayName() : parsedPath.getFullPathOfProjectWithBranch();
                        ciEvent.setProjectDisplayName(projectDisplayName + "/" + parsedPath.getCurrentBranch());
                        ciEvent.setParentCiId(parsedPath.getJobCiId(true)).setMultiBranchType(MultiBranchType.MULTI_BRANCH_CHILD).setSkipValidation(true);
                    }
                }
                if (isPipelineEvent(event)) {
                    List<CIParameter> parametersList = new ArrayList<>();
                    JSONArray variablesList = VariablesHelper.getVariablesListFromPipelineEvent(event);
                    // check if this parameter is in job level:
                    List<Variable> allVariables = VariablesHelper.getVariables(parsedPath, gitLabApi, applicationSettings.getConfig());
                    variablesList.forEach(var -> {
                        parametersList.add(VariablesHelper.convertVariableToParameter(var));
                    });
                    if (parametersList.size() > 0) {
                        ciEvent.setParameters(parametersList);
                    }
                }
            }
            OctaneSDK.getClients().forEach(client -> client.getEventsService().publishEvent(ciEvent));
        });
        if (eventType == CIEventType.FINISHED) {
            Integer projectId = isPipelineEvent(event) ? event.getJSONObject("project").getInt("id") : event.getInt("project_id");
            if (!isPipelineEvent(event)) {
                Project project = gitLabApi.getProjectApi().getProject(projectId);
                Integer jobId = getEventTargetObjectId(event);
                Job job = gitLabApi.getJobApi().getJob(projectId, jobId);
                if (job.getArtifactsFile() != null) {
                    sendCodeCoverage(projectId, project, job);
                    GherkinTestResultsProvider gherkinTestResultsProvider = GherkinTestResultsProvider.getInstance(applicationSettings);
                    boolean isGherkinTestsExist = gherkinTestResultsProvider.createTestList(project, job, gitLabApi.getJobApi().downloadArtifactsFile(projectId, job.getId()));
                    // looking for Regular tests
                    if (!isGherkinTestsExist) {
                        JunitTestResultsProvider testResultsProduce = JunitTestResultsProvider.getInstance(applicationSettings);
                        boolean testResultsExist = testResultsProduce.createTestList(project, job, gitLabApi.getJobApi().downloadArtifactsFile(projectId, job.getId()));
                        if (!testResultsExist) {
                            String warning = String.format("No test results found by using the %s pattern", applicationSettings.getConfig().getGitlabTestResultsFilePattern());
                            log.warn(warning);
                            return Response.ok().entity(warning).build();
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        log.warn("An error occurred while handling GitLab event", e);
    }
    log.traceExit();
    return Response.ok().build();
}
Also used : CIEvent(com.hp.octane.integrations.dto.events.CIEvent) ParsedPath(com.microfocus.octane.gitlab.helpers.ParsedPath) GherkinTestResultsProvider(com.microfocus.octane.gitlab.testresults.GherkinTestResultsProvider) JSONArray(org.json.JSONArray) GitLabApiException(org.gitlab4j.api.GitLabApiException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JunitTestResultsProvider(com.microfocus.octane.gitlab.testresults.JunitTestResultsProvider) CIEventType(com.hp.octane.integrations.dto.events.CIEventType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CIParameter(com.hp.octane.integrations.dto.parameters.CIParameter)

Aggregations

JunitTestResultsProvider (com.microfocus.octane.gitlab.testresults.JunitTestResultsProvider)2 GitLabApiException (org.gitlab4j.api.GitLabApiException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CIEvent (com.hp.octane.integrations.dto.events.CIEvent)1 CIEventType (com.hp.octane.integrations.dto.events.CIEventType)1 CIParameter (com.hp.octane.integrations.dto.parameters.CIParameter)1 PermissionException (com.hp.octane.integrations.exceptions.PermissionException)1 ParsedPath (com.microfocus.octane.gitlab.helpers.ParsedPath)1 GherkinTestResultsProvider (com.microfocus.octane.gitlab.testresults.GherkinTestResultsProvider)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 Job (org.gitlab4j.api.models.Job)1 JSONArray (org.json.JSONArray)1