Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.29 KB | None | 0 0
  1. replica.key = issue.key
  2. replica.type = issue.type
  3. replica.assignee = issue.assignee
  4. replica.reporter = issue.reporter
  5. replica.summary = issue.summary
  6. replica.description = issue.description
  7. replica.labels = issue.labels
  8. replica.comments = issue.comments
  9. replica.resolution = issue.resolution
  10. replica.status = issue.status
  11. replica.attachments = issue.attachments
  12. replica.project = issue.project
  13.  
  14. // EPIC SYNC
  15. ({
  16. final def injector = play.api.Play$.MODULE$.current().injector()
  17. def relationLevelError2 = { String msg, Throwable cause ->
  18. new IllegalStateException(msg, cause)
  19. }
  20. def relationLevelError = { String msg ->
  21. new IllegalStateException(msg)
  22. }
  23. def issueLevelError = { String msg ->
  24. new com.exalate.api.exception.IssueTrackerException(msg)
  25. }
  26. def issueLevelError2 = { String msg, Throwable e ->
  27. new com.exalate.api.exception.IssueTrackerException(msg, e)
  28. }
  29. def fn = { Closure<?> closure ->
  30. new scala.runtime.AbstractFunction1<Object, Object>() {
  31. @Override
  32. Object apply(Object p) {
  33. return closure.call(p)
  34. }
  35. }
  36. }
  37. def fn2 = { Closure<?> closure ->
  38. new scala.runtime.AbstractFunction2<Object, Object, Object>() {
  39. @Override
  40. Object apply(Object p1, Object p2) {
  41. return closure.call(p1, p2)
  42. }
  43. }
  44. }
  45. def await = { scala.concurrent.Future<?> f -> scala.concurrent.Await$.MODULE$.result(f, scala.concurrent.duration.Duration$.MODULE$.Inf()) }
  46. def orNull = { scala.Option<?> opt -> opt.isDefined() ? opt.get() : null }
  47. def none = { scala.Option$.MODULE$.<?>empty() }
  48. def pair = { l, r -> scala.Tuple2$.MODULE$.<?, ?>apply(l, r) }
  49. def seq = { ... ts ->
  50. def list = Arrays.asList(ts)
  51. def scalaBuffer = scala.collection.JavaConversions.asScalaBuffer(list)
  52. scalaBuffer.toSeq()
  53. }
  54. def seqPlus = { scala.collection.Seq<?> tsLeft, ... tsRight ->
  55. def list = Arrays.asList(tsRight)
  56. def scalaBuffer = scala.collection.JavaConversions.asScalaBuffer(list)
  57. scala.collection.Seq$.MODULE$
  58. .newBuilder()
  59. .$plus$plus$eq(tsLeft)
  60. .$plus$plus$eq(scalaBuffer)
  61. .result()
  62. }
  63. def paginateInternal
  64. paginateInternal = { Integer offset, Integer limit, scala.collection.Seq<?> result, scala.runtime.AbstractFunction2<Integer, Integer, ?> nextPageFn, scala.runtime.AbstractFunction1<?, Integer> getTotalFn ->
  65. def page = nextPageFn.apply(offset, limit)
  66. def total = getTotalFn.apply(page)
  67. def last = total < limit
  68. def newResult = seqPlus(result, page)
  69. if (last) {
  70. newResult
  71. } else {
  72. paginateInternal(offset + limit, limit, newResult, nextPageFn, getTotalFn)
  73. }
  74. }
  75. def paginate = { Integer limit, scala.runtime.AbstractFunction2<Integer, Integer, ?> nextPageFn, scala.runtime.AbstractFunction1<?, Integer> getTotalFn ->
  76. scala.collection.Seq<?> resultSeq = paginateInternal(0, limit, seq(), nextPageFn, getTotalFn)
  77. scala.collection.JavaConversions.bufferAsJavaList(resultSeq.toBuffer())
  78. }
  79. def getGeneralSettings = {
  80. def gsp = injector.instanceOf(com.exalate.api.persistence.issuetracker.jcloud.IJCloudGeneralSettingsPersistence.class)
  81. def gsOpt = await(gsp.get())
  82. def gs = orNull(gsOpt)
  83. gs
  84. }
  85. final def gs = getGeneralSettings()
  86.  
  87. def removeTailingSlash = { String str -> str.trim().replace("/+\$","") }
  88. final def jiraCloudUrl = removeTailingSlash(gs.issueTrackerUrl)
  89.  
  90. def getFieldsJson = {
  91. //"com.pyxis.greenhopper.jira:gh-epic-link"
  92.  
  93. def fieldsResponse
  94. try {
  95. fieldsResponse = await(httpClient.thisJira("/rest/api/2/field", "GET", null, null).get())
  96. } catch (Exception e) {
  97. throw relationLevelError2("Unable to get the fields json, please contact Exalate Support: " + e.message, e)
  98. }
  99. if (fieldsResponse.status() != 200) {
  100. throw relationLevelError("Can not get fields (status "+ fieldsResponse.status() +"), please contact Exalate Support: "+ fieldsResponse.body())
  101. }
  102. groovy.json.JsonSlurper s = new groovy.json.JsonSlurper()
  103. /*
  104. [..., {"id":"customfield_10990","key":"customfield_10990","name":"Epic Link","custom":true,"orderable":true,"navigable":true,"searchable":true,"clauseNames":["cf[10990]","Epic Link"],"schema":{"type":"any","custom":"com.pyxis.greenhopper.jira:gh-epic-link","customId":10990}, ...}]
  105. */
  106. def fieldsJson
  107. try {
  108. fieldsJson = s.parseText(fieldsResponse.body())
  109. } catch (Exception e) {
  110. throw relationLevelError2("Can not parse fields json, please contact Exalate Support: " + fieldsResponse.body(), e)
  111. }
  112. if (!(fieldsJson instanceof List)) {
  113. throw relationLevelError("Fields json has unrecognized strucutre, please contact Exalate Support: " + fieldsResponse.body())
  114. }
  115. fieldsJson as List<Map<String, Object>>
  116. }
  117. final def fieldsJson = getFieldsJson().findAll { it.schema instanceof Map }
  118. final def epicLinkCfJson = fieldsJson.find { it.schema.custom == "com.pyxis.greenhopper.jira:gh-epic-link" }
  119. final def epicNameCfJson = fieldsJson.find { it.schema.custom == "com.pyxis.greenhopper.jira:gh-epic-label" }
  120.  
  121. def search = { String jql ->
  122. //noinspection GroovyAssignabilityCheck
  123. def allSearchResultJsons = paginate(
  124. 50,
  125. fn2 { Integer offset, Integer limit ->
  126. def searchResponse
  127. try {
  128. //noinspection GroovyAssignabilityCheck
  129. searchResponse = await(await(httpClient.authenticate(
  130. none(),
  131. httpClient
  132. .ws()
  133. .url(jiraCloudUrl+"/rest/api/2/search")
  134. .withQueryString(seq(
  135. pair("jql", jql),
  136. pair("startAt", offset as String),
  137. pair("maxResults", limit as String),
  138. pair("fields", "id,key")
  139. ))
  140. .withMethod("GET"),
  141. gs
  142. )).get())
  143. } catch (Exception e) {
  144. throw issueLevelError2("Unable to search, please contact Exalate Support: " +
  145. "\nRequest: GET /rest/api/2/search?jql="+ jql +"&startAt="+ offset +"&maxResults="+ limit +"&fields=id,key" +
  146. "\nError: " + e.message, e)
  147. }
  148. if (searchResponse.status() != 200) {
  149. throw issueLevelError("Can not search (status "+ searchResponse.status() +"), please contact Exalate Support: " +
  150. "\nRequest: GET /rest/api/2/search?jql="+ jql +"&startAt="+ offset +"&maxResults="+ limit +"&fields=id,key"+
  151. "\nResponse: "+ searchResponse.body())
  152. }
  153. def searchResult = searchResponse.body()
  154. groovy.json.JsonSlurper s = new groovy.json.JsonSlurper()
  155. def searchResultJson
  156. try {
  157. searchResultJson = s.parseText(searchResult)
  158. } catch (Exception e) {
  159. throw issueLevelError2("Can not parse the search json, please contact Exalate Support: " + searchResult, e)
  160. }
  161.  
  162. /*
  163. {
  164. "expand": "names,schema",
  165. "startAt": 0,
  166. "maxResults": 50,
  167. "total": 1,
  168. "issues": [
  169. {
  170. "expand": "",
  171. "id": "10001",
  172. "self": "http://www.example.com/jira/rest/api/2/issue/10001",
  173. "key": "HSP-1"
  174. }
  175. ],
  176. "warningMessages": [
  177. "The value 'splat' does not exist for the field 'Foo'."
  178. ]
  179. }
  180. */
  181. if (!(searchResultJson instanceof Map)) {
  182. throw issueLevelError("Issue search json has unrecognized structure, please contact Exalate Support: " + searchResult)
  183. }
  184. searchResultJson as Map<String, Object>
  185. },
  186. fn { Map<String, Object> page -> (page.issues as List<Map<String, Object>>).size() }
  187. )
  188. def foundIssueListList = allSearchResultJsons.collect { page ->
  189. if (!(page.issues instanceof List)) {
  190. throw issueLevelError("Issue Search json has unrecognized structure inside each page, please contact Exalate Support: " + page)
  191. }
  192. page.issues as List<Map<String, Object>>
  193. }
  194. def foundIssues = foundIssueListList.flatten()
  195. foundIssues.collect { story -> [
  196. "id": story.id as Long,
  197. "key": story.key as String
  198. ]}
  199. }
  200.  
  201. def getIssueByIdOrKey = { idOrKey ->
  202. def response
  203. try {
  204. //noinspection GroovyAssignabilityCheck
  205. response = await(await(httpClient.authenticate(
  206. none(),
  207. httpClient
  208. .ws()
  209. .url(jiraCloudUrl+"/rest/api/2/issue/"+idOrKey)
  210. .withMethod("GET"),
  211. gs
  212. )).get())
  213. } catch (Exception e) {
  214. throw issueLevelError2("Unable to get the issue "+ idOrKey +", please contact Exalate Support: " + e.message, e)
  215. }
  216. if (response.status() != 200) {
  217. throw issueLevelError("Can not get the issue "+ idOrKey +" (status "+ response.status() +"), please contact Exalate Support: "+ response.body())
  218. }
  219. def resultStr = response.body() as String
  220. def s = new groovy.json.JsonSlurper()
  221. def resultJson
  222. try {
  223. resultJson = s.parseText(resultStr)
  224. } catch (Exception e) {
  225. throw issueLevelError2("Can not parse the issue "+ idOrKey +" json, please contact Exalate Support: " + resultStr, e)
  226. }
  227.  
  228. /*
  229. {
  230. "expand": "names,schema",
  231. "startAt": 0,
  232. "maxResults": 50,
  233. "total": 1,
  234. "issues": [
  235. {
  236. "expand": "",
  237. "id": "10001",
  238. "self": "http://www.example.com/jira/rest/agile/1.0/board/92/issue/10001",
  239. "key": "HSP-1",
  240. "fields": {}
  241. }
  242. ]
  243. }
  244. */
  245. if (!(resultJson instanceof Map)) {
  246. throw issueLevelError("Issue "+idOrKey+" json has unrecognized structure, please contact Exalate Support: " + resultStr)
  247. }
  248. resultJson as Map<String, Object>
  249. }
  250.  
  251. def toRestIssueKeyInternal = { exIssueKey ->
  252. [
  253. "id" : exIssueKey?.id,
  254. "key": exIssueKey?.getURN(),
  255. ]
  256. }
  257.  
  258. def toEpicContext = { epicIssueKey, storyIssueKeys ->
  259. [
  260. "epic" : toRestIssueKeyInternal(epicIssueKey),
  261. "stories": storyIssueKeys
  262. ]
  263. }
  264.  
  265. def getStories = { com.exalate.basic.domain.BasicIssueKey epicExIssueKey ->
  266. def epicLinkSearchClauseNames = epicLinkCfJson.clauseNames as List<String>
  267. final def epicLinkSearchClauseName = epicLinkSearchClauseNames[0]
  268. def jql = epicLinkSearchClauseName + " = " + epicExIssueKey.URN
  269.  
  270. search(jql)
  271. }
  272.  
  273.  
  274. if (epicLinkCfJson != null && issue.customFields[epicLinkCfJson?.schema?.customId as String].value != null) {
  275. final def thisIssueJson = getIssueByIdOrKey(issueKey.id)
  276. def epicLinkKey = (thisIssueJson.fields[epicLinkCfJson.key as String]) as String
  277. if (epicLinkKey == null) {
  278. throw issueLevelError("Can not find the epic link ("+ epicLinkCfJson.key +") for issue `"+ issueKey.URN +"` ("+ issueKey.id +"), please contact Exalate Support: "+ thisIssueJson)
  279. }
  280. def epic = getIssueByIdOrKey(epicLinkKey)
  281. def epicLinkId = epic.id as Long
  282. replica.customKeys."Epic Link" = ["id": epicLinkId, "key": epicLinkKey]
  283. def exEpicIssueKey = new com.exalate.basic.domain.BasicIssueKey(epicLinkId, epicLinkKey)
  284. def stories = getStories(exEpicIssueKey)
  285. replica.customKeys."epicContext" = toEpicContext(exEpicIssueKey, stories)
  286. }
  287.  
  288. if (epicNameCfJson != null && issue.customFields[epicNameCfJson?.schema?.customId as String].value != null) {
  289. def stories = getStories(issueKey)
  290. replica.customKeys."Epic Name" = issue.customFields[epicNameCfJson?.schema?.customId as String].value
  291. replica.customKeys."epicContext" = toEpicContext(issueKey, stories)
  292. }
  293. })()
  294. // END: EPIC SYNC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement