Search in sources :

Example 11 with GitClientException

use of com.epam.pipeline.exception.git.GitClientException in project cloud-pipeline by epam.

the class GitlabClient method getCommits.

public List<GitCommitEntry> getCommits(String refName, Date since, Date until) throws GitClientException {
    try {
        String projectId = makeProjectId(namespace, projectName);
        Map<String, Object> params = new HashMap<>();
        if (refName != null) {
            params.put("ref_name", refName);
        }
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(tz);
        if (since != null) {
            params.put("since", df.format(since));
        }
        if (until != null) {
            params.put("until", df.format(until));
        }
        String url = addUrlParameters(String.format(GIT_COMMITS, gitHost, projectId), params);
        URI uri = new URI(url);
        LOGGER.trace("Getting repository commits from URL: {}", uri);
        RestTemplate template = new RestTemplate();
        ResponseEntity<List<GitCommitEntry>> sourcesResponse = template.exchange(uri, HttpMethod.GET, getAuthHeaders(), new ParameterizedTypeReference<List<GitCommitEntry>>() {
        });
        if (sourcesResponse.getStatusCode() == HttpStatus.OK) {
            return sourcesResponse.getBody();
        } else {
            throw new UnexpectedResponseStatusException(sourcesResponse.getStatusCode());
        }
    } catch (UnsupportedEncodingException | URISyntaxException | HttpClientErrorException e) {
        throw new GitClientException(e.getMessage(), e);
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TimeZone(java.util.TimeZone) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) RestTemplate(org.springframework.web.client.RestTemplate) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with GitClientException

use of com.epam.pipeline.exception.git.GitClientException in project cloud-pipeline by epam.

the class GitlabClient method getVersion.

/**
 * Loads Gitlab version information
 * @return a GitlabVersion object
 * @throws GitClientException
 */
public GitlabVersion getVersion() throws GitClientException {
    try {
        URI uri = new URI(String.format(GITLAB_VERSION_URL, gitHost));
        ResponseEntity<GitlabVersion> versionResponse = new RestTemplate().exchange(uri, HttpMethod.GET, getAuthHeaders(), GitlabVersion.class);
        if (versionResponse.getStatusCode() == HttpStatus.OK) {
            return versionResponse.getBody();
        } else {
            throw new UnexpectedResponseStatusException(versionResponse.getStatusCode());
        }
    } catch (URISyntaxException e) {
        throw new GitClientException(e.getMessage(), e);
    }
}
Also used : GitlabVersion(com.epam.pipeline.entity.git.GitlabVersion) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) RestTemplate(org.springframework.web.client.RestTemplate) GitClientException(com.epam.pipeline.exception.git.GitClientException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 13 with GitClientException

use of com.epam.pipeline.exception.git.GitClientException in project cloud-pipeline by epam.

the class InstanceOfferManager method getInstanceEstimatedPrice.

public InstancePrice getInstanceEstimatedPrice(Long id, String version, String configName, String instanceType, int instanceDisk, Boolean spot, Long regionId) throws GitClientException {
    Boolean useSpot = spot;
    if (StringUtils.isEmpty(instanceType) || instanceDisk <= 0 || useSpot == null) {
        PipelineConfiguration pipelineConfiguration = versionManager.loadParametersFromScript(id, version, configName);
        if (StringUtils.isEmpty(instanceType)) {
            instanceType = pipelineConfiguration.getInstanceType();
        }
        if (instanceDisk <= 0) {
            instanceDisk = Integer.parseInt(pipelineConfiguration.getInstanceDisk());
        }
        if (useSpot == null) {
            useSpot = pipelineConfiguration.getIsSpot();
        }
    }
    Assert.isTrue(isInstanceAllowed(instanceType), messageHelper.getMessage(MessageConstants.ERROR_INSTANCE_TYPE_IS_NOT_ALLOWED, instanceType));
    AwsRegion region = awsRegionManager.loadRegionOrGetDefault(regionId);
    double pricePerHourForInstance = getPricePerHourForInstance(instanceType, isSpotRequest(useSpot), region.getAwsRegionName());
    double pricePerDisk = getPriceForDisk(instanceDisk, region.getAwsRegionName());
    double pricePerHour = pricePerDisk + pricePerHourForInstance;
    InstancePrice instancePrice = new InstancePrice(instanceType, instanceDisk, pricePerHour);
    List<PipelineRun> runs = pipelineRunManager.loadAllRunsByPipeline(id, version).stream().filter(run -> run.getStatus().isFinal()).collect(Collectors.toList());
    if (!runs.isEmpty()) {
        long minimumDuration = -1;
        long maximumDuration = -1;
        long totalDurations = 0;
        for (PipelineRun run : runs) {
            long duration = run.getEndDate().getTime() - run.getStartDate().getTime();
            if (minimumDuration == -1 || minimumDuration > duration) {
                minimumDuration = duration;
            }
            if (maximumDuration == -1 || maximumDuration < duration) {
                maximumDuration = duration;
            }
            totalDurations += duration;
        }
        double averageDuration = (double) totalDurations / runs.size();
        instancePrice.setAverageTimePrice(pricePerHour * averageDuration / ONE_HOUR);
        instancePrice.setMinimumTimePrice(pricePerHour * minimumDuration / ONE_HOUR);
        instancePrice.setMaximumTimePrice(pricePerHour * maximumDuration / ONE_HOUR);
    }
    return instancePrice;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Arrays(java.util.Arrays) InstanceType(com.epam.pipeline.entity.cluster.InstanceType) URL(java.net.URL) Date(java.util.Date) BehaviorSubject(io.reactivex.subjects.BehaviorSubject) LoggerFactory(org.slf4j.LoggerFactory) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) MessageHelper(com.epam.pipeline.common.MessageHelper) ContextualPreferenceManager(com.epam.pipeline.manager.contextual.ContextualPreferenceManager) ListUtils(org.apache.commons.collections4.ListUtils) AntPathMatcher(org.springframework.util.AntPathMatcher) AllowedInstanceAndPriceTypes(com.epam.pipeline.entity.cluster.AllowedInstanceAndPriceTypes) InstancePrice(com.epam.pipeline.entity.cluster.InstancePrice) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Set(java.util.Set) AwsRegion(com.epam.pipeline.entity.region.AwsRegion) Collectors(java.util.stream.Collectors) ContextualPreferenceLevel(com.epam.pipeline.entity.contextual.ContextualPreferenceLevel) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) MessageConstants(com.epam.pipeline.common.MessageConstants) GitClientException(com.epam.pipeline.exception.git.GitClientException) PipelineRunManager(com.epam.pipeline.manager.pipeline.PipelineRunManager) PipelineRunPrice(com.epam.pipeline.entity.cluster.PipelineRunPrice) AtomicReference(java.util.concurrent.atomic.AtomicReference) PipelineVersionManager(com.epam.pipeline.manager.pipeline.PipelineVersionManager) ContextualPreferenceExternalResource(com.epam.pipeline.entity.contextual.ContextualPreferenceExternalResource) InstanceOffer(com.epam.pipeline.entity.cluster.InstanceOffer) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) Observable(io.reactivex.Observable) Subject(io.reactivex.subjects.Subject) AbstractSystemPreference(com.epam.pipeline.manager.preference.AbstractSystemPreference) InstanceOfferRequestVO(com.epam.pipeline.controller.vo.InstanceOfferRequestVO) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Logger(org.slf4j.Logger) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) InstanceOfferDao(com.epam.pipeline.dao.cluster.InstanceOfferDao) BufferedReader(java.io.BufferedReader) AllArgsConstructor(lombok.AllArgsConstructor) Collections(java.util.Collections) AwsRegionManager(com.epam.pipeline.manager.region.AwsRegionManager) NoArgsConstructor(lombok.NoArgsConstructor) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) AwsRegion(com.epam.pipeline.entity.region.AwsRegion) InstancePrice(com.epam.pipeline.entity.cluster.InstancePrice) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration)

