Search in sources :

Example 1 with ConfigStructure

use of com.microfocus.octane.gitlab.model.ConfigStructure in project octane-gitlab-service by MicroFocus.

the class OctaneServices method getProxyConfiguration.

// @Override
// public File getAllowedOctaneStorage() {
// File sdkStorage = new File("sdk_storage");
// boolean available = sdkStorage.exists();
// if (!available) {
// available = sdkStorage.mkdirs();
// }
// return available ? sdkStorage : null;
// }
@Override
public CIProxyConfiguration getProxyConfiguration(URL targetUrl) {
    try {
        CIProxyConfiguration result = null;
        if (ProxyHelper.isProxyNeeded(applicationSettings, targetUrl)) {
            log.debug("proxy is required for host " + targetUrl);
            ConfigStructure config = applicationSettings.getConfig();
            String protocol = targetUrl.getProtocol();
            URL proxyUrl = new URL(config.getProxyField(protocol, "proxyUrl"));
            String proxyPassword = config.getProxyField(protocol, "proxyPassword");
            if (proxyPassword != null && proxyPassword.startsWith(PREFIX)) {
                try {
                    proxyPassword = PasswordEncryption.decrypt(proxyPassword.substring(PREFIX.length()));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            result = dtoFactory.newDTO(CIProxyConfiguration.class).setHost(proxyUrl.getHost()).setPort(proxyUrl.getPort()).setUsername(config.getProxyField(protocol, "proxyUser")).setPassword(proxyPassword);
        }
        return result;
    } catch (Exception e) {
        log.warn("Failed to return the proxy configuration, using null as default.", e);
        return null;
    }
}
Also used : CIProxyConfiguration(com.hp.octane.integrations.dto.configuration.CIProxyConfiguration) ConfigStructure(com.microfocus.octane.gitlab.model.ConfigStructure) URL(java.net.URL) PermissionException(com.hp.octane.integrations.exceptions.PermissionException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 2 with ConfigStructure

use of com.microfocus.octane.gitlab.model.ConfigStructure in project octane-gitlab-service by MicroFocus.

the class OctaneServices method getOctaneConfiguration.

/* @Override*/
public OctaneConfiguration getOctaneConfiguration() {
    OctaneConfiguration result = null;
    try {
        ConfigStructure config = applicationSettings.getConfig();
        if (config != null && config.getOctaneLocation() != null && !config.getOctaneLocation().isEmpty()) {
            String octaneApiClientSecret = config.getOctaneApiClientSecret();
            if (octaneApiClientSecret != null && octaneApiClientSecret.startsWith(PREFIX)) {
                try {
                    octaneApiClientSecret = PasswordEncryption.decrypt(octaneApiClientSecret.substring(PREFIX.length()));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            result = OctaneConfiguration.createWithUiLocation(config.getCiServerIdentity(), config.getOctaneLocation());
            result.setClient(config.getOctaneApiClientID());
            result.setSecret(octaneApiClientSecret);
            ConfigurationParameterFactory.addParameter(result, EncodeCiJobBase64Parameter.KEY, "true");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}
Also used : ConfigStructure(com.microfocus.octane.gitlab.model.ConfigStructure) OctaneConfiguration(com.hp.octane.integrations.OctaneConfiguration) PermissionException(com.hp.octane.integrations.exceptions.PermissionException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 3 with ConfigStructure

use of com.microfocus.octane.gitlab.model.ConfigStructure in project octane-gitlab-service by MicroFocus.

the class EventListener method handleMergeRequestEvent.

private Response handleMergeRequestEvent(JSONObject event) throws GitLabApiException {
    log.info("Merge Request event occurred.");
    ConfigStructure config = applicationSettings.getConfig();
    if (getMREventType(event).equals(MergeRequestEventType.UNKNOWN)) {
        String warning = "Unknown event on merge request has taken place!";
        log.warn(warning);
        return Response.ok().entity(warning).build();
    }
    Project project = gitLabApi.getProjectApi().getProject(event.getJSONObject("project").getInt("id"));
    Map<String, String> projectGroupVariables = VariablesHelper.getProjectGroupVariables(gitLabApi, project);
    Optional<Variable> publishMergeRequests = VariablesHelper.getProjectVariable(gitLabApi, project.getId(), config.getPublishMergeRequestsVariableName());
    if (((publishMergeRequests.isEmpty() || !Boolean.parseBoolean(publishMergeRequests.get().getValue())) && (!projectGroupVariables.containsKey(config.getPublishMergeRequestsVariableName()) || !Boolean.parseBoolean(projectGroupVariables.get(config.getPublishMergeRequestsVariableName()))))) {
        return Response.ok().build();
    }
    Optional<Variable> destinationWSVar = VariablesHelper.getProjectVariable(gitLabApi, project.getId(), config.getDestinationWorkspaceVariableName());
    String destinationWS;
    if (destinationWSVar.isEmpty() && !projectGroupVariables.containsKey(config.getDestinationWorkspaceVariableName())) {
        String err = "Variable for destination workspace has not been set for project with id" + project.getId();
        log.error(err);
        return Response.ok().entity(err).build();
    } else if (destinationWSVar.isPresent()) {
        destinationWS = destinationWSVar.get().getValue();
    } else {
        destinationWS = projectGroupVariables.get(config.getDestinationWorkspaceVariableName());
    }
    Optional<Variable> useSSHFormatVar = VariablesHelper.getProjectVariable(gitLabApi, project.getId(), config.getUseSSHFormatVariableName());
    boolean useSSHFormat = useSSHFormatVar.isPresent() && Boolean.parseBoolean(useSSHFormatVar.get().getValue()) || projectGroupVariables.containsKey(config.getUseSSHFormatVariableName()) && Boolean.parseBoolean(projectGroupVariables.get(config.getUseSSHFormatVariableName()));
    String repoUrl = useSSHFormat ? project.getSshUrlToRepo() : project.getHttpUrlToRepo();
    int mergeRequestId = getEventTargetObjectId(event);
    MergeRequest mergeRequest = gitLabApi.getMergeRequestApi().getMergeRequest(project.getId(), mergeRequestId);
    List<Commit> mergeRequestCommits = gitLabApi.getMergeRequestApi().getCommits(project.getId(), mergeRequest.getIid());
    Map<String, List<Diff>> mrCommitDiffs = new HashMap<>();
    mergeRequestCommits.forEach(commit -> {
        try {
            List<Diff> diffs = gitLabApi.getCommitsApi().getDiff(project.getId(), commit.getId());
            mrCommitDiffs.put(commit.getId(), diffs);
        } catch (GitLabApiException e) {
            log.warn(e.getMessage());
        }
    });
    PullRequestHelper.convertAndSendMergeRequestToOctane(mergeRequest, mergeRequestCommits, mrCommitDiffs, repoUrl, destinationWS);
    return Response.ok().build();
}
Also used : ConfigStructure(com.microfocus.octane.gitlab.model.ConfigStructure) GitLabApiException(org.gitlab4j.api.GitLabApiException) SCMCommit(com.hp.octane.integrations.dto.scm.SCMCommit)

Example 4 with ConfigStructure

use of com.microfocus.octane.gitlab.model.ConfigStructure in project octane-gitlab-service by MicroFocus.

the class GitLabApiWrapper method initGitlabApiWrapper.

@PostConstruct
public void initGitlabApiWrapper() throws MalformedURLException, GitLabApiException, ConfigurationException {
    ConfigStructure config = applicationSettings.getConfig();
    Map<String, Object> proxyConfig = null;
    URL targetUrl = CIPluginSDKUtils.parseURL(config.getGitlabLocation());
    if (ProxyHelper.isProxyNeeded(applicationSettings, targetUrl)) {
        String protocol = targetUrl.getProtocol().toLowerCase();
        String proxyUrl = config.getProxyField(protocol, "proxyUrl");
        String proxyPassword = config.getProxyField(protocol, "proxyPassword");
        if (proxyPassword != null && proxyPassword.startsWith(PREFIX)) {
            try {
                proxyPassword = PasswordEncryption.decrypt(proxyPassword.substring(PREFIX.length()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        proxyConfig = ProxyClientConfig.createProxyClientConfig(proxyUrl, config.getProxyField(protocol, "proxyUser"), proxyPassword);
    }
    String gitlabPersonalAccessToken = config.getGitlabPersonalAccessToken();
    if (gitlabPersonalAccessToken != null && gitlabPersonalAccessToken.startsWith(PREFIX)) {
        try {
            gitlabPersonalAccessToken = PasswordEncryption.decrypt(gitlabPersonalAccessToken.substring(PREFIX.length()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    gitLabApi = new GitLabApi(config.getGitlabLocation(), gitlabPersonalAccessToken, null, proxyConfig);
    try {
        gitLabApi.getProjectApi().getOwnedProjects();
    } catch (GitLabApiException e) {
        String message = "GitLab API failed to perform basic operations. Please validate GitLab properties - location, personalAccessToken(including token permissions/scopes in GitLab server)" + " if one of the end points doesnt required proxy, please put it on the non proxy hosts.";
        log.error(message);
        System.out.println(message);
        throw e;
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) ConfigStructure(com.microfocus.octane.gitlab.model.ConfigStructure) GitLabApiException(org.gitlab4j.api.GitLabApiException) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) ConfigurationException(javax.naming.ConfigurationException) GitLabApiException(org.gitlab4j.api.GitLabApiException) PostConstruct(javax.annotation.PostConstruct)

Example 5 with ConfigStructure

use of com.microfocus.octane.gitlab.model.ConfigStructure in project octane-gitlab-service by MicroFocus.

the class ProxyHelper method isProxyNeeded.

public static boolean isProxyNeeded(ApplicationSettings applicationSettings, URL targetHost) {
    if (targetHost == null)
        return false;
    boolean result = false;
    ConfigStructure config = applicationSettings.getConfig();
    if (config.getProxyField(targetHost.getProtocol(), "proxyUrl") != null) {
        String nonProxyHostsStr = config.getProxyField(targetHost.getProtocol(), "nonProxyHosts");
        if (!CIPluginSDKUtils.isNonProxyHost(targetHost.getHost(), nonProxyHostsStr)) {
            result = true;
        }
    }
    return result;
}
Also used : ConfigStructure(com.microfocus.octane.gitlab.model.ConfigStructure)

Aggregations

ConfigStructure (com.microfocus.octane.gitlab.model.ConfigStructure)5 GitLabApiException (org.gitlab4j.api.GitLabApiException)4 PermissionException (com.hp.octane.integrations.exceptions.PermissionException)2 URL (java.net.URL)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 OctaneConfiguration (com.hp.octane.integrations.OctaneConfiguration)1 CIProxyConfiguration (com.hp.octane.integrations.dto.configuration.CIProxyConfiguration)1 SCMCommit (com.hp.octane.integrations.dto.scm.SCMCommit)1 MalformedURLException (java.net.MalformedURLException)1 PostConstruct (javax.annotation.PostConstruct)1 ConfigurationException (javax.naming.ConfigurationException)1 GitLabApi (org.gitlab4j.api.GitLabApi)1