UNPKG

1.55 kBtext/coffeescriptView Raw
1Config = require "../config"
2Ticket = require "./ticket"
3Utils = require "../utils"
4
5class Search
6
7 @withQueryForProject: (query, project, msg, max=5) ->
8 labels = []
9 if Config.labels.regex.test query
10 labels = (query.match(Config.labels.regex).map((label) -> label.replace('#', '').trim())).concat(labels)
11 query = query.replace Config.labels.regex, ""
12
13 jql = if query.length > 0 then "text ~ \"#{query}\"" else ""
14 noResults = "No results for #{query}"
15 found = "Found <#{Config.jira.url}/secure/IssueNavigator.jspa?jqlQuery=__JQL__&runQuery=true|__xx__ issues> containing `#{query}`"
16
17 if project
18 jql += " and " if jql.length > 0
19 jql += "project = #{project}"
20 noResults += " in project `#{project}`"
21 found += " in project `#{project}`"
22
23 if labels.length > 0
24 jql += " and " if jql.length > 0
25 jql += "labels = #{label}" for label in labels
26 noResults += " with labels `#{labels.join ', '}`"
27 found += " with labels `#{labels.join ', '}`"
28
29 found.replace "__JQL__", encodeURIComponent jql
30 Utils.fetch "#{Config.jira.url}/rest/api/2/search",
31 method: "POST"
32 body: JSON.stringify
33 jql: jql
34 startAt: 0
35 maxResults: max
36 fields: Config.jira.fields
37 .then (json) ->
38 if json.issues.length > 0
39 text = found.replace("__xx__", json.total).replace "__JQL__", encodeURIComponent jql
40 else
41 text = noResults
42
43 text: text
44 tickets: (new Ticket issue for issue in json.issues)
45
46module.exports = Search