Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. import com.atlassian.sal.api.component.ComponentLocator
  2. import com.atlassian.confluence.pages.CommentManager
  3. import com.atlassian.confluence.pages.PageManager
  4. import com.atlassian.confluence.spaces.SpaceManager
  5. import com.atlassian.confluence.pages.Page
  6. import com.atlassian.confluence.user.UserAccessor
  7. import com.atlassian.sal.api.user.UserKey
  8. import com.atlassian.sal.api.user.UserManager
  9. import com.atlassian.confluence.core.DefaultSaveContext
  10. import com.atlassian.confluence.core.SaveContext
  11. import com.atlassian.confluence.event.events.content.page.PageEvent
  12. import com.atlassian.applinks.api.ApplicationLinkService
  13. import com.atlassian.applinks.api.application.jira.JiraApplicationType
  14. import com.atlassian.sal.api.net.Response
  15. import com.atlassian.sal.api.net.ResponseException
  16. import com.atlassian.sal.api.net.ResponseHandler
  17. import com.onresolve.scriptrunner.runner.customisers.WithPlugin
  18. import groovy.json.JsonBuilder
  19. import groovy.json.JsonSlurper
  20. import org.apache.http.util.EntityUtils
  21. import org.jsoup.Jsoup
  22. import org.jsoup.nodes.Document
  23. import org.jsoup.nodes.Element
  24. import static com.atlassian.sal.api.net.Request.MethodType.POST
  25. import org.apache.log4j.Level
  26. import org.apache.log4j.Logger
  27.  
  28. log = Logger.getLogger("com.test.Page")
  29. log.setLevel(Level.DEBUG)
  30.  
  31. def pageManager = ComponentLocator.getComponent(PageManager)
  32. def entityUtils = ComponentLocator.getComponent(EntityUtils)
  33. def profile = ComponentLocator.getComponent(UserManager)
  34. def userAccessor = ComponentLocator.getComponent(UserAccessor)
  35.  
  36. def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
  37. def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType)
  38. def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()
  39.  
  40. def page = pageManager.getPage('OP','2019-10-09 Meeting notes')
  41. def text_page = page.getBodyAsString()
  42.  
  43. // ==================
  44.  
  45. def doc = Jsoup.parse(text_page)
  46. ArrayList<String> subAss = new ArrayList<>();
  47. def table = doc.select("table").get(1) // Выбираем первую таблицу
  48. def rows = table.select("tr")
  49. int e = 0 // соавторы
  50. def q
  51. def t
  52. def ser
  53. def dateNew
  54. for (int i = 1; i < rows.size(); i++) {
  55. def row = rows.get(i)
  56. def cols = row.select("td")
  57. def summary = cols.get(1).text()
  58. def description = cols.get(2).text()
  59. def subAssignee = cols.get(4).select('ac|link')
  60. //log.debug(subAssignee.size())
  61.  
  62. for( int r = 0; r < subAssignee.size(); r++){
  63. if(subAssignee.size() != 0){
  64. def user = subAssignee[e].select('ri|user').attr('ri:userkey')
  65. e = e + 1
  66. q = userAccessor.getUserByKey(new UserKey(user))
  67. subAss.add(q.name)
  68. //log.debug(q)
  69. //log.debug(subAss)
  70. } else {}
  71. }
  72.  
  73. def assignee = cols.get(3).select('ac|link')
  74. //log.debug(assignee)
  75.  
  76. if(assignee.size() != 0){
  77. def user = assignee.select('ri|user').attr('ri:userkey')
  78. t = userAccessor.getUserByKey(new UserKey(user))
  79. ser = t.name
  80. log.debug(t.name)
  81. } else { ser = null}
  82.  
  83. def date = cols.get(6).select('time')
  84. //log.debug(date)
  85.  
  86. if (date.size() != 0 ){
  87. dateNew = date.attr('datetime')
  88. //log.debug(dateNew)
  89. } else { dateNew =null}
  90.  
  91.  
  92. //==========================
  93.  
  94. //==============================================
  95.  
  96. def body = new JsonBuilder([
  97. fields: [
  98. project : [key: "QQ"],
  99. issuetype : [name: "Техническая консультация"],
  100. summary : summary,
  101. description: description,
  102. reporter : [name: "bazhenov_av"],
  103. assignee : [name: ser],
  104. //priority : [result[i+4]],
  105. customfield_11801 : dateNew
  106. ]
  107. ]).toString()
  108.  
  109.  
  110.  
  111. def request = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue")
  112. .addHeader("Content-Type", "application/json")
  113. .setEntity(body)
  114. request.execute(new ResponseHandler<Response>() {
  115. @Override
  116. void handle(Response response) throws ResponseException {
  117. if (response.statusCode != 201) {
  118. log.error("Creating Jira issue failed: ${response.responseBodyAsString}")
  119. }
  120.  
  121. // Создание ссылки на WIKI
  122. //========================================================================================
  123. def issueData = (new JsonSlurper()).parseText(response.getResponseBodyAsString())
  124. def linkWiki = new JsonBuilder([
  125. globalId: "appId=5aac7244-e0d3-31df-a7f5-d93dce867807&pageId=10748184",
  126. application: [type: "com.atlassian.confluence"],
  127. relationship:"Wiki Page",
  128. object: [
  129. url:"http://confluence-test-app.sands.local/pages/viewpage.action?pageId=10748184",
  130. title:"Wiki Page",
  131. ]
  132. ]).toString()
  133. def linkW = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue/${issueData.key}/remotelink")
  134. .addHeader("Content-Type", "application/json")
  135. .setEntity(linkWiki)
  136.  
  137. linkW.execute(new ResponseHandler<Response>() {
  138. @Override
  139. void handle(Response responseLink) throws ResponseException {
  140. if (responseLink.statusCode != 201) {
  141. log.error("Creating Link failed: ${responseLink.responseBodyAsString}")
  142. }
  143. }
  144. })
  145. //========================================================================================
  146. // Добавление наблюдателей
  147. //========================================================================================
  148.  
  149. def issueWatchers = (new JsonSlurper()).parseText(response.getResponseBodyAsString())
  150. def watchers = new JsonBuilder([
  151. q.name
  152. ]).toString()
  153. log.debug(watchers)
  154. def issueW = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue/${issueData.key}/watchers")
  155. .addHeader("Content-Type", "application/json")
  156. .setEntity(watchers)
  157.  
  158. issueW.execute(new ResponseHandler<Response>() {
  159. @Override
  160. void handle(Response responseWatchers) throws ResponseException {
  161. if (responseWatchers.statusCode != 204) {
  162. log.error("Add Watchers failed: ${responseWatchers.responseBodyAsString}")
  163. }
  164. }
  165. })
  166.  
  167. //=======================================================================================
  168. }
  169. })
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement