PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-jira/src/main/java/org/apache/camel/component/jira/consumer/NewCommentConsumer.java

https://gitlab.com/matticala/apache-camel
Java | 79 lines | 46 code | 8 blank | 25 comment | 4 complexity | 482b37b7a945f182c92499c195f8c491 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.jira.consumer;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Stack;
  21. import com.atlassian.jira.rest.client.domain.BasicIssue;
  22. import com.atlassian.jira.rest.client.domain.Comment;
  23. import com.atlassian.jira.rest.client.domain.Issue;
  24. import org.apache.camel.Exchange;
  25. import org.apache.camel.Processor;
  26. import org.apache.camel.component.jira.JIRAEndpoint;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. /**
  30. * Consumes new comments on JIRA issues.
  31. *
  32. * NOTE: In your JQL, try to optimize the query as much as possible! For example, the JIRA Toolkit Plugin includes a
  33. // "Number of comments" custom field -- use '"Number of comments" > 0' in your query. Also try to minimize based on
  34. // state (status=Open), increase the polling delay, etc. We have to do a separate query for *every single* resulting
  35. * ticket in order to load its comments! For large organizations, the JIRA API can be significantly slow.
  36. */
  37. public class NewCommentConsumer extends AbstractJIRAConsumer {
  38. private static final transient Logger LOG = LoggerFactory.getLogger(NewCommentConsumer.class);
  39. private List<Long> commentIds = new ArrayList<>();
  40. public NewCommentConsumer(JIRAEndpoint endpoint, Processor processor) {
  41. super(endpoint, processor);
  42. LOG.info("JIRA NewCommentConsumer: Indexing current issue comments...");
  43. getComments();
  44. }
  45. @Override
  46. protected int poll() throws Exception {
  47. Stack<Comment> newComments = getComments();
  48. while (!newComments.empty()) {
  49. Comment newComment = newComments.pop();
  50. Exchange e = getEndpoint().createExchange();
  51. e.getIn().setBody(newComment);
  52. getProcessor().process(e);
  53. }
  54. return newComments.size();
  55. }
  56. // In the end, we want *new* comments oldest to newest.
  57. private Stack<Comment> getComments() {
  58. Stack<Comment> newComments = new Stack<>();
  59. List<BasicIssue> issues = getIssues();
  60. for (BasicIssue issue : issues) {
  61. Issue fullIssue = client().getIssueClient().getIssue(issue.getKey(), null);
  62. for (Comment comment : fullIssue.getComments()) {
  63. if (!commentIds.contains(comment.getId())) {
  64. newComments.push(comment);
  65. commentIds.add(comment.getId());
  66. }
  67. }
  68. }
  69. return newComments;
  70. }
  71. }