PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/api/integrations/jira/org/humbug/jira/ZulipListener.groovy

https://gitlab.com/EnLab/zulip
Groovy | 149 lines | 117 code | 19 blank | 13 comment | 7 complexity | dddf015e36ad7901e5a21ec97b60cb22 MD5 | raw file
  1. /*
  2. * Copyright (c) 2014 Zulip, Inc
  3. */
  4. package org.zulip.jira
  5. import static com.atlassian.jira.event.type.EventType.*
  6. import com.atlassian.jira.event.issue.AbstractIssueEventListener
  7. import com.atlassian.jira.event.issue.IssueEvent
  8. import java.util.logging.Level
  9. import java.util.logging.Logger
  10. import org.apache.commons.httpclient.HttpClient
  11. import org.apache.commons.httpclient.HttpStatus;
  12. import org.apache.commons.httpclient.methods.PostMethod
  13. import org.apache.commons.httpclient.NameValuePair
  14. class ZulipListener extends AbstractIssueEventListener {
  15. Logger LOGGER = Logger.getLogger(ZulipListener.class.getName());
  16. // The email address of one of the bots you created on your Zulip settings page.
  17. String zulipEmail = ""
  18. // That bot's API key.
  19. String zulipAPIKey = ""
  20. // What stream to send messages to. Must already exist.
  21. String zulipStream = "jira"
  22. // The base JIRA url for browsing
  23. String issueBaseUrl = "https://jira.COMPANY.com/browse/"
  24. // Your zulip domain, only change if you have a custom one
  25. String base_url = "https://api.zulip.com"
  26. @Override
  27. void workflowEvent(IssueEvent event) {
  28. processIssueEvent(event)
  29. }
  30. String processIssueEvent(IssueEvent event) {
  31. String author = event.user.displayName
  32. String issueId = event.issue.key
  33. String issueUrl = issueBaseUrl + issueId
  34. String issueUrlMd = String.format("[%s](%s)", issueId, issueBaseUrl + issueId)
  35. String title = event.issue.summary
  36. String subject = truncate(String.format("%s: %s", issueId, title), 60)
  37. String assignee = "no one"
  38. if (event.issue.assignee) {
  39. assignee = event.issue.assignee.name
  40. }
  41. String comment = "";
  42. if (event.comment) {
  43. comment = event.comment.body
  44. }
  45. String content;
  46. // Event types:
  47. // https://docs.atlassian.com/jira/5.0/com/atlassian/jira/event/type/EventType.html
  48. // Issue API:
  49. // https://docs.atlassian.com/jira/5.0/com/atlassian/jira/issue/Issue.html
  50. switch (event.getEventTypeId()) {
  51. case ISSUE_COMMENTED_ID:
  52. content = String.format("%s **updated** %s with comment:\n\n> %s",
  53. author, issueUrlMd, comment)
  54. break
  55. case ISSUE_CREATED_ID:
  56. content = String.format("%s **created** %s priority %s, assigned to **%s**: \n\n> %s",
  57. author, issueUrlMd, event.issue.priorityObject.name,
  58. assignee, title)
  59. break
  60. case ISSUE_ASSIGNED_ID:
  61. content = String.format("%s **reassigned** %s to **%s**",
  62. author, issueUrlMd, assignee)
  63. break
  64. case ISSUE_DELETED_ID:
  65. content = String.format("%s **deleted** %s!",
  66. author, issueUrlMd)
  67. break
  68. case ISSUE_RESOLVED_ID:
  69. content = String.format("%s **resolved** %s as %s:\n\n> %s",
  70. author, issueUrlMd, event.issue.resolutionObject.name,
  71. comment)
  72. break
  73. case ISSUE_CLOSED_ID:
  74. content = String.format("%s **closed** %s with resolution %s:\n\n> %s",
  75. author, issueUrlMd, event.issue.resolutionObject.name,
  76. comment)
  77. break
  78. case ISSUE_REOPENED_ID:
  79. content = String.format("%s **reopened** %s:\n\n> %s",
  80. author, issueUrlMd, comment)
  81. break
  82. default:
  83. return
  84. }
  85. sendStreamMessage(zulipStream, subject, content)
  86. }
  87. String post(String method, NameValuePair[] parameters) {
  88. PostMethod post = new PostMethod(zulipUrl(method))
  89. post.setRequestHeader("Content-Type", post.FORM_URL_ENCODED_CONTENT_TYPE)
  90. // TODO: Include more useful data in the User-agent
  91. post.setRequestHeader("User-agent", "ZulipJira/0.1")
  92. try {
  93. post.setRequestBody(parameters)
  94. HttpClient client = new HttpClient()
  95. client.executeMethod(post)
  96. String response = post.getResponseBodyAsString()
  97. if (post.getStatusCode() != HttpStatus.SC_OK) {
  98. String params = ""
  99. for (NameValuePair pair: parameters) {
  100. params += "\n" + pair.getName() + ":" + pair.getValue()
  101. }
  102. LOGGER.log(Level.SEVERE, "Error sending Zulip message:\n" + response + "\n\n" +
  103. "We sent:" + params)
  104. }
  105. return response;
  106. } catch (IOException e) {
  107. throw new RuntimeException(e)
  108. } finally {
  109. post.releaseConnection()
  110. }
  111. }
  112. String truncate(String string, int length) {
  113. if (string.length() < length) {
  114. return string
  115. }
  116. return string.substring(0, length - 3) + "..."
  117. }
  118. String sendStreamMessage(String stream, String subject, String message) {
  119. NameValuePair[] body = [new NameValuePair("api-key", zulipAPIKey),
  120. new NameValuePair("email", zulipEmail),
  121. new NameValuePair("type", "stream"),
  122. new NameValuePair("to", stream),
  123. new NameValuePair("subject", subject),
  124. new NameValuePair("content", message)]
  125. return post("send_message", body);
  126. }
  127. String zulipUrl(method) {
  128. return base_url + "/v1/" + method
  129. }
  130. }