Search in sources :

Example 1 with Job

use of org.gitlab4j.api.models.Job 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 Job

use of org.gitlab4j.api.models.Job in project choerodon-starters by open-hand.

the class JobApi method getJobs.

/**
 * Get a list of jobs in a project.
 * <p>
 * GET /projects/:id/jobs
 *
 * @param projectId the project ID to get the list of jobs for
 * @param scope     the scope of jobs, one of: CREATED, PENDING, RUNNING, FAILED, SUCCESS, CANCELED, SKIPPED, MANUAL
 * @return a list containing the jobs for the specified project ID
 * @throws GitLabApiException if any exception occurs during execution
 */
public List<Job> getJobs(int projectId, JobScope scope) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("scope", scope).withParam(PER_PAGE_PARAM, getDefaultPerPage());
    Response response = get(Status.OK, formData.asMap(), "projects", projectId, "jobs");
    return (response.readEntity(new GenericType<List<Job>>() {
    }));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Job(org.gitlab4j.api.models.Job)

Example 3 with Job

use of org.gitlab4j.api.models.Job in project choerodon-starters by open-hand.

the class JobApi method eraseJob.

/**
 * Erase specified job in a project.
 * <p>
 * POST /projects/:id/jobs/:job_id/erase
 *
 * @param projectId the project ID to erase specified job
 * @param jobId     the ID to erase job
 * @return job instance which just erased
 * @throws GitLabApiException if any exception occurs during execution
 */
public Job eraseJob(int projectId, int jobId) throws GitLabApiException {
    GitLabApiForm formData = null;
    Response response = post(Status.CREATED, formData, "projects", projectId, "jobs", jobId, "erase");
    return (response.readEntity(Job.class));
}
Also used : Response(javax.ws.rs.core.Response) Job(org.gitlab4j.api.models.Job)

Example 4 with Job

use of org.gitlab4j.api.models.Job in project octane-gitlab-service by MicroFocus.

the class OctaneServices method getJobBuildStatus.

@Override
public CIBuildStatusInfo getJobBuildStatus(String jobCiId, String parameterName, String parameterValue) {
    ParsedPath parsedPath = new ParsedPath(jobCiId, gitLabApi, PathType.PIPELINE);
    try {
        List<Pipeline> pipelines = gitLabApi.getPipelineApi().getPipelines(parsedPath.getPathWithNameSpace());
        Optional<Pipeline> chosenPipeline = pipelines.stream().map(pipeline -> {
            try {
                List<Variable> pipelineVariables = gitLabApi.getPipelineApi().getPipelineVariables(parsedPath.getPathWithNameSpace(), pipeline.getId());
                for (Variable variable : pipelineVariables) {
                    if (variable.getKey().equals(parameterName) && variable.getValue().equals(parameterValue)) {
                        return pipeline;
                    }
                }
                return null;
            } catch (GitLabApiException e) {
                log.error("Failed to get variables from pipeline", e);
                throw new RuntimeException(e);
            }
        }).filter(Objects::nonNull).findAny();
        if (chosenPipeline.isPresent()) {
            String status = chosenPipeline.get().getStatus().toValue();
            CIBuildStatus currentCIBuildStatus = getCIBuildStatus(status);
            Optional<CIBuildStatus> buildStatus = Arrays.stream(CIBuildStatus.values()).filter(ciBuildStatus -> Objects.equals(ciBuildStatus, currentCIBuildStatus)).findAny();
            if (!buildStatus.isPresent()) {
                throw new RuntimeException("Failed to get the correct build status");
            }
            return dtoFactory.newDTO(CIBuildStatusInfo.class).setJobCiId(jobCiId).setBuildStatus(buildStatus.get()).setBuildCiId(getBuildCiId(chosenPipeline.get())).setParamName(parameterName).setParamValue(parameterValue).setResult(getCiBuildResult(status));
        }
        throw new RuntimeException("Failed to get information about the pipeline");
    } catch (GitLabApiException e) {
        log.error("Failed to get job build status of the pipeline run", e);
        throw new RuntimeException(e);
    }
}
Also used : java.util(java.util) com.hp.octane.integrations.dto.tests(com.hp.octane.integrations.dto.tests) CIBuildStatus(com.hp.octane.integrations.dto.snapshots.CIBuildStatus) Branch(org.gitlab4j.api.models.Branch) Job(org.gitlab4j.api.models.Job) URL(java.net.URL) EncodeCiJobBase64Parameter(com.hp.octane.integrations.services.configurationparameters.EncodeCiJobBase64Parameter) HttpStatus(org.apache.http.HttpStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) Scope(org.springframework.context.annotation.Scope) Variable(org.gitlab4j.api.models.Variable) ConfigStructure(com.microfocus.octane.gitlab.model.ConfigStructure) TestResultsHelper(com.microfocus.octane.gitlab.helpers.TestResultsHelper) CIParameter(com.hp.octane.integrations.dto.parameters.CIParameter) DTOFactory(com.hp.octane.integrations.dto.DTOFactory) PipelineNode(com.hp.octane.integrations.dto.pipelines.PipelineNode) CIBuildResult(com.hp.octane.integrations.dto.snapshots.CIBuildResult) CIBuildStatusInfo(com.hp.octane.integrations.dto.general.CIBuildStatusInfo) PermissionException(com.hp.octane.integrations.exceptions.PermissionException) PREFIX(com.microfocus.octane.gitlab.helpers.PasswordEncryption.PREFIX) CIServerInfo(com.hp.octane.integrations.dto.general.CIServerInfo) CIPluginServices(com.hp.octane.integrations.CIPluginServices) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) JunitTestResultsProvider(com.microfocus.octane.gitlab.testresults.JunitTestResultsProvider) Application(com.microfocus.octane.gitlab.app.Application) Pipeline(org.gitlab4j.api.models.Pipeline) ApplicationSettings(com.microfocus.octane.gitlab.app.ApplicationSettings) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) CIPluginInfo(com.hp.octane.integrations.dto.general.CIPluginInfo) Component(org.springframework.stereotype.Component) Logger(org.apache.logging.log4j.Logger) OctaneConfiguration(com.hp.octane.integrations.OctaneConfiguration) CIParameters(com.hp.octane.integrations.dto.parameters.CIParameters) java.io(java.io) CIJobsList(com.hp.octane.integrations.dto.general.CIJobsList) GitLabApiException(org.gitlab4j.api.GitLabApiException) com.microfocus.octane.gitlab.helpers(com.microfocus.octane.gitlab.helpers) ConfigurationParameterFactory(com.hp.octane.integrations.services.configurationparameters.factory.ConfigurationParameterFactory) LogManager(org.apache.logging.log4j.LogManager) CIProxyConfiguration(com.hp.octane.integrations.dto.configuration.CIProxyConfiguration) GitLabApi(org.gitlab4j.api.GitLabApi) CIBuildStatus(com.hp.octane.integrations.dto.snapshots.CIBuildStatus) Variable(org.gitlab4j.api.models.Variable) GitLabApiException(org.gitlab4j.api.GitLabApiException) Pipeline(org.gitlab4j.api.models.Pipeline)

