diff --git a/source/src/main/java/org/cerberus/core/crud/entity/Application.java b/source/src/main/java/org/cerberus/core/crud/entity/Application.java index e9a22f086..d3f3b0d90 100644 --- a/source/src/main/java/org/cerberus/core/crud/entity/Application.java +++ b/source/src/main/java/org/cerberus/core/crud/entity/Application.java @@ -85,8 +85,9 @@ public Application(String application) { public static final String TYPE_FAT = "FAT"; public static final String TYPE_NONE = "NONE"; - public static final String BUGTRACKER_REDIRECT = "REDIRECT"; + public static final String BUGTRACKER_NONE = "NONE"; public static final String BUGTRACKER_JIRA = "JIRA"; + public static final String BUGTRACKER_GITHUB = "GITHUB"; public boolean hasSameKey(Application obj) { if (obj == null) { diff --git a/source/src/main/java/org/cerberus/core/crud/entity/Parameter.java b/source/src/main/java/org/cerberus/core/crud/entity/Parameter.java index e3b1bcd70..7dc43e9f8 100644 --- a/source/src/main/java/org/cerberus/core/crud/entity/Parameter.java +++ b/source/src/main/java/org/cerberus/core/crud/entity/Parameter.java @@ -94,9 +94,11 @@ public class Parameter { public static final String VALUE_cerberus_jiracloud_apiuser = "cerberus_jiracloud_apiuser"; public static final String VALUE_cerberus_jiracloud_apiuser_apitoken = "cerberus_jiracloud_apiuser_apitoken"; public static final String VALUE_cerberus_autobugcreation_enable = "cerberus_autobugcreation_enable"; + public static final String VALUE_cerberus_github_apitoken = "cerberus_github_apitoken"; public static final String SECUREDPARAMINSQLCLAUSE = "(\"cerberus_accountcreation_defaultpassword\",\"cerberus_proxyauthentification_password\",\"cerberus_jenkinsadmin_password\"," - + "\"cerberus_smtp_password\",\"cerberus_executeCerberusCommand_password\",\"cerberus_xraycloud_clientsecret\",\"cerberus_xraycloud_clientid\",\"cerberus_xraydc_token\",\"cerberus_jiracloud_apiuser_apitoken\")"; + + "\"cerberus_smtp_password\",\"cerberus_executeCerberusCommand_password\",\"cerberus_xraycloud_clientsecret\",\"cerberus_xraycloud_clientid\",\"cerberus_xraydc_token\"," + + "\"cerberus_jiracloud_apiuser_apitoken\",\"cerberus_github_apitoken\")"; public static final Integer CACHE_DURATION = 60; public static final Integer SHORT_CACHE_DURATION = 2; diff --git a/source/src/main/java/org/cerberus/core/engine/execution/impl/ExecutionRunService.java b/source/src/main/java/org/cerberus/core/engine/execution/impl/ExecutionRunService.java index d40617218..fe4dd0d3d 100644 --- a/source/src/main/java/org/cerberus/core/engine/execution/impl/ExecutionRunService.java +++ b/source/src/main/java/org/cerberus/core/engine/execution/impl/ExecutionRunService.java @@ -104,7 +104,7 @@ import java.util.List; import org.cerberus.core.engine.entity.Identifier; import org.cerberus.core.engine.execution.IIdentifierService; -import org.cerberus.core.service.jira.IJiraService; +import org.cerberus.core.service.bug.IBugService; import org.cerberus.core.service.robotextension.impl.SikuliService; import org.cerberus.core.service.xray.IXRayService; import org.cerberus.core.service.robotproxy.IRobotProxyService; @@ -157,7 +157,7 @@ public class ExecutionRunService implements IExecutionRunService { private IRobotProxyService executorService; private IEventService eventService; private IXRayService xRayService; - private IJiraService jiraService; + private IBugService bugService; private IIdentifierService identifierService; @Override @@ -911,7 +911,7 @@ startStep, startStep, startStep, startStep, new BigDecimal("0"), null, stepMess, // JIRA Issue creation Connector is triggered at the end of every execution.. // TODO Add conditions in order to create it only when testcase is stable enought. if (!willBeRetried) { - jiraService.createIssue(execution); + bugService.createIssue(execution); } /* diff --git a/source/src/main/java/org/cerberus/core/service/bug/IBugService.java b/source/src/main/java/org/cerberus/core/service/bug/IBugService.java new file mode 100644 index 000000000..ebd0ead6e --- /dev/null +++ b/source/src/main/java/org/cerberus/core/service/bug/IBugService.java @@ -0,0 +1,38 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.service.bug; + +import org.cerberus.core.crud.entity.TestCaseExecution; +import org.json.JSONArray; + +/** + * + * @author vertigo17 + */ +public interface IBugService { + + /** + * + * @param execution + */ + public void createIssue(TestCaseExecution execution); + + +} diff --git a/source/src/main/java/org/cerberus/core/service/bug/github/IGithubGenerationService.java b/source/src/main/java/org/cerberus/core/service/bug/github/IGithubGenerationService.java new file mode 100644 index 000000000..73acd0681 --- /dev/null +++ b/source/src/main/java/org/cerberus/core/service/bug/github/IGithubGenerationService.java @@ -0,0 +1,40 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.service.bug.github; + +import org.cerberus.core.crud.entity.TestCaseExecution; +import org.json.JSONObject; + +/** + * + * @author vertigo17 + */ +public interface IGithubGenerationService { + + /** + * + * @param issueTypeName + * @param execution + * @return + * @throws Exception + */ + public JSONObject generateGithubIssue(TestCaseExecution execution, String issueTypeName) throws Exception; + +} diff --git a/source/src/main/java/org/cerberus/core/service/bug/github/IGithubService.java b/source/src/main/java/org/cerberus/core/service/bug/github/IGithubService.java new file mode 100644 index 000000000..159705606 --- /dev/null +++ b/source/src/main/java/org/cerberus/core/service/bug/github/IGithubService.java @@ -0,0 +1,38 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.service.bug.github; + +import org.cerberus.core.crud.entity.TestCaseExecution; + +/** + * + * @author vertigo17 + */ +public interface IGithubService { + + /** + * + * @param execution + * @param repoName + * @param labelType + */ + public void createGithubIssue(TestCaseExecution execution, String repoName, String labelType); + +} diff --git a/source/src/main/java/org/cerberus/core/service/bug/github/impl/GithubGenerationService.java b/source/src/main/java/org/cerberus/core/service/bug/github/impl/GithubGenerationService.java new file mode 100644 index 000000000..f94dbe0f1 --- /dev/null +++ b/source/src/main/java/org/cerberus/core/service/bug/github/impl/GithubGenerationService.java @@ -0,0 +1,113 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.service.bug.github.impl; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.cerberus.core.crud.entity.TestCaseExecution; +import org.cerberus.core.crud.service.IParameterService; +import org.cerberus.core.crud.service.ITagService; +import org.cerberus.core.util.StringUtil; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.json.JSONArray; +import org.json.JSONException; +import org.cerberus.core.service.bug.github.IGithubGenerationService; + +/** + * + * @author vertigo17 + */ +@Service +public class GithubGenerationService implements IGithubGenerationService { + + private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(GithubGenerationService.class); + + @Autowired + private IParameterService parameterService; + @Autowired + private ITagService tagService; + + @Override + public JSONObject generateGithubIssue(TestCaseExecution execution, String LabelName) { + JSONObject issueObject = new JSONObject(); + + try { + String cerberusUrl = parameterService.getParameterStringByKey("cerberus_gui_url", "", ""); + if (StringUtil.isEmptyOrNull(cerberusUrl)) { + cerberusUrl = parameterService.getParameterStringByKey("cerberus_url", "", ""); + } + cerberusUrl = StringUtil.addSuffixIfNotAlready(cerberusUrl, "/"); + + String cerberusUrlExe = cerberusUrl + "TestCaseExecution.jsp?executionId=" + String.valueOf(execution.getId()); + String cerberusUrlTag = cerberusUrl + "ReportingExecutionByTag.jsp?Tag=" + URLEncoder.encode(execution.getTag(), "UTF-8"); + String cerberusUrlExeHisto = cerberusUrl + "ReportingExecutionOverTime.jsp?tests=" + URLEncoder.encode(execution.getTest(), "UTF-8") + "&testcases=" + URLEncoder.encode(execution.getTestCase(), "UTF-8"); + + issueObject.put("title", "TestCase Execution " + execution.getId() + " failed - " + execution.getTest() + " " + execution.getTestCase()); + + String body = "Test Case: **" + execution.getTest() + "** - **" + execution.getTestCase() + "** - " + execution.getDescription() + " \n\n"; + body += "Executed on " + execution.getEnvironment() + " - " + execution.getCountry() + "\n"; + body += "On " + convertToDate(execution.getEnd()) + "\n\n"; + body += "Ended with status " + execution.getControlStatus() + "\n **" + execution.getControlMessage() + "**\n\n"; + body += "Please check the detailed execution [" + execution.getId() + "](" + cerberusUrlExe + ") \n"; + body += "You can also access [latest executions](" + cerberusUrlExeHisto + ") perfomed on that testcase.\n\n"; + body += "Execution was triggered from campaign execution [" + execution.getTag() + "](" + cerberusUrlTag + ")"; + issueObject.put("body", body); + + if (StringUtil.isNotEmptyOrNull(LabelName)) { + JSONArray labels = new JSONArray(); + labels.put(LabelName); + issueObject.put("labels", labels); + } + + LOG.debug(issueObject.toString(1)); + + } catch (JSONException ex) { + LOG.debug(ex, ex); + } catch (UnsupportedEncodingException ex) { + LOG.error(ex, ex); + } + return issueObject; + + } + + private String convertToDate(long cerberusDate) { + + SimpleDateFormat formater; // Define the MySQL Format. + formater = new SimpleDateFormat("yyyy-MM-dd' at 'HH:mm:ss"); + + return formater.format(cerberusDate); + + } + + private String convertToDate(Timestamp cerberusDate) { + + SimpleDateFormat formater; // Define the MySQL Format. + formater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); + + return formater.format(cerberusDate); + + } +} diff --git a/source/src/main/java/org/cerberus/core/service/bug/github/impl/GithubService.java b/source/src/main/java/org/cerberus/core/service/bug/github/impl/GithubService.java new file mode 100644 index 000000000..46e617755 --- /dev/null +++ b/source/src/main/java/org/cerberus/core/service/bug/github/impl/GithubService.java @@ -0,0 +1,223 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.service.bug.github.impl; + +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.Base64; +import javax.net.ssl.SSLContext; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.TrustSelfSignedStrategy; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; +import org.apache.http.ssl.SSLContextBuilder; +import org.apache.http.util.EntityUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.cerberus.core.crud.entity.LogEvent; +import org.cerberus.core.crud.entity.Parameter; +import org.cerberus.core.crud.entity.TestCaseExecution; +import org.cerberus.core.crud.service.IApplicationService; +import org.cerberus.core.crud.service.ILogEventService; +import org.cerberus.core.crud.service.IParameterService; +import org.cerberus.core.crud.service.ITestCaseService; +import org.cerberus.core.service.proxy.IProxyService; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.cerberus.core.service.bug.github.IGithubService; +import org.cerberus.core.service.bug.github.IGithubGenerationService; +import org.cerberus.core.util.StringUtil; + +/** + * + * @author vertigo17 + */ +@Service +public class GithubService implements IGithubService { + + @Autowired + private IParameterService parameterService; + @Autowired + private IProxyService proxyService; + @Autowired + private IGithubGenerationService githubGenerationService; + @Autowired + private IApplicationService applicationService; + @Autowired + private ILogEventService logEventService; + @Autowired + private ITestCaseService testCaseService; + + private static final Logger LOG = LogManager.getLogger(GithubService.class); + + private static final boolean DEFAULT_PROXY_ACTIVATE = false; + private static final String DEFAULT_PROXY_HOST = "proxy"; + private static final int DEFAULT_PROXY_PORT = 80; + private static final boolean DEFAULT_PROXYAUTHENT_ACTIVATE = false; + private static final String DEFAULT_PROXYAUTHENT_USER = "squid"; + private static final String DEFAULT_PROXYAUTHENT_PASSWORD = "squid"; + + private static final String GITHUB_ISSUECREATION_URL_DEFAULT = "https://api.github.com/repos/"; + private static final String GITHUB_ISSUECREATION_URLPATH = "/issues"; + private static final String GITHUB_API_VERSION = "2022-11-28"; + + private static final int DEFAULT_XRAY_CACHE_DURATION = 300; + + @Override + public void createGithubIssue(TestCaseExecution execution, String repoName, String issueType) { + + try { + + JSONObject githubRequest = new JSONObject(); + + LOG.debug("call GITHUB Issue creation following execution {}", execution.getId()); + + githubRequest = githubGenerationService.generateGithubIssue(execution, issueType); + + // Builf Github URL. + String githubUrl = GITHUB_ISSUECREATION_URL_DEFAULT + StringUtil.addPrefixIfNotAlready(StringUtil.addSuffixIfNotAlready(repoName, "/"), "/") + GITHUB_ISSUECREATION_URLPATH; + + CloseableHttpClient httpclient = null; + HttpClientBuilder httpclientBuilder; + + if (proxyService.useProxy(githubUrl, "")) { + + String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", "", DEFAULT_PROXY_HOST); + int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", "", DEFAULT_PROXY_PORT); + + HttpHost proxyHostObject = new HttpHost(proxyHost, proxyPort); + + if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", "", DEFAULT_PROXYAUTHENT_ACTIVATE)) { + + String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", "", DEFAULT_PROXYAUTHENT_USER); + String proxyPassword = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", "", DEFAULT_PROXYAUTHENT_PASSWORD); + + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); + + LOG.debug("Activating Proxy With Authentification."); + httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject) + .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) + .setDefaultCredentialsProvider(credsProvider); + + } else { + + LOG.debug("Activating Proxy (No Authentification)."); + httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject); + } + } else { + httpclientBuilder = HttpClientBuilder.create(); + } + + boolean acceptUnsignedSsl = parameterService.getParameterBooleanByKey("cerberus_accept_unsigned_ssl_certificate", "", true); + + if (acceptUnsignedSsl) { + // authorize non valide certificat ssl + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() { + @Override + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return true; + } + }).build(); + + httpclientBuilder + .setSSLContext(sslContext) + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + } + + httpclient = httpclientBuilder.build(); + + HttpPost post = new HttpPost(githubUrl); + StringEntity entity = new StringEntity(githubRequest.toString(), StandardCharsets.UTF_8); + post.setEntity(entity); + post.setHeader("Accept", "application/vnd.github+json"); + post.setHeader("Content-type", "application/json"); + post.setHeader("X-GitHub-Api-Version", GITHUB_API_VERSION); + // + // TODO Make auth based on parameters + + String bearerAuth = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_github_apitoken, "", ""); + post.setHeader("Authorization", "Bearer " + bearerAuth); + + try { + + LOG.debug("Calling {} with Authent {}", githubUrl, bearerAuth); + HttpResponse response = httpclient.execute(post); + + int rc = response.getStatusLine().getStatusCode(); + if (rc >= 200 && rc < 300) { + LOG.debug("Github Issue Creation request http return code : " + rc); + String responseString = EntityUtils.toString(response.getEntity()); + LOG.debug("Response : {}", responseString); + JSONObject githubResponse = new JSONObject(responseString); + String newGithubBugURL = ""; + int githubIssueKey = 0; + if (githubResponse.has("number")) { + githubIssueKey = githubResponse.getInt("number"); + if (githubResponse.has("html_url")) { + URL ghURL = new URL(githubResponse.getString("html_url")); + newGithubBugURL = ghURL.toString(); + } + // Update here the test case with new issue. + testCaseService.addNewBugEntry(execution.getTest(), execution.getTestCase(), String.valueOf(githubIssueKey), newGithubBugURL, "Created from Cerberus Engine."); + LOG.debug("Setting new GITHUB Issue '{}' to test case '{} - {}'", githubResponse.getInt("number"), execution.getTest() + execution.getTestCase()); + } else { + LOG.warn("Github Issue creation request http return code : {} is missing 'number' entry.", rc); + String message = "Github Issue creation request to '" + githubUrl + "' failed with http return code : " + rc + ". and no 'number' entry. " + responseString; + logEventService.createForPrivateCalls("GITHUB", "APICALL", LogEvent.STATUS_WARN, message); + LOG.warn("Message sent to " + githubUrl + " :"); + LOG.warn(githubRequest.toString(1)); + LOG.warn("Response : {}", responseString); + } + } else { + LOG.warn("Github Issue creation request http return code : " + rc); + String responseString = EntityUtils.toString(response.getEntity()); + String message = "Github Issue creation request to '" + githubUrl + "' failed with http return code : " + rc + ". " + responseString; + logEventService.createForPrivateCalls("GITHUB", "APICALL", LogEvent.STATUS_WARN, message); + LOG.warn("Message sent to " + githubUrl + " :"); + LOG.warn(githubRequest.toString(1)); + LOG.warn("Response : {}", responseString); + } + + } catch (IOException e) { + LOG.warn("Github Issue creation request Exception : " + e, e); + logEventService.createForPrivateCalls("GITHUB", "APICALL", LogEvent.STATUS_WARN, "GITHUB Issue creation request to '" + githubUrl + "' failed : " + e.toString() + "."); + } + + } catch (Exception ex) { + LOG.error(ex, ex); + } + + } + +} diff --git a/source/src/main/java/org/cerberus/core/service/bug/impl/BugService.java b/source/src/main/java/org/cerberus/core/service/bug/impl/BugService.java new file mode 100644 index 000000000..5ded66468 --- /dev/null +++ b/source/src/main/java/org/cerberus/core/service/bug/impl/BugService.java @@ -0,0 +1,87 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.service.bug.impl; + +import org.cerberus.core.crud.entity.Application; +import org.cerberus.core.crud.entity.Parameter; +import org.cerberus.core.crud.entity.TestCaseExecution; +import org.cerberus.core.crud.service.IApplicationService; +import org.cerberus.core.crud.service.IParameterService; +import org.cerberus.core.crud.service.ITestCaseService; +import org.cerberus.core.exception.CerberusException; +import org.cerberus.core.service.bug.IBugService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.scheduling.annotation.Async; +import org.cerberus.core.service.bug.github.IGithubService; +import org.cerberus.core.service.bug.jira.IJiraService; + +/** + * + * @author vertigo17 + */ +@Service +public class BugService implements IBugService { + + @Autowired + private IParameterService parameterService; + @Autowired + private IApplicationService applicationService; + @Autowired + private ITestCaseService testCaseService; + @Autowired + private IGithubService githubService; + @Autowired + private IJiraService jiraService; + + private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(BugService.class); + + @Override + @Async + public void createIssue(TestCaseExecution execution) { + + if (!parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_autobugcreation_enable, execution.getSystem(), false)) { + return; + } + + // Testcase should have a priority defined and in WORKING status + if ((execution.getTestCasePriority() >= 1) && ("WORKING".equals(execution.getStatus()))) { + // There should not be any already existing bug. + if (!testCaseService.isBugAlreadyOpen(execution.getTestCaseObj())) { + + // All is fine to open a new bug + Application currentAppli = new Application(); + try { + currentAppli = applicationService.convert(applicationService.readByKey(execution.getApplication())); + } catch (CerberusException ex) { + LOG.warn(ex, ex); + } + + if (currentAppli != null && Application.BUGTRACKER_JIRA.equalsIgnoreCase(currentAppli.getBugTrackerConnector())) { + jiraService.createJiraIssue(execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2()); + } + if (currentAppli != null && Application.BUGTRACKER_GITHUB.equalsIgnoreCase(currentAppli.getBugTrackerConnector())) { + githubService.createGithubIssue(execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2()); + } + } + } + } + +} diff --git a/source/src/main/java/org/cerberus/core/service/jira/IJiraGenerationService.java b/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraGenerationService.java similarity index 96% rename from source/src/main/java/org/cerberus/core/service/jira/IJiraGenerationService.java rename to source/src/main/java/org/cerberus/core/service/bug/jira/IJiraGenerationService.java index 7293480e7..36a88c016 100644 --- a/source/src/main/java/org/cerberus/core/service/jira/IJiraGenerationService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraGenerationService.java @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with Cerberus. If not, see . */ -package org.cerberus.core.service.jira; +package org.cerberus.core.service.bug.jira; import org.cerberus.core.crud.entity.TestCaseExecution; import org.json.JSONObject; diff --git a/source/src/main/java/org/cerberus/core/service/jira/IJiraService.java b/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraService.java similarity index 86% rename from source/src/main/java/org/cerberus/core/service/jira/IJiraService.java rename to source/src/main/java/org/cerberus/core/service/bug/jira/IJiraService.java index 75b76e932..aa519f720 100644 --- a/source/src/main/java/org/cerberus/core/service/jira/IJiraService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraService.java @@ -17,10 +17,9 @@ * You should have received a copy of the GNU General Public License * along with Cerberus. If not, see . */ -package org.cerberus.core.service.jira; +package org.cerberus.core.service.bug.jira; import org.cerberus.core.crud.entity.TestCaseExecution; -import org.json.JSONArray; /** * @@ -28,12 +27,6 @@ */ public interface IJiraService { - /** - * - * @param execution - */ - public void createIssue(TestCaseExecution execution); - /** * * @param execution diff --git a/source/src/main/java/org/cerberus/core/service/jira/impl/JiraGenerationService.java b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraGenerationService.java similarity index 97% rename from source/src/main/java/org/cerberus/core/service/jira/impl/JiraGenerationService.java rename to source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraGenerationService.java index 2b88ce11f..3c0b71781 100644 --- a/source/src/main/java/org/cerberus/core/service/jira/impl/JiraGenerationService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraGenerationService.java @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with Cerberus. If not, see . */ -package org.cerberus.core.service.jira.impl; +package org.cerberus.core.service.bug.jira.impl; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -28,13 +28,13 @@ import org.cerberus.core.crud.entity.TestCaseExecution; import org.cerberus.core.crud.service.IParameterService; import org.cerberus.core.crud.service.ITagService; -import org.cerberus.core.service.jira.IJiraGenerationService; import org.cerberus.core.util.StringUtil; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.json.JSONArray; import org.json.JSONException; +import org.cerberus.core.service.bug.jira.IJiraGenerationService; /** * @@ -67,7 +67,7 @@ public JSONObject generateJiraIssue(TestCaseExecution execution, String projectK JSONObject fields = new JSONObject(); - fields.put("summary", "NRT Execution " + execution.getId() + " failed - " + execution.getTest() + " " + execution.getTestCase()); + fields.put("summary", "TestCase Execution " + execution.getId() + " failed - " + execution.getTest() + " " + execution.getTestCase()); JSONObject description = new JSONObject(); description.put("type", "doc"); diff --git a/source/src/main/java/org/cerberus/core/service/jira/impl/JiraService.java b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraService.java similarity index 86% rename from source/src/main/java/org/cerberus/core/service/jira/impl/JiraService.java rename to source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraService.java index cc80d8923..eac4b7840 100644 --- a/source/src/main/java/org/cerberus/core/service/jira/impl/JiraService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraService.java @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with Cerberus. If not, see . */ -package org.cerberus.core.service.jira.impl; +package org.cerberus.core.service.bug.jira.impl; import java.io.IOException; import java.net.URL; @@ -25,7 +25,6 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Base64; -import java.util.HashMap; import javax.net.ssl.SSLContext; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; @@ -44,23 +43,19 @@ import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.cerberus.core.crud.entity.Application; import org.cerberus.core.crud.entity.LogEvent; import org.cerberus.core.crud.entity.Parameter; -import org.cerberus.core.crud.entity.TestCase; import org.cerberus.core.crud.entity.TestCaseExecution; import org.cerberus.core.crud.service.IApplicationService; import org.cerberus.core.crud.service.ILogEventService; import org.cerberus.core.crud.service.IParameterService; import org.cerberus.core.crud.service.ITestCaseService; -import org.cerberus.core.exception.CerberusException; -import org.cerberus.core.service.jira.IJiraGenerationService; -import org.cerberus.core.service.jira.IJiraService; import org.cerberus.core.service.proxy.IProxyService; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.scheduling.annotation.Async; +import org.cerberus.core.service.bug.jira.IJiraGenerationService; +import org.cerberus.core.service.bug.jira.IJiraService; /** * @@ -83,8 +78,6 @@ public class JiraService implements IJiraService { private ITestCaseService testCaseService; // Area to store JIRA XRay token in cache. - private HashMap cacheEntry = new HashMap<>(); - private static final Logger LOG = LogManager.getLogger(JiraService.class); private static final boolean DEFAULT_PROXY_ACTIVATE = false; @@ -99,34 +92,6 @@ public class JiraService implements IJiraService { private static final int DEFAULT_XRAY_CACHE_DURATION = 300; - @Override - @Async - public void createIssue(TestCaseExecution execution) { - - if (!parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_autobugcreation_enable, execution.getSystem(), false)) { - return; - } - - // Testcase should have a priority defined and in WORKING status - if ((execution.getTestCasePriority() >= 1) && ("WORKING".equals(execution.getStatus()))) { - // There should not be any already existing bug. - if (!testCaseService.isBugAlreadyOpen(execution.getTestCaseObj())) { - - // All is fine to open a new bug - Application currentAppli = new Application(); - try { - currentAppli = applicationService.convert(applicationService.readByKey(execution.getApplication())); - } catch (CerberusException ex) { - LOG.warn(ex, ex); - } - - if (currentAppli != null && Application.BUGTRACKER_JIRA.equalsIgnoreCase(currentAppli.getBugTrackerConnector())) { - createJiraIssue(execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2()); - } - } - } - } - @Override public void createJiraIssue(TestCaseExecution execution, String projectKey, String issueType) { diff --git a/source/src/main/resources/database.sql b/source/src/main/resources/database.sql index 745bc6f42..2d7d1fe7c 100644 --- a/source/src/main/resources/database.sql +++ b/source/src/main/resources/database.sql @@ -6507,3 +6507,18 @@ INSERT INTO `parameter` (`system`, `param`, `value`, `description`) INSERT IGNORE INTO `invariant` (`idname`, `value`, `sort`, `description`, `VeryShortDesc`) VALUES ('PRIORITY', '0', 0, 'No Priority defined', ''); +-- 1834 +UPDATE application set BugTrackerConnector = '' WHERE BugTrackerConnector ="REDIRECT"; + +-- 1835 +INSERT INTO `invariant` (`idname`, `value`, `sort`, `description`) + VALUES ('BUGTRACKERCONNECTOR', 'GITHUB', 200, 'Github Bug Tracker connector') + ,('BUGTRACKERCONNECTOR', 'NONE', 50, 'No Server to Server connector') + ,('BUGTRACKERCONNECTOR', 'JIRA', 100, 'JIRA Bug Tracker connector') ; + +-- 1836 +DELETE FROM invariant WHERE idname ='BUGTRACKERCONNECTOR' and value='REDIRECT'; + +-- 1837 +INSERT INTO `parameter` (`system`, `param`, `value`, `description`) + VALUES ('', 'cerberus_github_apitoken', '', 'Github Personal Access Token that will be used to create issues from API.'); diff --git a/source/src/main/webapp/images/bt-GITHUB.png b/source/src/main/webapp/images/bt-GITHUB.png new file mode 100644 index 000000000..ae2a61bb4 Binary files /dev/null and b/source/src/main/webapp/images/bt-GITHUB.png differ diff --git a/source/src/main/webapp/js/transversalobject/Application.js b/source/src/main/webapp/js/transversalobject/Application.js index f85d03b2b..6b2b0714d 100644 --- a/source/src/main/webapp/js/transversalobject/Application.js +++ b/source/src/main/webapp/js/transversalobject/Application.js @@ -387,7 +387,46 @@ function updateBugTrackerConnector() { }).on("focus", function () { $(this).autocomplete("search", ""); }); - + + } else if (connector === 'GITHUB') { + $('#editApplicationModal #TrackerLogo').attr("src", "./images/bt-GITHUB.png") + $('#editApplicationModal #btP1').show(); + $('#editApplicationModal #btP2').show(); + $('#editApplicationModal #btP3').hide(); + $("[name='bugtrackerparam1Field']").html("organisation/repo"); + $("[name='bugtrackerparam2Field']").html("Label"); + // + $('#editApplicationModal #bugtrackerparam2').autocomplete({ + source: ["bug"], + minLength: 0, + messages: { + noResults: '', + results: function (amount) { + return ''; + } + } + }).on("focus", function () { + $(this).autocomplete("search", ""); + }); + + } else if (connector === 'NONE') { + $('#editApplicationModal #TrackerLogo').attr("src", "") + $('#editApplicationModal #btP1').hide(); + $('#editApplicationModal #btP2').hide(); + $('#editApplicationModal #btP3').hide(); + // + $('#editApplicationModal #bugtrackerparam2').autocomplete({ + source: [""], + minLength: 0, + messages: { + noResults: '', + results: function (amount) { + return ''; + } + } + }).on("focus", function () { + $(this).autocomplete("search", ""); + }); } }