From 4891b15ab539260c64905cd7621abd84f6d20e6a Mon Sep 17 00:00:00 2001 From: imonteroperez Date: Mon, 18 Apr 2022 17:42:51 +0200 Subject: [PATCH 1/2] Manage escape JQL based on JRASERVER-25092 and provide support for parameterized tests --- .../JiraTestResultReporter/JiraUtils.java | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java b/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java index 738b746..baedb00 100644 --- a/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java +++ b/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java @@ -121,13 +121,19 @@ public static String createIssue(Job job, Job project, EnvVars envVars, CaseResu IssueInput issueInput = JiraUtils.createIssueInput(project, test, envVars); SearchResult searchResult = JiraUtils.findIssues(project, test, envVars, issueInput); if (searchResult != null && searchResult.getTotal() > 0) { + boolean duplicate = false; FieldInput fi = JiraTestDataPublisher.JiraTestDataPublisherDescriptor.templates.get(0).getFieldInput(test, envVars); - String id = issueInput.getField(fi.getId()).getValue().toString(); - JiraUtils.log(String.format("Ignoring creating issue '%s' as it would be a duplicate. (from Jira server)", id)); + String text = issueInput.getField(fi.getId()).getValue().toString(); for (Issue issue: searchResult.getIssues()) { - TestToIssueMapping.getInstance().addTestToIssueMapping(job, test.getId(), issue.getKey()); + if (issue.getSummary().equals(text)) { + JiraUtils.log(String.format("Ignoring creating issue '%s' as it would be a duplicate. (from Jira server)", text)); + duplicate = true; + TestToIssueMapping.getInstance().addTestToIssueMapping(job, test.getId(), issue.getKey()); + } + } + if (duplicate) { + return null; } - return null; } String issueKey = JiraUtils.createIssueInput(issueInput, test); TestToIssueMapping.getInstance().addTestToIssueMapping(job, test.getId(), issueKey); @@ -230,35 +236,30 @@ public static SearchResult findIssues(Job project, TestResult test, EnvVars envV /** * Escape the JQL query of special characters. * - * Currently: - * + - & | ! ( ) { } [ ] ^ ~ * ? \ / : - * - * Reference: - * https://confluence.atlassian.com/jiracoreserver073/search-syntax-for-text-fields-861257223.html + * Based on https://jira.atlassian.com/browse/JRASERVER-25092, currently supported: + * + - & | ~ * + * + * Unsupported: + * ! ( ) { } ^ ? \ / * + * Provides special support for parameterized tests [ ] + * * @param jql the JQL query. * @return the JQL query with special chars escaped. */ static String escapeJQL(String jql) { - return jql.replace("'","\\'") + String result = jql.replace("'","\\'") .replace("\"","\\\"") .replace("\\+", "\\\\+") .replace("-", "\\\\-") .replace("&", "\\\\&") .replace("\\|", "\\\\|") - .replace("!", "\\\\!") - .replace("\\(", "\\\\(") - .replace("\\)", "\\\\)") - .replace("\\{", "\\\\{") - .replace("}", "\\\\}") - .replace("[", "\\\\[") - .replace("]", "\\\\]") - .replace("\\^", "\\\\^") .replace("~", "\\\\~") - .replace("\\*", "\\\\*") - .replace("\\?", "\\\\\\?") - .replace("\\\\","\\\\\\\\") - .replace("\\/", "\\\\/") - .replace(":", "\\\\\\\\:"); + .replace("\\*", "\\\\*"); + + if (result.contains("[")) { + result = result.substring(0, result.lastIndexOf("[")); // let's remove the parameter part + } + return result; } } From e2cd08ed571672e735148ea3bf2c6a2c38c57b22 Mon Sep 17 00:00:00 2001 From: Ildefonso Montero Date: Thu, 21 Apr 2022 10:18:28 +0200 Subject: [PATCH 2/2] Update src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java Co-authored-by: Cathy Chan --- .../org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java b/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java index baedb00..cf435b6 100644 --- a/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java +++ b/src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraUtils.java @@ -242,7 +242,7 @@ public static SearchResult findIssues(Job project, TestResult test, EnvVars envV * Unsupported: * ! ( ) { } ^ ? \ / * - * Provides special support for parameterized tests [ ] + * Provides special support for parameterized tests by ignoring the parameter in [ ] * * @param jql the JQL query. * @return the JQL query with special chars escaped.