Skip to content

Commit

Permalink
Automatic JIRA ticket creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Aug 25, 2024
1 parent 718341c commit 5d6468f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ public class Parameter {
public static final String VALUE_cerberus_featureflipping_tagstatistics_enable = "cerberus_featureflipping_tagstatistics_enable";
public static final String VALUE_cerberus_jiracloud_url = "cerberus_jiracloud_url";
public static final String VALUE_cerberus_jiradc_url = "cerberus_jiradc_url";
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 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_smtp_password\",\"cerberus_executeCerberusCommand_password\",\"cerberus_xraycloud_clientsecret\",\"cerberus_xraycloud_clientid\",\"cerberus_xraydc_token\",\"cerberus_jiracloud_apiuser_apitoken\")";

public static final Integer CACHE_DURATION = 60;
public static final Integer SHORT_CACHE_DURATION = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,24 @@ public interface ITestCaseService {
*/
public void updateApplicationObject(String application, String oldObject, String newObject);

/**
*
* @param testcase
* @return
*/
boolean isBugAlreadyOpen(TestCase testcase);

/**
* Add the corresponding bug key to the list of bugs only if no other active
* bug already exist.
*
* @param test
* @param testFolder
* @param bugKey
* @param bugURL
* @param description
*/
public void addBugIfNotAlreadyOpen(String test, String testFolder, String bugKey, String description);
public void addNewBugEntry(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 @@ -440,7 +440,8 @@ public boolean isToSecureParameter(Parameter parameter) {
|| parameter.getParam().equals("cerberus_smtp_password")
|| 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_xraydc_token)
|| parameter.getParam().equals(Parameter.VALUE_cerberus_jiracloud_apiuser_apitoken)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,13 @@ public void updateApplicationObject(String application, String oldObject, String
}

@Override
public void addBugIfNotAlreadyOpen(String test, String testFolder, String bugKey, String description) {
public boolean isBugAlreadyOpen(TestCase testcase) {
LOG.debug("TO BE IMPLEMENTED");
return false;
}

@Override
public void addNewBugEntry(String test, String testFolder, String bugKey, String bugURL, String description) {
LOG.debug("TO BE IMPLEMENTED");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public JSONObject generateJiraIssue(TestCaseExecution execution, String projectK

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");

JSONObject fields = new JSONObject();

Expand Down Expand Up @@ -100,6 +101,10 @@ public JSONObject generateJiraIssue(TestCaseExecution execution, String projectK
contentA1A.put(generateContent("Please check the detailed execution "));
contentA1A.put(generateContentLink(String.valueOf(execution.getId()), cerberusUrlExe));
contentA1A.put(generateBreak());
contentA1A.put(generateContent("You can also access "));
contentA1A.put(generateContentLink("latest executions", cerberusUrlExeHisto));
contentA1A.put(generateContent(" perfomed on that testcase."));
contentA1A.put(generateBreak());
contentA1A.put(generateBreak());
contentA1A.put(generateContent("Execution was triggered from campaign execution "));
contentA1A.put(generateContentLink(String.valueOf(execution.getTag()), cerberusUrlTag));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.charset.StandardCharsets;
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;
Expand All @@ -45,6 +46,8 @@
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;
Expand Down Expand Up @@ -91,7 +94,7 @@ public class JiraService implements IJiraService {
private static final String DEFAULT_PROXYAUTHENT_USER = "squid";
private static final String DEFAULT_PROXYAUTHENT_PASSWORD = "squid";

private static final String JIRACLOUD_ISSUECREATION_URL = "";
private static final String JIRACLOUD_ISSUECREATION_URL_DEFAULT = "https://missing-jira-url/";
private static final String JIRACLOUD_ISSUECREATION_URLPATH = "/rest/api/3/issue";

private static final int DEFAULT_XRAY_CACHE_DURATION = 300;
Expand All @@ -100,15 +103,27 @@ public class JiraService implements IJiraService {
@Async
public void createIssue(TestCaseExecution execution) {

Application currentAppli = new Application();
try {
currentAppli = applicationService.convert(applicationService.readByKey(execution.getApplication()));
} catch (CerberusException ex) {
LOG.warn(ex, ex);
if (!parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_autobugcreation_enable, execution.getSystem(), false)) {
return;
}

if (currentAppli != null && Application.BUGTRACKER_JIRA.equalsIgnoreCase(currentAppli.getBugTrackerConnector())) {
createJiraIssue(execution, currentAppli.getBugTrackerParam1(), currentAppli.getBugTrackerParam2());
// 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());
}
}
}
}

Expand All @@ -124,7 +139,7 @@ public void createJiraIssue(TestCaseExecution execution, String projectKey, Stri
jiraRequest = jiraGenerationService.generateJiraIssue(execution, projectKey, issueType);

// TODO Make url to JIRA instance a parameter.
String jiraUrl = JIRACLOUD_ISSUECREATION_URL + JIRACLOUD_ISSUECREATION_URLPATH;
String jiraUrl = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_jiracloud_url, "", JIRACLOUD_ISSUECREATION_URL_DEFAULT) + JIRACLOUD_ISSUECREATION_URLPATH;

CloseableHttpClient httpclient = null;
HttpClientBuilder httpclientBuilder;
Expand Down Expand Up @@ -182,11 +197,14 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
// TODO Make auth based on parameters
post.setHeader("Authorization", "Basic ");

String basicAuth = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_jiracloud_apiuser, "", "") + ":" + parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_jiracloud_apiuser_apitoken, "", "");
String basicAuth64 = Base64.getEncoder().encodeToString(basicAuth.getBytes());
post.setHeader("Authorization", "Basic " + basicAuth64);

try {

// LOG.debug("Calling {} with Bearer {}", xRayUrl, getToken(execution.getSystem(), execution.getTestCaseObj().getOrigine()));
LOG.debug("Calling {} with Authent {}", jiraUrl, basicAuth64);
HttpResponse response = httpclient.execute(post);

int rc = response.getStatusLine().getStatusCode();
Expand All @@ -195,16 +213,16 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
String responseString = EntityUtils.toString(response.getEntity());
LOG.debug("Response : {}", responseString);
JSONObject jiraResponse = new JSONObject(responseString);
String xrayURL = "";
String newJiraBugURL = "";
String jiraIssueKey = "";
if (jiraResponse.has("key")) {
jiraIssueKey = jiraResponse.getString("key");
if (jiraResponse.has("self")) {
URL jiURL = new URL(jiraResponse.getString("self"));
xrayURL = jiURL.getProtocol() + "://" + jiURL.getHost();
newJiraBugURL = jiURL.getProtocol() + "://" + jiURL.getHost();
}
// Update here the test case with new issue.
testCaseService.addBugIfNotAlreadyOpen(execution.getTest(), execution.getTestCase(), jiraIssueKey, "Created from Cerberus Engine.");
testCaseService.addNewBugEntry(execution.getTest(), execution.getTestCase(), jiraIssueKey, newJiraBugURL, "Created from Cerberus Engine.");
LOG.debug("Setting new JIRA Issue '{}' to test case '{} - {}'", jiraResponse.getString("key"), execution.getTest() + execution.getTestCase());
} else {
LOG.warn("JIRA Issue creation request http return code : {} is missing 'key' entry.", rc);
Expand All @@ -229,8 +247,6 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
logEventService.createForPrivateCalls("JIRA", "APICALL", LogEvent.STATUS_WARN, "JIRA Issue creation request to '" + jiraUrl + "' failed : " + e.toString() + ".");
}

// }
// }
} catch (Exception ex) {
LOG.error(ex, ex);
}
Expand Down
13 changes: 13 additions & 0 deletions source/src/main/resources/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6494,3 +6494,16 @@ INSERT INTO `invariant` (`idname`, `value`, `sort`, `description`)
,('AUTHADDTO', 'Header', 150, 'Authorization Parameters in http headers')
,('INVARIANTPRIVATE', 'AUTHADDTO', '1050', 'Authorization API Key Parameter Method');

-- 1831
INSERT INTO `parameter` (`system`, `param`, `value`, `description`)
VALUES ('', 'cerberus_jiracloud_apiuser', '', 'JIRA Cloud User that will be used to create JIRA issues. Ex : [email protected]'),
('', 'cerberus_jiracloud_apiuser_apitoken', '', 'JIRA Cloud User API Token that will be used to create JIRA issues.');

-- 1832
INSERT INTO `parameter` (`system`, `param`, `value`, `description`)
VALUES ('', 'cerberus_autobugcreation_enable', 'false', 'Activate the automatic creation of a bug following an execution.');

-- 1833
INSERT IGNORE INTO `invariant` (`idname`, `value`, `sort`, `description`, `VeryShortDesc`) VALUES
('PRIORITY', '0', 0, 'No Priority defined', '');

0 comments on commit 5d6468f

Please sign in to comment.