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;
}
}
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;
}
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();
}
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;
}
}
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;
}
Aggregations