Example 5 with Job

use of org.gitlab4j.api.models.Job in project choerodon-starters by open-hand.

the class JobApi method getJobsForPipeline.

/**
 * Get a list of jobs in a pipeline.
 * <p>
 * GET /projects/:id/pipelines/:pipeline_id/jobs
 *
 * @param projectId  the project ID to get the list of pipeline for
 * @param pipelineId the pipeline ID to get the list of jobs for
 * @param scope      the scope of jobs, one of: CREATED, PENDING, RUNNING, FAILED, SUCCESS, CANCELED, SKIPPED, MANUAL
 * @return a list containing the jobs for the specified project ID and pipeline ID
 * @throws GitLabApiException if any exception occurs during execution
 */
public List<Job> getJobsForPipeline(int projectId, int pipelineId, JobScope scope) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("scope", scope).withParam(PER_PAGE_PARAM, getDefaultPerPage());
    Response response = get(Status.OK, formData.asMap(), "projects", projectId, "pipelines", pipelineId, "jobs");
    return (response.readEntity(new GenericType<List<Job>>() {
    }));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Job(org.gitlab4j.api.models.Job)

Aggregations

Job (org.gitlab4j.api.models.Job)8 Response (javax.ws.rs.core.Response)6 PermissionException (com.hp.octane.integrations.exceptions.PermissionException)2 JunitTestResultsProvider (com.microfocus.octane.gitlab.testresults.JunitTestResultsProvider)2 GenericType (javax.ws.rs.core.GenericType)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 GitLabApiException (org.gitlab4j.api.GitLabApiException)2 CIPluginServices (com.hp.octane.integrations.CIPluginServices)1 OctaneConfiguration (com.hp.octane.integrations.OctaneConfiguration)1 DTOFactory (com.hp.octane.integrations.dto.DTOFactory)1 CIProxyConfiguration (com.hp.octane.integrations.dto.configuration.CIProxyConfiguration)1 CIBuildStatusInfo (com.hp.octane.integrations.dto.general.CIBuildStatusInfo)1 CIJobsList (com.hp.octane.integrations.dto.general.CIJobsList)1 CIPluginInfo (com.hp.octane.integrations.dto.general.CIPluginInfo)1 CIServerInfo (com.hp.octane.integrations.dto.general.CIServerInfo)1 CIParameter (com.hp.octane.integrations.dto.parameters.CIParameter)1 CIParameters (com.hp.octane.integrations.dto.parameters.CIParameters)1 PipelineNode (com.hp.octane.integrations.dto.pipelines.PipelineNode)1 CIBuildResult (com.hp.octane.integrations.dto.snapshots.CIBuildResult)1 CIBuildStatus (com.hp.octane.integrations.dto.snapshots.CIBuildStatus)1