Example 14 with GitClientException

use of com.epam.pipeline.exception.git.GitClientException in project cloud-pipeline by epam.

the class GitlabClient method createRepositoryRevision.

public GitTagEntry createRepositoryRevision(String name, String ref, String message, String releaseDescription) throws GitClientException {
    if (name == null) {
        throw new GitClientException("Tag name is required");
    }
    if (ref == null) {
        throw new GitClientException("Ref (commit SHA, another tag name, or branch name) is required");
    }
    try {
        String projectId = makeProjectId(namespace, projectName);
        Map<String, Object> params = new HashMap<>();
        params.put("tag_name", name);
        params.put("ref", ref);
        if (message != null) {
            params.put("message", message);
        }
        if (releaseDescription != null) {
            params.put("release_description", releaseDescription);
        }
        String url = addUrlParameters(String.format(GIT_REVISIONS, gitHost, projectId), params);
        URI uri = new URI(url);
        LOGGER.trace("Creating new tag using URL: {}", uri);
        RestTemplate template = new RestTemplate();
        ResponseEntity<GitTagEntry> sourcesResponse = template.exchange(uri, HttpMethod.POST, getAuthHeaders(), new ParameterizedTypeReference<GitTagEntry>() {
        });
        if (sourcesResponse.getStatusCode() == HttpStatus.CREATED) {
            return sourcesResponse.getBody();
        } else {
            throw new UnexpectedResponseStatusException(sourcesResponse.getStatusCode());
        }
    } catch (UnsupportedEncodingException | URISyntaxException e) {
        throw new GitClientException(e);
    }
}
Also used : HashMap(java.util.HashMap) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RestTemplate(org.springframework.web.client.RestTemplate) GitTagEntry(com.epam.pipeline.entity.git.GitTagEntry)

