Class: Lita::Handlers::Jira

Inherits:
Handler
  • Object
show all
Includes:
JiraHelper::Issue, JiraHelper::Misc, JiraHelper::Regex, JiraHelper::Utility
Defined in:
lib/lita/handlers/jira.rb

Overview

Main handler rubocop:disable Metrics/ClassLength

Constant Summary

Constants included from JiraHelper::Regex

JiraHelper::Regex::AMBIENT_PATTERN, JiraHelper::Regex::COMMENT_PATTERN, JiraHelper::Regex::EMAIL_PATTERN, JiraHelper::Regex::ISSUE_PATTERN, JiraHelper::Regex::POINTS_PATTERN, JiraHelper::Regex::PROJECT_PATTERN, JiraHelper::Regex::SUBJECT_PATTERN, JiraHelper::Regex::SUMMARY_PATTERN

Instance Method Summary collapse

Methods included from JiraHelper::Utility

#delete_user!, #get_email, #normalize_user, #store_user!, #user_stored?

Methods included from JiraHelper::Misc

#client

Methods included from JiraHelper::Issue

#create_issue, #fetch_issue, #fetch_issues, #fetch_project, #format_issue, #format_issue_link, #format_issues, #optional_issue_property

Instance Method Details

#ambient(response) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lita/handlers/jira.rb', line 137

def ambient(response)
  return if invalid_ambient?(response)

  # response.matches returns an array of array of strings, where the inner arrays are [issue, project]
  # (e.g. [["XYZ-123", "XYZ"]]). We map it into an array of issues (["XYZ-123"]).
  issue_keys = response.matches.map { |match| match[0] }

  if issue_keys.length > 1
    # Note that if any of the issue keys do not exist in JIRA, then an exception is thrown and no results are returned.
    # A JIRA 'suggestion' has been filed to allow partial results: https://jira.atlassian.com/browse/JRASERVER-40245
    jql = "key in (#{issue_keys.join(',')})"
    # Exceptions are suppressed and no results are returned since this is just ambient parsing and we do not want
    # the bot to pop up with error messages when an explicit command was not requested.
    issues = fetch_issues(jql, true)
    response.reply(format_issues(issues)) if issues && !issues.empty?
  else
    # Only one issue key was parsed, so directly fetch the one issue.
    issue = fetch_issue(response.match_data['issue'], false)
    response.reply(format_issue(issue)) if issue
  end
end

#comment(response) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/lita/handlers/jira.rb', line 97

def comment(response)
  issue = fetch_issue(response.match_data['issue'])
  return response.reply(t('error.request')) unless issue
  comment = issue.comments.build
  comment.save!(body: response.match_data['comment'])
  response.reply(t('comment.added', issue: issue.key))
end

#details(response) ⇒ Object



91
92
93
94
95
# File 'lib/lita/handlers/jira.rb', line 91

def details(response)
  issue = fetch_issue(response.match_data['issue'])
  return response.reply(t('error.request')) unless issue
  response.reply(format_issue(issue))
end

#myissues(response) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/lita/handlers/jira.rb', line 121

def myissues(response)
  return response.reply(t('error.not_identified')) unless user_stored?(response.user)

  begin
    issues = fetch_issues("assignee = '#{get_email(response.user)}' AND status not in (Closed)")
  rescue StandardError => e
    log.error("JIRA HTTPError #{e}")
    response.reply(t('error.request'))
    return
  end

  return response.reply(t('myissues.empty')) if issues.empty?

  response.reply(format_issues(issues))
end

#point(response) ⇒ Object

rubocop:disable Metrics/AbcSize



114
115
116
117
118
119
# File 'lib/lita/handlers/jira.rb', line 114

def point(response)
  return response.reply(t('error.field_undefined')) if config.points_field.blank?
  issue = fetch_issue(response.match_data['issue'])
  return response.reply(t('error.request')) unless issue
  set_points_on_issue(issue, response)
end

#summary(response) ⇒ Object



85
86
87
88
89
# File 'lib/lita/handlers/jira.rb', line 85

def summary(response)
  issue = fetch_issue(response.match_data['issue'])
  return response.reply(t('error.request')) unless issue
  response.reply(t('issue.summary', key: issue.key, summary: issue.summary))
end

#todo(response) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/lita/handlers/jira.rb', line 105

def todo(response)
  issue = create_issue(response.match_data['project'],
                       response.match_data['subject'],
                       response.match_data['summary'])
  return response.reply(t('error.request')) unless issue
  response.reply(t('issue.created', key: issue.key))
end