Skip to content

Commit

Permalink
Automatic issue creation with Github and Jira connector. Any non OK e…
Browse files Browse the repository at this point in the history
…xecution can now create an issue and link it to the Testcase.
  • Loading branch information
vertigo17 committed Nov 10, 2024
1 parent 4c35863 commit ddf7978
Show file tree
Hide file tree
Showing 28 changed files with 263 additions and 69 deletions.
32 changes: 20 additions & 12 deletions source/src/main/java/org/cerberus/core/crud/dao/ITestCaseDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public interface ITestCaseDAO {
*/
void updateApplicationObject(String field, String application, String oldObject, String newObject) throws CerberusException;

/**
*
* @param test
* @param testcase
* @param newBugList
* @throws CerberusException
*/
void updateBugList(String test, String testcase, String newBugList) throws CerberusException;

/**
* @param testCase
* @return
Expand Down Expand Up @@ -129,17 +138,17 @@ public interface ITestCaseDAO {
boolean deleteTestCase(TestCase testCase);

/**
* @param campaign the campaign name
* @param countries arrays of country to filter
* @param campaign the campaign name
* @param countries arrays of country to filter
* @param labelIdList
* @param status arrays of status to filter
* @param system arrays of system to filter
* @param status arrays of status to filter
* @param system arrays of system to filter
* @param application arrays of application to filter
* @param priority arrays of priority to filter
* @param type arrays of type to filter
* @param priority arrays of priority to filter
* @param type arrays of type to filter
* @param testFolder
* @param maxReturn nd max of records to return. (Prevent from returning too
* large list)
* @param maxReturn nd max of records to return. (Prevent from returning too
* large list)
* @return the list of TCase used in the campaign
* @since 1.0.2
*/
Expand Down Expand Up @@ -188,7 +197,6 @@ public interface ITestCaseDAO {
*/
public String findSystemOfTestCase(String test, String testcase) throws CerberusException;


/**
* @param system
* @param test
Expand Down Expand Up @@ -216,7 +224,7 @@ public interface ITestCaseDAO {
* @return
*/
public AnswerList<TestCase> readByVarious(String[] test, String[] app, String[] creator, String[] implementer, String[] system,
String[] campaign, List<Integer> labelid, String[] priority, String[] type, String[] status, int length);
String[] campaign, List<Integer> labelid, String[] priority, String[] type, String[] status, int length);

/**
* @param test
Expand Down Expand Up @@ -245,7 +253,7 @@ public AnswerList<TestCase> readByVarious(String[] test, String[] app, String[]
/**
* @param keyTest
* @param keyTestCase
* @param testCase target object value.
* @param testCase target object value.
* @return
*/
public Answer update(String keyTest, String keyTestCase, TestCase testCase);
Expand All @@ -268,7 +276,7 @@ public AnswerList<TestCase> readByVarious(String[] test, String[] app, String[]
* @param resultSet ResultSet relative to select from table TestCase
* @return object {@link TestCase}
* @throws SQLException when trying to get value from
* {@link java.sql.ResultSet#getString(String)}
* {@link java.sql.ResultSet#getString(String)}
* @see FactoryTestCase
*/
public TestCase loadFromResultSet(ResultSet resultSet) throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,31 @@ public void updateApplicationObject(String field, String application, String old
}
}

@Override
public void updateBugList(String test, String testcase, String newBugList) throws CerberusException {
final String query = new StringBuilder("UPDATE testcase tc ")
.append("SET tc.Bugs=?, tc.`dateModif` = CURRENT_TIMESTAMP , tc.`UsrModif` = 'Cerberus-Engine' ")
.append("where tc.test = ? and tc.testcase = ?;")
.toString();

LOG.debug("SQL " + query);
LOG.debug("SQL.param.service " + test);
LOG.debug("SQL.param.service " + testcase);
LOG.debug("SQL.param.service " + newBugList);

try (Connection connection = this.databaseSpring.connect(); PreparedStatement preStat = connection.prepareStatement(query);) {

int i = 1;
preStat.setString(i++, newBugList);
preStat.setString(i++, test);
preStat.setString(i++, testcase);

preStat.executeUpdate();
} catch (SQLException exception) {
LOG.warn("Unable to execute query : " + exception.toString());
}
}

@Override
public String getMaxTestcaseIdByTestFolder(String test) {
String max = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ public interface ITestCaseService {
* Add the corresponding bug key to the list of bugs only if no other active
* bug already exist.
*
* @param tc
* @param test
* @param testFolder
* @param bugKey
* @param bugURL
* @param description
*/
public void addNewBugEntry(String test, String testFolder, String bugKey, String bugURL, String description);
public void 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 @@ -383,11 +383,6 @@ public boolean hasPermissionsUpdate(Parameter parameter, HttpServletRequest requ
|| Parameter.VALUE_cerberus_smtp_username.equalsIgnoreCase(parameter.getParam())
|| Parameter.VALUE_cerberus_manage_timeout.equalsIgnoreCase(parameter.getParam())
|| Parameter.VALUE_cerberus_apikey_enable.equalsIgnoreCase(parameter.getParam())
// || Parameter.VALUE_cerberus_apikey_value1.equalsIgnoreCase(parameter.getParam())
// || Parameter.VALUE_cerberus_apikey_value2.equalsIgnoreCase(parameter.getParam())
// || Parameter.VALUE_cerberus_apikey_value3.equalsIgnoreCase(parameter.getParam())
// || Parameter.VALUE_cerberus_apikey_value4.equalsIgnoreCase(parameter.getParam())
// || Parameter.VALUE_cerberus_apikey_value5.equalsIgnoreCase(parameter.getParam())
|| Parameter.VALUE_cerberus_url.equalsIgnoreCase(parameter.getParam())
|| Parameter.VALUE_cerberus_webperf_thirdpartyfilepath.equalsIgnoreCase(parameter.getParam())
|| Parameter.VALUE_cerberus_executeCerberusCommand_password.equalsIgnoreCase(parameter.getParam())
Expand Down Expand Up @@ -441,7 +436,8 @@ public boolean isToSecureParameter(Parameter parameter) {
|| parameter.getParam().equals("cerberus_executeCerberusCommand_password")
|| parameter.getParam().equals(Parameter.VALUE_cerberus_xraycloud_clientsecret)
|| parameter.getParam().equals(Parameter.VALUE_cerberus_xraydc_token)
|| parameter.getParam().equals(Parameter.VALUE_cerberus_jiracloud_apiuser_apitoken)) {
|| parameter.getParam().equals(Parameter.VALUE_cerberus_jiracloud_apiuser_apitoken)
|| parameter.getParam().equals(Parameter.VALUE_cerberus_github_apitoken)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.cerberus.core.crud.service.impl;

import java.text.SimpleDateFormat;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -82,6 +83,9 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.cerberus.core.crud.entity.Parameter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
* @author bcivel
Expand Down Expand Up @@ -550,14 +554,41 @@ public void updateApplicationObject(String application, String oldObject, String
}

@Override
public boolean isBugAlreadyOpen(TestCase testcase) {
LOG.debug("TO BE IMPLEMENTED");
public boolean isBugAlreadyOpen(TestCase tc) {
try {
JSONArray bugList = tc.getBugs();
for (int n = 0; n < bugList.length(); n++) {
JSONObject bug = bugList.getJSONObject(n);
if (bug.has("act")) {
if (bug.getBoolean("act")) {
return true;
}
}
}
} catch (JSONException ex) {
LOG.warn(ex,ex);
}
return false;
}

@Override
public void addNewBugEntry(String test, String testFolder, String bugKey, String bugURL, String description) {
LOG.debug("TO BE IMPLEMENTED");
public void addNewBugEntry(TestCase tc, String testFolder, String testCase, String bugKey, String bugURL, String description) {
try {
JSONArray bugList = tc.getBugs();
JSONObject newBug = new JSONObject();
newBug.put("act", true);
newBug.put("dateCreated", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'").format(new java.util.Date()));
newBug.put("id", bugKey);
newBug.put("dateClosed", "1970-01-01T00:00:00.000Z");
newBug.put("desc", description);
newBug.put("url", bugURL);
bugList.put(newBug);
tc.setBugs(bugList);
testCaseDao.updateBugList(testFolder, testCase, bugList.toString());
} catch (JSONException | CerberusException ex) {
LOG.warn(ex, ex);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.cerberus.core.service.bug.github;

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

/**
Expand All @@ -29,10 +30,11 @@ public interface IGithubService {

/**
*
* @param tc
* @param execution
* @param repoName
* @param labelType
*/
public void createGithubIssue(TestCaseExecution execution, String repoName, String labelType);
public void createGithubIssue(TestCase tc, TestCaseExecution execution, String repoName, String labelType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
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.TestCase;
import org.cerberus.core.crud.entity.TestCaseExecution;
import org.cerberus.core.crud.service.IApplicationService;
import org.cerberus.core.crud.service.ILogEventService;
Expand Down Expand Up @@ -94,7 +95,7 @@ public class GithubService implements IGithubService {
private static final int DEFAULT_XRAY_CACHE_DURATION = 300;

@Override
public void createGithubIssue(TestCaseExecution execution, String repoName, String issueType) {
public void createGithubIssue(TestCase tc, TestCaseExecution execution, String repoName, String issueType) {

try {

Expand Down Expand Up @@ -189,7 +190,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
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.");
testCaseService.addNewBugEntry(tc, execution.getTest(), execution.getTestCase(), String.valueOf(githubIssueKey), newGithubBugURL, "Created automaticaly 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

import org.cerberus.core.crud.entity.Application;
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.IParameterService;
import org.cerberus.core.crud.service.ITestCaseService;
import org.cerberus.core.engine.entity.ExecutionLog;
import org.cerberus.core.exception.CerberusException;
import org.cerberus.core.service.bug.IBugService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -58,28 +60,49 @@ public class BugService implements IBugService {
public void createIssue(TestCaseExecution execution) {

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) && ("WORKING".equals(execution.getStatus()))) {
// There should not be any already existing bug.
if (!testCaseService.isBugAlreadyOpen(execution.getTestCaseObj())) {
if ((execution.getTestCasePriority() >= 1)) {
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());

// 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);
}
// There should not be any already existing bug.
if (!testCaseService.isBugAlreadyOpen(tc)) {

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());
// 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) {
switch (currentAppli.getBugTrackerConnector()) {
case Application.BUGTRACKER_JIRA:
jiraService.createJiraIssue(tc, execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2());

break;
case Application.BUGTRACKER_GITHUB:
githubService.createGithubIssue(tc, execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2());

break;
default:
throw new AssertionError();
}
}
} else {
LOG.debug("Not opening Issue because issue is already open");
}
} catch (CerberusException ex) {
LOG.warn(ex, ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.cerberus.core.service.bug.jira;

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

/**
Expand All @@ -29,10 +30,11 @@ public interface IJiraService {

/**
*
* @param testcase
* @param execution
* @param projectKey
* @param issueType
*/
public void createJiraIssue(TestCaseExecution execution, String projectKey, String issueType);
public void createJiraIssue(TestCase testcase, TestCaseExecution execution, String projectKey, String issueType);

}
Loading

0 comments on commit ddf7978

Please sign in to comment.