diff --git a/source/src/main/java/org/cerberus/core/apiprivate/ExecutionPrivateController.java b/source/src/main/java/org/cerberus/core/apiprivate/ExecutionPrivateController.java index a91681b81..79849aa1e 100644 --- a/source/src/main/java/org/cerberus/core/apiprivate/ExecutionPrivateController.java +++ b/source/src/main/java/org/cerberus/core/apiprivate/ExecutionPrivateController.java @@ -20,21 +20,19 @@ package org.cerberus.core.apiprivate; import java.util.List; -import java.util.logging.Level; import javax.servlet.http.HttpServletRequest; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.cerberus.core.crud.service.ITestCaseExecutionService; import org.cerberus.core.engine.entity.ExecutionUUID; -import org.cerberus.core.engine.execution.IExecutionStartService; import org.cerberus.core.exception.CerberusException; +import org.cerberus.core.service.bug.IBugService; import org.cerberus.core.util.servlet.ServletUtil; import org.json.JSONArray; import org.json.JSONObject; import org.owasp.html.PolicyFactory; import org.owasp.html.Sanitizers; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -55,6 +53,8 @@ public class ExecutionPrivateController { @Autowired private ITestCaseExecutionService executionService; @Autowired + private IBugService bugService; + @Autowired private ExecutionUUID executionUUIDObject; @GetMapping("/getLastByCriteria") @@ -119,6 +119,7 @@ public String updateDeclareFalseNegative( try { executionService.updateFalseNegative(executionId, true, request.getUserPrincipal().getName()); } catch (CerberusException ex) { + LOG.warn(ex, ex); return ex.toString(); } return ""; @@ -135,10 +136,28 @@ public String updateUndeclareFalseNegative( try { executionService.updateFalseNegative(executionId, false, request.getUserPrincipal().getName()); } catch (CerberusException ex) { + LOG.warn(ex, ex); return ex.toString(); } return ""; } + @PostMapping("{executionId}/createBug") + public String createBug( + @PathVariable("executionId") long executionId, + HttpServletRequest request) { + + JSONObject newBugCreated = new JSONObject(); + // Calling Servlet Transversal Util. + ServletUtil.servletStart(request); + try { + newBugCreated = bugService.createBugFromID(executionId, ""); + } catch (Exception ex) { + LOG.warn(ex, ex); + } + return newBugCreated.toString(); + + } + } diff --git a/source/src/main/java/org/cerberus/core/crud/service/ITestCaseService.java b/source/src/main/java/org/cerberus/core/crud/service/ITestCaseService.java index 0910f2aa8..a0173980d 100644 --- a/source/src/main/java/org/cerberus/core/crud/service/ITestCaseService.java +++ b/source/src/main/java/org/cerberus/core/crud/service/ITestCaseService.java @@ -30,6 +30,7 @@ import java.util.Date; import java.util.List; import java.util.Map; +import org.json.JSONObject; /** * @author bcivel @@ -150,8 +151,9 @@ public interface ITestCaseService { * @param bugKey * @param bugURL * @param description + * @return */ - public void addNewBugEntry(TestCase tc, String test, String testFolder, String bugKey, String bugURL, String description); + public JSONObject addNewBugEntry(TestCase tc, String test, String testFolder, String bugKey, String bugURL, String description); /** * @param tCase diff --git a/source/src/main/java/org/cerberus/core/crud/service/impl/TestCaseService.java b/source/src/main/java/org/cerberus/core/crud/service/impl/TestCaseService.java index 5f84e42f6..155bd2574 100644 --- a/source/src/main/java/org/cerberus/core/crud/service/impl/TestCaseService.java +++ b/source/src/main/java/org/cerberus/core/crud/service/impl/TestCaseService.java @@ -566,13 +566,14 @@ public boolean isBugAlreadyOpen(TestCase tc) { } } } catch (JSONException ex) { - LOG.warn(ex,ex); + LOG.warn(ex, ex); } return false; } @Override - public void addNewBugEntry(TestCase tc, String testFolder, String testCase, String bugKey, String bugURL, String description) { + public JSONObject addNewBugEntry(TestCase tc, String testFolder, String testCase, String bugKey, String bugURL, String description) { + JSONObject bugCreated = new JSONObject(); try { JSONArray bugList = tc.getBugs(); JSONObject newBug = new JSONObject(); @@ -585,10 +586,11 @@ public void addNewBugEntry(TestCase tc, String testFolder, String testCase, Stri bugList.put(newBug); tc.setBugs(bugList); testCaseDao.updateBugList(testFolder, testCase, bugList.toString()); + return newBug; } catch (JSONException | CerberusException ex) { LOG.warn(ex, ex); } - + return bugCreated; } @Override 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 b53150fa5..d0119ba81 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 @@ -921,7 +921,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) { - bugService.createIssue(execution); + bugService.createBugAsync(execution, false); } /* 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 index ebd0ead6e..4813a9bed 100644 --- a/source/src/main/java/org/cerberus/core/service/bug/IBugService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/IBugService.java @@ -21,6 +21,7 @@ import org.cerberus.core.crud.entity.TestCaseExecution; import org.json.JSONArray; +import org.json.JSONObject; /** * @@ -31,8 +32,24 @@ public interface IBugService { /** * * @param execution + * @param forceCreation */ - public void createIssue(TestCaseExecution execution); + public void createBugAsync(TestCaseExecution execution, boolean forceCreation); + /** + * + * @param execution + * @param forceCreation + * @return + */ + public JSONObject createBug(TestCaseExecution execution, boolean forceCreation); + + /** + * + * @param execution + * @param user + * @return + */ + public JSONObject createBugFromID(long execution, String user); } 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 index b163fd9d3..3d6e92379 100644 --- 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 @@ -21,6 +21,7 @@ import org.cerberus.core.crud.entity.TestCase; import org.cerberus.core.crud.entity.TestCaseExecution; +import org.json.JSONObject; /** * @@ -34,7 +35,8 @@ public interface IGithubService { * @param execution * @param repoName * @param labelType + * @return */ - public void createGithubIssue(TestCase tc, TestCaseExecution execution, String repoName, String labelType); + public JSONObject createGithubIssue(TestCase tc, TestCaseExecution execution, String repoName, String labelType); } 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 index 2631f2331..95a43ca42 100644 --- 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 @@ -95,7 +95,8 @@ public class GithubService implements IGithubService { private static final int DEFAULT_XRAY_CACHE_DURATION = 300; @Override - public void createGithubIssue(TestCase tc, TestCaseExecution execution, String repoName, String issueType) { + public JSONObject createGithubIssue(TestCase tc, TestCaseExecution execution, String repoName, String issueType) { + JSONObject newBugCreated = new JSONObject(); try { @@ -190,7 +191,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif newGithubBugURL = ghURL.toString(); } // Update here the test case with new issue. - testCaseService.addNewBugEntry(tc, execution.getTest(), execution.getTestCase(), String.valueOf(githubIssueKey), newGithubBugURL, "Created automaticaly from Execution " + execution.getId()); + newBugCreated = testCaseService.addNewBugEntry(tc, execution.getTest(), execution.getTestCase(), String.valueOf(githubIssueKey), newGithubBugURL, "Created from Execution " + execution.getId()); 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); @@ -218,7 +219,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif } catch (Exception ex) { LOG.error(ex, ex); } - + return newBugCreated; } } 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 index 3e83baad5..251415af8 100644 --- 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 @@ -25,6 +25,7 @@ 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.ITestCaseExecutionService; import org.cerberus.core.crud.service.ITestCaseService; import org.cerberus.core.engine.entity.ExecutionLog; import org.cerberus.core.exception.CerberusException; @@ -34,6 +35,8 @@ import org.springframework.scheduling.annotation.Async; import org.cerberus.core.service.bug.github.IGithubService; import org.cerberus.core.service.bug.jira.IJiraService; +import org.json.JSONException; +import org.json.JSONObject; /** * @@ -49,6 +52,8 @@ public class BugService implements IBugService { @Autowired private ITestCaseService testCaseService; @Autowired + private ITestCaseExecutionService testCaseExecutionService; + @Autowired private IGithubService githubService; @Autowired private IJiraService jiraService; @@ -57,54 +62,88 @@ public class BugService implements IBugService { @Override @Async - public void createIssue(TestCaseExecution execution) { + public void createBugAsync(TestCaseExecution execution, boolean forceCreation) { + createBug(execution, forceCreation); + } - if (!parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_autobugcreation_enable, execution.getSystem(), false)) { - LOG.debug("Not creating issue due to parameter."); - return; - } - LOG.debug("Trying to create issue."); - execution.addExecutionLog(ExecutionLog.STATUS_INFO, "Trying To create the issue."); - // Testcase should have a priority defined and in WORKING status - if ((execution.getTestCasePriority() >= 1) && !"OK".equalsIgnoreCase(execution.getControlStatus())) { - LOG.debug("Execution is not OK, with prio > 0."); - execution.addExecutionLog(ExecutionLog.STATUS_INFO, "Execution is not OK, with prio > 0 "); - TestCase tc = null; - try { - tc = testCaseService.findTestCaseByKey(execution.getTest(), execution.getTestCase()); - - // There should not be any already existing bug. - if (!testCaseService.isBugAlreadyOpen(tc)) { - - // 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); - } + @Override + public JSONObject createBug(TestCaseExecution execution, boolean forceCreation) { + JSONObject newBugCreated = new JSONObject(); + try { - if (currentAppli != null) { - switch (currentAppli.getBugTrackerConnector()) { - case Application.BUGTRACKER_JIRA: - jiraService.createJiraIssue(tc, execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2()); + if (!parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_autobugcreation_enable, execution.getSystem(), false)) { + LOG.debug("Not creating bug due to parameter."); + newBugCreated.put("message", "Not creating bug due to parameter : " + Parameter.VALUE_cerberus_autobugcreation_enable); + return newBugCreated; + } + LOG.debug("Trying to create bug."); + execution.addExecutionLog(ExecutionLog.STATUS_INFO, "Trying To create the bug."); + // Testcase should have a priority defined and in WORKING status + if (((execution.getTestCasePriority() >= 1) && !"OK".equalsIgnoreCase(execution.getControlStatus())) || forceCreation) { + LOG.debug("Execution is not OK, with prio > 0."); + execution.addExecutionLog(ExecutionLog.STATUS_INFO, "Bug creation - Execution is not OK, with prio > 0."); + TestCase tc = null; + try { + tc = testCaseService.findTestCaseByKey(execution.getTest(), execution.getTestCase()); - break; - case Application.BUGTRACKER_GITHUB: - githubService.createGithubIssue(tc, execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2()); + // There should not be any already existing bug. + if ((!testCaseService.isBugAlreadyOpen(tc)) || forceCreation) { + execution.addExecutionLog(ExecutionLog.STATUS_INFO, "Bug creation - There is no existing open bug reported."); - break; - default: - throw new AssertionError(); + // 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); + newBugCreated.put("message", ex.toString()); } + + if (currentAppli != null) { + switch (currentAppli.getBugTrackerConnector()) { + case Application.BUGTRACKER_JIRA: + newBugCreated.put("bug", jiraService.createJiraIssue(tc, execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2())); + + break; + case Application.BUGTRACKER_GITHUB: + newBugCreated.put("bug", githubService.createGithubIssue(tc, execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2())); + + break; + default: + throw new AssertionError(); + } + } + } else { + LOG.debug("Not opening Issue because issue is already open"); + newBugCreated.put("message", "Not opening Issue because issue is already open"); + execution.addExecutionLog(ExecutionLog.STATUS_INFO, "Bug creation - There is already an open bug reported."); } - } else { - LOG.debug("Not opening Issue because issue is already open"); + } catch (CerberusException ex) { + newBugCreated.put("message", ex.toString()); + LOG.warn(ex, ex); } - } catch (CerberusException ex) { - LOG.warn(ex, ex); } + } catch (JSONException ex) { + LOG.error(ex, ex); + } + return newBugCreated; + } + + @Override + public JSONObject createBugFromID(long executionId, String user) { + JSONObject newBugCreated = new JSONObject(); + + LOG.debug("Trying to create bug from execution id {}.", executionId); + TestCaseExecution execution = null; + try { + execution = testCaseExecutionService.findTCExecutionByKey(executionId); + + return this.createBug(execution, true); + + } catch (CerberusException ex) { + LOG.warn(ex, ex); } + return newBugCreated; } } diff --git a/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraService.java b/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraService.java index 031bcf53d..76577166b 100644 --- a/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/jira/IJiraService.java @@ -21,6 +21,7 @@ import org.cerberus.core.crud.entity.TestCase; import org.cerberus.core.crud.entity.TestCaseExecution; +import org.json.JSONObject; /** * @@ -33,8 +34,9 @@ public interface IJiraService { * @param testcase * @param execution * @param projectKey - * @param issueType + * @param bugType + * @return */ - public void createJiraIssue(TestCase testcase, TestCaseExecution execution, String projectKey, String issueType); + public JSONObject createJiraIssue(TestCase testcase, TestCaseExecution execution, String projectKey, String bugType); } diff --git a/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraGenerationService.java b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraGenerationService.java index 3c0b71781..91436a43b 100644 --- a/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraGenerationService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraGenerationService.java @@ -52,7 +52,7 @@ public class JiraGenerationService implements IJiraGenerationService { @Override public JSONObject generateJiraIssue(TestCaseExecution execution, String projectKey, String issueTypeName) { - JSONObject issueObject = new JSONObject(); + JSONObject bugObject = new JSONObject(); try { String cerberusUrl = parameterService.getParameterStringByKey("cerberus_gui_url", "", ""); @@ -123,19 +123,19 @@ public JSONObject generateJiraIssue(TestCaseExecution execution, String projectK project.put("key", projectKey); fields.put("project", project); - issueObject.put("fields", fields); + bugObject.put("fields", fields); JSONObject updateObject = new JSONObject(); - issueObject.put("update", updateObject); + bugObject.put("update", updateObject); - LOG.debug(issueObject.toString(1)); + LOG.debug(bugObject.toString(1)); } catch (JSONException ex) { LOG.debug(ex, ex); } catch (UnsupportedEncodingException ex) { Logger.getLogger(JiraGenerationService.class.getName()).log(Level.SEVERE, null, ex); } - return issueObject; + return bugObject; } diff --git a/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraService.java b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraService.java index 01c250984..5d415276e 100644 --- a/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraService.java +++ b/source/src/main/java/org/cerberus/core/service/bug/jira/impl/JiraService.java @@ -95,15 +95,16 @@ public class JiraService implements IJiraService { private static final int DEFAULT_XRAY_CACHE_DURATION = 300; @Override - public void createJiraIssue(TestCase tc, TestCaseExecution execution, String projectKey, String issueType) { + public JSONObject createJiraIssue(TestCase tc, TestCaseExecution execution, String projectKey, String bugType) { + JSONObject newBugCreated = new JSONObject(); try { JSONObject jiraRequest = new JSONObject(); - LOG.debug("call JIRA Issue creation following execution {}", execution.getId()); + LOG.debug("call JIRA Bug creation following execution {}", execution.getId()); - jiraRequest = jiraGenerationService.generateJiraIssue(execution, projectKey, issueType); + jiraRequest = jiraGenerationService.generateJiraIssue(execution, projectKey, bugType); // TODO Make url to JIRA instance a parameter. String jiraUrl = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_jiracloud_url, "", JIRACLOUD_ISSUECREATION_URL_DEFAULT) + JIRACLOUD_ISSUECREATION_URLPATH; @@ -189,7 +190,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif newJiraBugURL = jiURL.getProtocol() + "://" + jiURL.getHost() + "/browse/" + jiraIssueKey; } // Update here the test case with new issue. - testCaseService.addNewBugEntry(tc, execution.getTest(), execution.getTestCase(), jiraIssueKey, newJiraBugURL, "Created automaticaly from Execution " + execution.getId()); + newBugCreated = testCaseService.addNewBugEntry(tc, execution.getTest(), execution.getTestCase(), jiraIssueKey, newJiraBugURL, "Created from Execution " + execution.getId()); LOG.debug("Setting new JIRA Issue '{}' to test case '{} - {}'", jiraResponse.getString("key"), execution.getTest() + execution.getTestCase()); execution.addExecutionLog(ExecutionLog.STATUS_INFO, "JIRA Bug created"); @@ -221,7 +222,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif } catch (Exception ex) { LOG.error(ex, ex); } - + return newBugCreated; } } diff --git a/source/src/main/resources/documentation/D1/include/en/integration_applicationconfiguration_en.adoc b/source/src/main/resources/documentation/D1/include/en/integration_applicationconfiguration_en.adoc index f032bbede..1159bd958 100644 --- a/source/src/main/resources/documentation/D1/include/en/integration_applicationconfiguration_en.adoc +++ b/source/src/main/resources/documentation/D1/include/en/integration_applicationconfiguration_en.adoc @@ -17,6 +17,8 @@ image:applicationpage.png[ApplicationPage] | Type | The Type of the *[red]#application#* define whether the *[red]#application#* is a GUI, a service or a batch treatment. More detail in the <> section. +| Bug URL | This correspond to the URL that points to the bug on the Bug system of the application. Variable %BUGID% can be used on the URL. + | New Bug URL | This correspond to the URL that points to the page where a new bug can be created on the Bug system of the application. More detail in the <> section. |=== diff --git a/source/src/main/webapp/TestCaseExecution.jsp b/source/src/main/webapp/TestCaseExecution.jsp index 84e61d89f..22d899b56 100644 --- a/source/src/main/webapp/TestCaseExecution.jsp +++ b/source/src/main/webapp/TestCaseExecution.jsp @@ -412,14 +412,15 @@
-
+
+
-
+
diff --git a/source/src/main/webapp/include/transversalobject/Application.html b/source/src/main/webapp/include/transversalobject/Application.html index dd360c8a8..019d97a6f 100644 --- a/source/src/main/webapp/include/transversalobject/Application.html +++ b/source/src/main/webapp/include/transversalobject/Application.html @@ -9,7 +9,7 @@