All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clarolab.bamboo.client.BambooProjectClient Maven / Gradle / Ivy

package com.clarolab.bamboo.client;

import com.clarolab.bamboo.entities.BambooMain;
import com.clarolab.bamboo.entities.BambooProject;
import com.clarolab.bamboo.utils.Constants;
import com.google.common.collect.Lists;
import lombok.Builder;
import lombok.Data;
import lombok.extern.java.Log;

import java.util.List;

@Data
@Log
public class BambooProjectClient extends BambooClient{

    @Builder
    public BambooProjectClient(BambooApiClient bambooApiClient){
        super(bambooApiClient, Constants.AMOUNT_OF_ELEMENTS_TO_GET);
    }

    public int getProjectsSize() throws Exception {
        return perform(Constants.BAMBOO_PROJECT_SIZE_ENDPOINT, BambooMain.class).getProjectsSize();
    }

    public List getProjects() throws Exception {
        int start = 0;
        List bambooProjects = Lists.newArrayList();
        BambooMain bambooMain = perform(String.format(Constants.BAMBOO_PROJECT_ENDPOINT_LIMITED, start, getLimitResults()), BambooMain.class);
        List bambooProjectsFromRequest = bambooMain.getProjects();
        while(bambooProjects.size() != bambooMain.getProjectsSize()){
            bambooProjects.addAll(bambooProjectsFromRequest);
            start += getLimitResults();
            bambooProjectsFromRequest = perform(String.format(Constants.BAMBOO_PROJECT_ENDPOINT_LIMITED, start, getLimitResults()), BambooMain.class).getProjects();
        }

        return bambooProjects.isEmpty() ? null : bambooProjects;
    }

    public BambooProject getProjectFromName(String projectName) throws Exception {
        return filterProjectByParameter("name", projectName);
    }

    public BambooProject getProjectFromKey(String projectKey) throws Exception {
        return filterProjectByParameter("key", projectKey);
    }

    /**
     *
     * @param projectUrl    url should have the format {bamboo_server}/browse/{project_key}
     * @return BambooProject
     *
    */
    public BambooProject getProjectFromUrl(String projectUrl) throws Exception {
        String projectKey = projectUrl.substring(projectUrl.lastIndexOf("/")+1);
        log.info("Looking for project with key: " + projectKey);

        return getProjectFromKey(projectKey);
    }

    private BambooProject filterProjectByParameter(String key, String value) throws Exception {
        int start = 0;
        BambooMain bambooMain = perform(String.format(Constants.BAMBOO_PROJECT_ENDPOINT_LIMITED, start, getLimitResults()), BambooMain.class);
        BambooProject bambooProject = key.equals("name") ? bambooMain.getProjects().stream().filter(project -> project.getName().equals(value)).findFirst().orElse(null) :
                                                           bambooMain.getProjects().stream().filter(project -> project.getKey().equals(value)).findFirst().orElse(null);
        while(bambooProject == null && !bambooMain.projectsLimitReached()){
            start += getLimitResults();
            bambooMain = perform(String.format(Constants.BAMBOO_PROJECT_ENDPOINT_LIMITED, start, getLimitResults()), BambooMain.class);
            bambooProject = key.equals("name") ? bambooMain.getProjects().stream().filter(project -> project.getName().equals(value)).findFirst().orElse(null) :
                                                 bambooMain.getProjects().stream().filter(project -> project.getKey().equals(value)).findFirst().orElse(null);
        }

        if(bambooProject != null)
            return bambooProject;

        // If limit is reached, last result is not evaluated in loop
        if(bambooMain.projectsLimitReached())
            return key.equals("name") ? bambooMain.getProjects().stream().filter(project -> project.getName().equals(value)).findFirst().orElse(null) :
                                        bambooMain.getProjects().stream().filter(project -> project.getKey().equals(value)).findFirst().orElse(null);

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy