You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2019/10/29 20:34:29 UTC

[GitHub] [incubator-pinot] jihaozh commented on a change in pull request #4748: [TE][notification] Ability to reopen and reuse existing Jira tickets for alerts

jihaozh 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_r340308988
 
 

 ##########
 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:
   can simplify the comparator with the lambda expression, like
   ```
   (o1, o2) -> o2.getCreationDate().compareTo(o1.getCreationDate())
   ```

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org