Skip to content

Commit

Permalink
Allow bug creation on server side from GUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Dec 14, 2024
1 parent f664d2f commit a1df755
Show file tree
Hide file tree
Showing 28 changed files with 245 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,6 +53,8 @@ public class ExecutionPrivateController {
@Autowired
private ITestCaseExecutionService executionService;
@Autowired
private IBugService bugService;
@Autowired
private ExecutionUUID executionUUIDObject;

@GetMapping("/getLastByCriteria")
Expand Down Expand Up @@ -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 "";
Expand All @@ -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();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;

/**
* @author bcivel
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.cerberus.core.crud.entity.TestCaseExecution;
import org.json.JSONArray;
import org.json.JSONObject;

/**
*
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.cerberus.core.crud.entity.TestCase;
import org.cerberus.core.crud.entity.TestCaseExecution;
import org.json.JSONObject;

/**
*
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -218,7 +219,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
} catch (Exception ex) {
LOG.error(ex, ex);
}

return newBugCreated;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
*
Expand All @@ -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;
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.cerberus.core.crud.entity.TestCase;
import org.cerberus.core.crud.entity.TestCaseExecution;
import org.json.JSONObject;

/**
*
Expand All @@ -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);

}
Loading

0 comments on commit a1df755

Please sign in to comment.