Example 15 with GitClientException

use of com.epam.pipeline.exception.git.GitClientException in project cloud-pipeline by epam.

the class GitlabClient method getRepositoryContents.

/**
 * Retrieves repository contents - a list of files in a repository
 * @param path a path in a repository to browse
 * @return a list of {@link GitRepositoryEntry}, representing files and folders
 * @throws GitClientException if something goes wrong
 */
public List<GitRepositoryEntry> getRepositoryContents(String path, String revision, boolean recursive) throws GitClientException {
    try {
        String projectId = makeProjectId(namespace, projectName);
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(path)) {
            params.put("path", path);
        }
        if (StringUtils.isNotBlank(revision)) {
            params.put("ref_name", revision);
        }
        params.put("recursive", recursive);
        String url = addUrlParameters(String.format(GIT_GET_SOURCES_ROOT_URL, gitHost, projectId), params);
        URI uri = new URI(url);
        LOGGER.trace("Getting repository contents on path {}, URL: {}", path, uri);
        RestTemplate template = new RestTemplate();
        ResponseEntity<List<GitRepositoryEntry>> sourcesResponse = template.exchange(uri, HttpMethod.GET, getAuthHeaders(), new ParameterizedTypeReference<List<GitRepositoryEntry>>() {
        });
        if (sourcesResponse.getStatusCode() == HttpStatus.OK) {
            return sourcesResponse.getBody();
        } else {
            throw new UnexpectedResponseStatusException(sourcesResponse.getStatusCode());
        }
    } catch (UnsupportedEncodingException | URISyntaxException e) {
        throw new GitClientException(e);
    }
}
Also used : HashMap(java.util.HashMap) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RestTemplate(org.springframework.web.client.RestTemplate) List(java.util.List)

Aggregations

GitClientException (com.epam.pipeline.exception.git.GitClientException)23 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 URISyntaxException (java.net.URISyntaxException)11 UnexpectedResponseStatusException (com.epam.pipeline.exception.git.UnexpectedResponseStatusException)10 URI (java.net.URI)10 RestTemplate (org.springframework.web.client.RestTemplate)10 List (java.util.List)7 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)7 HashMap (java.util.HashMap)6 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)5 IOException (java.io.IOException)5 Date (java.util.Date)5 Autowired (org.springframework.beans.factory.annotation.Autowired)4 PipelineConfiguration (com.epam.pipeline.entity.configuration.PipelineConfiguration)3 GitCommitEntry (com.epam.pipeline.entity.git.GitCommitEntry)3 GitProject (com.epam.pipeline.entity.git.GitProject)3 GitTagEntry (com.epam.pipeline.entity.git.GitTagEntry)3 Revision (com.epam.pipeline.entity.pipeline.Revision)3 RunInstance (com.epam.pipeline.entity.pipeline.RunInstance)3 TaskGraphVO (com.epam.pipeline.controller.vo.TaskGraphVO)2