Search in sources :

Example 1 with Variable

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

the class MergeRequestHistoryHandler method executeFirstScan.

public void executeFirstScan() {
    try {
        List<Project> gitLabProjects = gitLabApi.getProjectApi().getProjects().stream().filter(project -> {
            Map<String, String> projectGroupVariables = VariablesHelper.getProjectGroupVariables(gitLabApi, project);
            Optional<Variable> shouldPublishToOctane = VariablesHelper.getProjectVariable(gitLabApi, project.getId(), applicationSettings.getConfig().getPublishMergeRequestsVariableName());
            return (shouldPublishToOctane.isPresent() && Boolean.parseBoolean(shouldPublishToOctane.get().getValue())) || (projectGroupVariables.containsKey(applicationSettings.getConfig().getPublishMergeRequestsVariableName()) && Boolean.parseBoolean(projectGroupVariables.get(applicationSettings.getConfig().getPublishMergeRequestsVariableName())));
        }).collect(Collectors.toList());
        gitLabProjects.forEach(project -> {
            try {
                Path pathToFile = Paths.get(watchPath.toString() + "/" + project.getId());
                if (!Files.exists(pathToFile)) {
                    sendMergeRequestsToOctane(project);
                    Files.createFile(pathToFile);
                }
            } catch (GitLabApiException | IOException e) {
                log.warn(e.getMessage(), e);
            }
        });
    } catch (GitLabApiException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) DependsOn(org.springframework.context.annotation.DependsOn) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WatchKey(java.nio.file.WatchKey) Variable(org.gitlab4j.api.models.Variable) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) PullRequestHelper(com.microfocus.octane.gitlab.helpers.PullRequestHelper) Map(java.util.Map) MergeRequest(org.gitlab4j.api.models.MergeRequest) TaskExecutor(org.springframework.core.task.TaskExecutor) Path(java.nio.file.Path) Commit(org.gitlab4j.api.models.Commit) Files(java.nio.file.Files) VariablesHelper(com.microfocus.octane.gitlab.helpers.VariablesHelper) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException) ApplicationSettings(com.microfocus.octane.gitlab.app.ApplicationSettings) GitLabApiWrapper(com.microfocus.octane.gitlab.helpers.GitLabApiWrapper) Project(org.gitlab4j.api.models.Project) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) WatchService(java.nio.file.WatchService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Diff(org.gitlab4j.api.models.Diff) Paths(java.nio.file.Paths) Optional(java.util.Optional) GitLabApiException(org.gitlab4j.api.GitLabApiException) LogManager(org.apache.logging.log4j.LogManager) GitLabApi(org.gitlab4j.api.GitLabApi) FileSystems(java.nio.file.FileSystems) Path(java.nio.file.Path) Project(org.gitlab4j.api.models.Project) Optional(java.util.Optional) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Variable

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

the class VariablesHelper method convertJSONArrayToVariables.

public static List<Variable> convertJSONArrayToVariables(JSONArray jsonVariablesList) {
    List<Variable> variableList = new ArrayList<>();
    jsonVariablesList.forEach(variable -> {
        Variable var = new Variable();
        var.setKey(((JSONObject) variable).getString("key"));
        var.setValue(((JSONObject) variable).getString("value"));
        // var.setProtected(((JSONObject) variable).getString("protected"));
        variableList.add(var);
    });
    return variableList;
}
Also used : Variable(org.gitlab4j.api.models.Variable) ArrayList(java.util.ArrayList)

Example 3 with Variable

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

the class PipelineApi method createPipelineScheduleVariable.

/**
 * Create a pipeline schedule variable.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/pipeline_schedules/:pipeline_schedule_id/variables</code></pre>
 *
 * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
 * @param pipelineScheduleId the pipelineSchedule ID
 * @param key the key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed
 * @param value the value for the variable
 * @return a Pipeline instance with the newly created pipeline schedule variable
 * @throws GitLabApiException if any exception occurs during execution
 */
public Variable createPipelineScheduleVariable(Object projectIdOrPath, Integer pipelineScheduleId, String key, String value) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("key", key, true).withParam("value", value, true);
    Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline_schedules", pipelineScheduleId, "variables");
    return (response.readEntity(Variable.class));
}
Also used : Response(javax.ws.rs.core.Response) Variable(org.gitlab4j.api.models.Variable)

Example 4 with Variable

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

the class GitlabServices method getParameters.

public List<CIParameter> getParameters(ParsedPath project) {
    List<CIParameter> parametersList = new ArrayList<>();
    List<Variable> projectVariables = VariablesHelper.getVariables(project, gitLabApi, applicationSettings.getConfig());
    projectVariables.forEach(var -> {
        CIParameter param = dtoFactory.newDTO(CIParameter.class);
        param.setType(CIParameterType.STRING);
        param.setName(var.getKey());
        param.setDefaultValue(var.getValue());
        parametersList.add(param);
    });
    return parametersList;
}
Also used : Variable(org.gitlab4j.api.models.Variable) ArrayList(java.util.ArrayList) CIParameter(com.hp.octane.integrations.dto.parameters.CIParameter)

Example 5 with Variable

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

Aggregations

Variable (org.gitlab4j.api.models.Variable)7 ArrayList (java.util.ArrayList)4 Response (javax.ws.rs.core.Response)3 CIParameter (com.hp.octane.integrations.dto.parameters.CIParameter)2 ApplicationSettings (com.microfocus.octane.gitlab.app.ApplicationSettings)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 GitLabApi (org.gitlab4j.api.GitLabApi)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