Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- replica.key = issue.key
- replica.type = issue.type
- replica.assignee = issue.assignee
- replica.reporter = issue.reporter
- replica.summary = issue.summary
- replica.description = issue.description
- replica.labels = issue.labels
- replica.comments = issue.comments
- replica.resolution = issue.resolution
- replica.status = issue.status
- replica.attachments = issue.attachments
- replica.project = issue.project
- // EPIC SYNC
- ({
- final def injector = play.api.Play$.MODULE$.current().injector()
- def relationLevelError2 = { String msg, Throwable cause ->
- new IllegalStateException(msg, cause)
- }
- def relationLevelError = { String msg ->
- new IllegalStateException(msg)
- }
- def issueLevelError = { String msg ->
- new com.exalate.api.exception.IssueTrackerException(msg)
- }
- def issueLevelError2 = { String msg, Throwable e ->
- new com.exalate.api.exception.IssueTrackerException(msg, e)
- }
- def fn = { Closure<?> closure ->
- new scala.runtime.AbstractFunction1<Object, Object>() {
- @Override
- Object apply(Object p) {
- return closure.call(p)
- }
- }
- }
- def fn2 = { Closure<?> closure ->
- new scala.runtime.AbstractFunction2<Object, Object, Object>() {
- @Override
- Object apply(Object p1, Object p2) {
- return closure.call(p1, p2)
- }
- }
- }
- def await = { scala.concurrent.Future<?> f -> scala.concurrent.Await$.MODULE$.result(f, scala.concurrent.duration.Duration$.MODULE$.Inf()) }
- def orNull = { scala.Option<?> opt -> opt.isDefined() ? opt.get() : null }
- def none = { scala.Option$.MODULE$.<?>empty() }
- def pair = { l, r -> scala.Tuple2$.MODULE$.<?, ?>apply(l, r) }
- def seq = { ... ts ->
- def list = Arrays.asList(ts)
- def scalaBuffer = scala.collection.JavaConversions.asScalaBuffer(list)
- scalaBuffer.toSeq()
- }
- def seqPlus = { scala.collection.Seq<?> tsLeft, ... tsRight ->
- def list = Arrays.asList(tsRight)
- def scalaBuffer = scala.collection.JavaConversions.asScalaBuffer(list)
- scala.collection.Seq$.MODULE$
- .newBuilder()
- .$plus$plus$eq(tsLeft)
- .$plus$plus$eq(scalaBuffer)
- .result()
- }
- def paginateInternal
- paginateInternal = { Integer offset, Integer limit, scala.collection.Seq<?> result, scala.runtime.AbstractFunction2<Integer, Integer, ?> nextPageFn, scala.runtime.AbstractFunction1<?, Integer> getTotalFn ->
- def page = nextPageFn.apply(offset, limit)
- def total = getTotalFn.apply(page)
- def last = total < limit
- def newResult = seqPlus(result, page)
- if (last) {
- newResult
- } else {
- paginateInternal(offset + limit, limit, newResult, nextPageFn, getTotalFn)
- }
- }
- def paginate = { Integer limit, scala.runtime.AbstractFunction2<Integer, Integer, ?> nextPageFn, scala.runtime.AbstractFunction1<?, Integer> getTotalFn ->
- scala.collection.Seq<?> resultSeq = paginateInternal(0, limit, seq(), nextPageFn, getTotalFn)
- scala.collection.JavaConversions.bufferAsJavaList(resultSeq.toBuffer())
- }
- def getGeneralSettings = {
- def gsp = injector.instanceOf(com.exalate.api.persistence.issuetracker.jcloud.IJCloudGeneralSettingsPersistence.class)
- def gsOpt = await(gsp.get())
- def gs = orNull(gsOpt)
- gs
- }
- final def gs = getGeneralSettings()
- def removeTailingSlash = { String str -> str.trim().replace("/+\$","") }
- final def jiraCloudUrl = removeTailingSlash(gs.issueTrackerUrl)
- def getFieldsJson = {
- //"com.pyxis.greenhopper.jira:gh-epic-link"
- def fieldsResponse
- try {
- fieldsResponse = await(httpClient.thisJira("/rest/api/2/field", "GET", null, null).get())
- } catch (Exception e) {
- throw relationLevelError2("Unable to get the fields json, please contact Exalate Support: " + e.message, e)
- }
- if (fieldsResponse.status() != 200) {
- throw relationLevelError("Can not get fields (status "+ fieldsResponse.status() +"), please contact Exalate Support: "+ fieldsResponse.body())
- }
- groovy.json.JsonSlurper s = new groovy.json.JsonSlurper()
- /*
- [..., {"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}, ...}]
- */
- def fieldsJson
- try {
- fieldsJson = s.parseText(fieldsResponse.body())
- } catch (Exception e) {
- throw relationLevelError2("Can not parse fields json, please contact Exalate Support: " + fieldsResponse.body(), e)
- }
- if (!(fieldsJson instanceof List)) {
- throw relationLevelError("Fields json has unrecognized strucutre, please contact Exalate Support: " + fieldsResponse.body())
- }
- fieldsJson as List<Map<String, Object>>
- }
- final def fieldsJson = getFieldsJson().findAll { it.schema instanceof Map }
- final def epicLinkCfJson = fieldsJson.find { it.schema.custom == "com.pyxis.greenhopper.jira:gh-epic-link" }
- final def epicNameCfJson = fieldsJson.find { it.schema.custom == "com.pyxis.greenhopper.jira:gh-epic-label" }
- def search = { String jql ->
- //noinspection GroovyAssignabilityCheck
- def allSearchResultJsons = paginate(
- 50,
- fn2 { Integer offset, Integer limit ->
- def searchResponse
- try {
- //noinspection GroovyAssignabilityCheck
- searchResponse = await(await(httpClient.authenticate(
- none(),
- httpClient
- .ws()
- .url(jiraCloudUrl+"/rest/api/2/search")
- .withQueryString(seq(
- pair("jql", jql),
- pair("startAt", offset as String),
- pair("maxResults", limit as String),
- pair("fields", "id,key")
- ))
- .withMethod("GET"),
- gs
- )).get())
- } catch (Exception e) {
- throw issueLevelError2("Unable to search, please contact Exalate Support: " +
- "\nRequest: GET /rest/api/2/search?jql="+ jql +"&startAt="+ offset +"&maxResults="+ limit +"&fields=id,key" +
- "\nError: " + e.message, e)
- }
- if (searchResponse.status() != 200) {
- throw issueLevelError("Can not search (status "+ searchResponse.status() +"), please contact Exalate Support: " +
- "\nRequest: GET /rest/api/2/search?jql="+ jql +"&startAt="+ offset +"&maxResults="+ limit +"&fields=id,key"+
- "\nResponse: "+ searchResponse.body())
- }
- def searchResult = searchResponse.body()
- groovy.json.JsonSlurper s = new groovy.json.JsonSlurper()
- def searchResultJson
- try {
- searchResultJson = s.parseText(searchResult)
- } catch (Exception e) {
- throw issueLevelError2("Can not parse the search json, please contact Exalate Support: " + searchResult, e)
- }
- /*
- {
- "expand": "names,schema",
- "startAt": 0,
- "maxResults": 50,
- "total": 1,
- "issues": [
- {
- "expand": "",
- "id": "10001",
- "self": "http://www.example.com/jira/rest/api/2/issue/10001",
- "key": "HSP-1"
- }
- ],
- "warningMessages": [
- "The value 'splat' does not exist for the field 'Foo'."
- ]
- }
- */
- if (!(searchResultJson instanceof Map)) {
- throw issueLevelError("Issue search json has unrecognized structure, please contact Exalate Support: " + searchResult)
- }
- searchResultJson as Map<String, Object>
- },
- fn { Map<String, Object> page -> (page.issues as List<Map<String, Object>>).size() }
- )
- def foundIssueListList = allSearchResultJsons.collect { page ->
- if (!(page.issues instanceof List)) {
- throw issueLevelError("Issue Search json has unrecognized structure inside each page, please contact Exalate Support: " + page)
- }
- page.issues as List<Map<String, Object>>
- }
- def foundIssues = foundIssueListList.flatten()
- foundIssues.collect { story -> [
- "id": story.id as Long,
- "key": story.key as String
- ]}
- }
- def getIssueByIdOrKey = { idOrKey ->
- def response
- try {
- //noinspection GroovyAssignabilityCheck
- response = await(await(httpClient.authenticate(
- none(),
- httpClient
- .ws()
- .url(jiraCloudUrl+"/rest/api/2/issue/"+idOrKey)
- .withMethod("GET"),
- gs
- )).get())
- } catch (Exception e) {
- throw issueLevelError2("Unable to get the issue "+ idOrKey +", please contact Exalate Support: " + e.message, e)
- }
- if (response.status() != 200) {
- throw issueLevelError("Can not get the issue "+ idOrKey +" (status "+ response.status() +"), please contact Exalate Support: "+ response.body())
- }
- def resultStr = response.body() as String
- def s = new groovy.json.JsonSlurper()
- def resultJson
- try {
- resultJson = s.parseText(resultStr)
- } catch (Exception e) {
- throw issueLevelError2("Can not parse the issue "+ idOrKey +" json, please contact Exalate Support: " + resultStr, e)
- }
- /*
- {
- "expand": "names,schema",
- "startAt": 0,
- "maxResults": 50,
- "total": 1,
- "issues": [
- {
- "expand": "",
- "id": "10001",
- "self": "http://www.example.com/jira/rest/agile/1.0/board/92/issue/10001",
- "key": "HSP-1",
- "fields": {}
- }
- ]
- }
- */
- if (!(resultJson instanceof Map)) {
- throw issueLevelError("Issue "+idOrKey+" json has unrecognized structure, please contact Exalate Support: " + resultStr)
- }
- resultJson as Map<String, Object>
- }
- def toRestIssueKeyInternal = { exIssueKey ->
- [
- "id" : exIssueKey?.id,
- "key": exIssueKey?.getURN(),
- ]
- }
- def toEpicContext = { epicIssueKey, storyIssueKeys ->
- [
- "epic" : toRestIssueKeyInternal(epicIssueKey),
- "stories": storyIssueKeys
- ]
- }
- def getStories = { com.exalate.basic.domain.BasicIssueKey epicExIssueKey ->
- def epicLinkSearchClauseNames = epicLinkCfJson.clauseNames as List<String>
- final def epicLinkSearchClauseName = epicLinkSearchClauseNames[0]
- def jql = epicLinkSearchClauseName + " = " + epicExIssueKey.URN
- search(jql)
- }
- if (epicLinkCfJson != null && issue.customFields[epicLinkCfJson?.schema?.customId as String].value != null) {
- final def thisIssueJson = getIssueByIdOrKey(issueKey.id)
- def epicLinkKey = (thisIssueJson.fields[epicLinkCfJson.key as String]) as String
- if (epicLinkKey == null) {
- throw issueLevelError("Can not find the epic link ("+ epicLinkCfJson.key +") for issue `"+ issueKey.URN +"` ("+ issueKey.id +"), please contact Exalate Support: "+ thisIssueJson)
- }
- def epic = getIssueByIdOrKey(epicLinkKey)
- def epicLinkId = epic.id as Long
- replica.customKeys."Epic Link" = ["id": epicLinkId, "key": epicLinkKey]
- def exEpicIssueKey = new com.exalate.basic.domain.BasicIssueKey(epicLinkId, epicLinkKey)
- def stories = getStories(exEpicIssueKey)
- replica.customKeys."epicContext" = toEpicContext(exEpicIssueKey, stories)
- }
- if (epicNameCfJson != null && issue.customFields[epicNameCfJson?.schema?.customId as String].value != null) {
- def stories = getStories(issueKey)
- replica.customKeys."Epic Name" = issue.customFields[epicNameCfJson?.schema?.customId as String].value
- replica.customKeys."epicContext" = toEpicContext(issueKey, stories)
- }
- })()
- // END: EPIC SYNC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement