xiaohui-sun commented on a change in pull request #4748: [TE][notification] 
Ability to reopen and reuse existing Jira tickets for alerts
URL: https://github.com/apache/incubator-pinot/pull/4748#discussion_r340302160
 
 

 ##########
 File path: 
thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/alert/scheme/DetectionJiraAlerter.java
 ##########
 @@ -76,43 +78,81 @@ public DetectionJiraAlerter(ADContentFormatterContext 
adContext, ThirdEyeAnomaly
     super(adContext, result);
     this.teConfig = thirdeyeConfig;
 
-    init();
+    this.jiraAdminConfig = 
JiraConfiguration.createFromProperties(this.teConfig.getAlerterConfiguration().get(JIRA_CONFIG_KEY));
+    this.jiraClient = new ThirdEyeJiraClient(this.jiraAdminConfig);
   }
 
-  private void init() {
-    this.jiraAdminConfig = 
JiraConfiguration.createFromProperties(this.teConfig.getAlerterConfiguration().get(JIRA_CONFIG_KEY));
-    this.jiraRestClient = new 
AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(
-        URI.create(jiraAdminConfig.getJiraHost()),
-        jiraAdminConfig.getJiraUser(),
-        jiraAdminConfig.getJiraPassword());
+  private void updateJiraAlert(Issue issue, JiraEntity jiraEntity) {
+    // Append labels
+    jiraEntity.getLabels().addAll(issue.getLabels());
+    
jiraEntity.setLabels(jiraEntity.getLabels().stream().distinct().collect(Collectors.toList()));
+
+    jiraClient.reopenIssue(issue);
+    jiraClient.updateIssue(issue, jiraEntity);
+
+    try {
+      jiraClient.addComment(issue, jiraEntity.getDescription());
+    } catch (Exception e) {
+      // Jira has a upper limit on the number of characters. In such cases we 
will only share a link in the comment.
+      StringBuilder sb = new StringBuilder();
+      sb.append("*<Truncating details due to jira limit! Please use the below 
link to view all the anomalies.>*");
+      sb.append(System.getProperty("line.separator"));
+
+      String desc = jiraEntity.getDescription().replaceAll("listed below", "");
+
+      // Print only the first line with the redirection link to ThirdEye
+      sb.append(desc, 0, desc.indexOf("\n"));
+      jiraClient.addComment(issue, sb.toString());
+    }
   }
 
-  private String createIssue(IssueInput issueInput) {
-    return 
jiraRestClient.getIssueClient().createIssue(issueInput).claim().getKey();
+  private JiraEntity buildJiraEntity(DetectionAlertFilterNotification 
notification, Set<MergedAnomalyResultDTO> anomalies) {
+    Map<String, Object> notificationSchemeProps = 
notification.getNotificationSchemeProps();
+    if (notificationSchemeProps == null || 
notificationSchemeProps.get(PROP_JIRA_SCHEME) == null) {
+      throw new IllegalArgumentException("Invalid jira settings in 
subscription group " + this.adContext.getNotificationConfig().getId());
+    }
+
+    Properties jiraClientConfig = new Properties();
+    
jiraClientConfig.putAll(ConfigUtils.getMap(notificationSchemeProps.get(PROP_JIRA_SCHEME)));
+
+    List<AnomalyResult> anomalyResultListOfGroup = new ArrayList<>(anomalies);
+    anomalyResultListOfGroup.sort(COMPARATOR_DESC);
+
+    BaseNotificationContent content = 
super.buildNotificationContent(jiraClientConfig);
+    return new JiraContentFormatter(this.jiraAdminConfig, jiraClientConfig, 
content,
+        this.teConfig, adContext).getJiraEntity(anomalyResultListOfGroup);
   }
 
   private void createJiraTickets(DetectionAlertFilterResult results) {
     LOG.info("Preparing a jira alert for subscription group id {}", 
this.adContext.getNotificationConfig().getId());
     Preconditions.checkNotNull(results.getResult());
     for (Map.Entry<DetectionAlertFilterNotification, 
Set<MergedAnomalyResultDTO>> result : results.getResult().entrySet()) {
       try {
-        Map<String, Object> notificationSchemeProps = 
result.getKey().getNotificationSchemeProps();
-        if (notificationSchemeProps == null || 
notificationSchemeProps.get(PROP_JIRA_SCHEME) == null) {
-          throw new IllegalArgumentException("Invalid jira settings in 
subscription group " + this.adContext.getNotificationConfig().getId());
+        JiraEntity jiraEntity = buildJiraEntity(result.getKey(), 
result.getValue());
+        Iterable<Issue> issues = 
jiraClient.getIssue(jiraEntity.getJiraProject(), jiraEntity.getSummary(),
+            this.jiraAdminConfig.getJiraUser()).getIssues();
+
+        List<Issue> jiraIssues = new ArrayList<>();
+        if (issues != null) {
+          // Sort the issues by descending order of create time
+          jiraIssues = StreamSupport.stream(issues.spliterator(), 
false).sorted(new Comparator<Issue>() {
 
 Review comment:
   Just a personal preference - this comparison could be written using lambda 
function which looks more compact.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to