24
24
import com .atlassian .jira .rest .client .api .domain .input .IssueInput ;
25
25
import com .atlassian .jira .rest .client .api .domain .input .IssueInputBuilder ;
26
26
import com .atlassian .jira .rest .client .api .domain .util .ErrorCollection ;
27
- import io .atlassian .util .concurrent .Promise ;
28
27
import hudson .EnvVars ;
29
28
import hudson .model .Job ;
30
29
import hudson .tasks .junit .CaseResult ;
31
30
import hudson .tasks .test .TestResult ;
31
+ import io .atlassian .util .concurrent .Promise ;
32
32
import jenkins .model .Jenkins ;
33
-
34
33
import org .apache .commons .lang3 .StringUtils ;
35
34
import org .jenkinsci .plugins .JiraTestResultReporter .config .AbstractFields ;
36
35
37
36
import java .io .ByteArrayInputStream ;
38
37
import java .net .URI ;
39
38
import java .nio .charset .StandardCharsets ;
40
- import java .util .HashSet ;
41
- import java .util .Map ;
42
- import java .util .Set ;
39
+ import java .util .*;
43
40
import java .util .logging .Level ;
44
41
import java .util .logging .Logger ;
42
+ import java .util .stream .Collectors ;
45
43
46
44
/**
47
45
* Created by tuicu.
@@ -107,17 +105,44 @@ public static String getErrorMessage(RestClientException e, String newLine) {
107
105
}
108
106
return errorMessages .toString ();
109
107
}
110
-
108
+
111
109
public static String createIssue (Job job , EnvVars envVars , CaseResult test ) throws RestClientException {
112
110
return createIssue (job , job , envVars , test , JiraIssueTrigger .JOB );
113
111
}
114
-
112
+
113
+ public static boolean cleanJobCacheFile (List <CaseResult > testCaseResults , Job testJob ){
114
+ List <String > testNames = testCaseResults .stream ().filter (CaseResult ::isFailed ).map ( CaseResult ::getId ).collect ( Collectors .toList ());
115
+ HashMap <String , String > keysToCheck = new HashMap <>();
116
+ List <String > jiraIds = new ArrayList <>();
117
+ for (String test : testNames ){
118
+ if (TestToIssueMapping .getInstance ().getTestIssueKey (testJob , test ) != null ) {
119
+ String jiraId = TestToIssueMapping .getInstance ().getTestIssueKey (testJob , test );
120
+ jiraIds .add (jiraId );
121
+ keysToCheck .put (jiraId , test );
122
+ }
123
+ }
124
+ if (keysToCheck .isEmpty ()) {
125
+ return false ;
126
+ }
127
+
128
+ SearchResult searchResult = JiraUtils .findUnresolvedJiraIssues (String .join ("," , jiraIds ));
129
+ if (searchResult != null && searchResult .getTotal () > 0 ) {
130
+ for (Issue issue : searchResult .getIssues ()) {
131
+ String testKey = issue .getKey ();
132
+ String testId = keysToCheck .get (testKey );
133
+ synchronized (testId ) {
134
+ TestToIssueMapping .getInstance ().removeTestToIssueMapping (testJob , testId , testKey );
135
+ }
136
+ }
137
+ }
138
+ return true ;
139
+ }
140
+
115
141
public static String createIssue (Job job , Job project , EnvVars envVars , CaseResult test , JiraIssueTrigger trigger ) throws RestClientException {
116
142
synchronized (test .getId ()) { //avoid creating duplicated issues
117
143
if (TestToIssueMapping .getInstance ().getTestIssueKey (job , test .getId ()) != null ) {
118
144
return null ;
119
145
}
120
-
121
146
IssueInput issueInput = JiraUtils .createIssueInput (project , test , envVars , trigger );
122
147
SearchResult searchResult = JiraUtils .findIssues (project , test , envVars , issueInput );
123
148
if (searchResult != null && searchResult .getTotal () > 0 ) {
@@ -221,6 +246,14 @@ public static SearchResult findIssues(Job project, TestResult test, EnvVars envV
221
246
FieldInput fi = JiraTestDataPublisher .JiraTestDataPublisherDescriptor .templates .get (0 ).getFieldInput (test , envVars );
222
247
String jql = String .format ("resolution = \" unresolved\" and project = \" %s\" and text ~ \" %s\" " , projectKey , escapeJQL (issueInput .getField (fi .getId ()).getValue ().toString ()));
223
248
249
+ return getSearchResult (jql );
250
+ }
251
+ private static SearchResult findUnresolvedJiraIssues (String keys ) throws RestClientException {
252
+ String jql = String .format ("key in (%s) and resolution != \" unresolved\" " , keys );
253
+ return getSearchResult (jql );
254
+ }
255
+
256
+ private static SearchResult getSearchResult (String jql ) {
224
257
final Set <String > fields = new HashSet <>();
225
258
fields .add ("issueKey" );
226
259
fields .add ("summary" );
@@ -229,8 +262,7 @@ public static SearchResult findIssues(Job project, TestResult test, EnvVars envV
229
262
fields .add ("updated" );
230
263
fields .add ("project" );
231
264
fields .add ("status" );
232
-
233
- log (jql );
265
+ JiraUtils .log (jql );
234
266
235
267
Promise <SearchResult > searchJqlPromise = JiraUtils .getJiraDescriptor ().getRestClient ().getSearchClient ().searchJql (jql , 50 , 0 , fields );
236
268
return searchJqlPromise .claim ();
0 commit comments