Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 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('${page.space}','${page.title}')
  41. def text_page = page.getBodyAsString()
  42.  
  43. /*
  44. def list
  45. SpaceKey - KeyPorject - Issuetype - reporter
  46. Law - LAW - Task bashirov_in
  47. HR HR Task shakhova_ia
  48. */
  49.  
  50.  
  51. // ==================
  52.  
  53. def doc = Jsoup.parse(text_page)
  54. //ArrayList<String> subAss = new ArrayList<>();
  55. def subAss = []
  56. def table = doc.select("table").get(1) // Выбираем первую таблицу
  57. def rows = table.select("tr")
  58. def q
  59. def t
  60. def body
  61. def ser
  62. def dateNew
  63. for (int i = 1; i < rows.size(); i++) {
  64. def row = rows.get(i)
  65. def cols = row.select("td")
  66. def summary = cols.get(1).text()
  67. def description = cols.get(2).text()
  68. def subAssignee = cols.get(4).select('ac|link')
  69. //log.debug(cols)
  70. //======================================================
  71. log.debug(subAssignee.size())
  72. subAssignee.each{
  73. def user = it.select('ri|user').attr('ri:userkey')
  74. q = userAccessor.getUserByKey(new UserKey(user))
  75. subAss.add(q.name)
  76. //log.debug(q)
  77. //log.debug(subAss)
  78. }
  79. //======================================================
  80. def assignee = cols.get(3).select('ac|link')
  81. //log.debug(assignee)
  82. if(assignee.size() != 0){
  83. def user = assignee.select('ri|user').attr('ri:userkey')
  84. t = userAccessor.getUserByKey(new UserKey(user))
  85. ser = t.name
  86. //log.debug(t.name)
  87. } else { ser = null}
  88. //=====================================================
  89. def date = cols.get(6).select('time')
  90. //log.debug(date)
  91. if (date.size() != 0 ){
  92. dateNew = date.attr('datetime')
  93. //log.debug(dateNew)
  94. } else { dateNew =null}
  95.  
  96.  
  97. //=====================================================
  98.  
  99. //=====================================================
  100.  
  101. /*
  102. def bodyOnlyTM = [fields: [
  103. project : [key: "TM"],
  104. issuetype : [name: "Техническая консультация"],
  105. summary : summary,
  106. description: description,
  107. reporter : [name: "bazhenov_av"],
  108. assignee : [name: ser],
  109. //priority : [result[i+4]],
  110. security: [id: "10400"],
  111. customfield_11801 : dateNew
  112. ]
  113. ]
  114. def bodyOnOtherTM = [fields: [
  115. project : [key: "QQ"],
  116. issuetype : [name: "Story"],
  117. summary : summary,
  118. description: description,
  119. reporter : [name: "vorobiev"],
  120. assignee : [name: ser],
  121. //priority : [result[i+4]],
  122. customfield_11801 : dateNew
  123. ]
  124. ]
  125.  
  126. if( page.space == "QQ"){
  127. body = new JsonBuilder([
  128. bodyOnlyTM
  129. ]).toString()
  130. } else {
  131. body = new JsonBuilder([
  132. bodyOnOtherTM
  133. ]).toString()
  134. }
  135. */
  136.  
  137. body = new JsonBuilder(
  138. body
  139. ).toString()
  140.  
  141.  
  142.  
  143. def request = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue")
  144. .addHeader("Content-Type", "application/json")
  145. .setEntity(body)
  146. request.execute(new ResponseHandler<Response>() {
  147. @Override
  148. void handle(Response response) throws ResponseException {
  149. if (response.statusCode != 201) {
  150. log.error("Creating Jira issue failed: ${response.responseBodyAsString}")
  151. }
  152. //========================================================================================
  153. // Создание ссылки на WIKI
  154. //========================================================================================
  155. def issueData = (new JsonSlurper()).parseText(response.getResponseBodyAsString())
  156. def linkWiki = new JsonBuilder([
  157. globalId: "appId=af34f21d-fdf9-3a94-b4fe-70ae558c9259&pageId=${page.id}",
  158. application: [type: "com.atlassian.confluence"],
  159. relationship:"Wiki Page",
  160. object: [
  161. url:"https://wiki.sands.local/pages/viewpage.action?pageId=${page.id}",
  162. title:"Wiki Page",
  163. ]
  164. ]).toString()
  165. def linkW = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue/${issueData.key}/remotelink")
  166. .addHeader("Content-Type", "application/json")
  167. .setEntity(linkWiki)
  168.  
  169. linkW.execute(new ResponseHandler<Response>() {
  170. @Override
  171. void handle(Response responseLink) throws ResponseException {
  172. if (responseLink.statusCode != 201) {
  173. log.error("Creating Link failed: ${responseLink.responseBodyAsString}")
  174. }
  175. }
  176. })
  177. //========================================================================================
  178. // Добавление наблюдателей
  179. //========================================================================================
  180. subAss.each { val ->
  181. def watcher = new JsonBuilder(val).toString()
  182. log.debug(subAss)
  183. def issueW = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue/${issueData.key}/watchers")
  184. .addHeader("Content-Type", "application/json")
  185. .setEntity(watcher)
  186. issueW.execute(new ResponseHandler<Response>() {
  187. @Override
  188. void handle(Response responseWatchers) throws ResponseException {
  189. if (responseWatchers.statusCode != 204) {
  190. log.error("Add Watchers failed: ${responseWatchers.responseBodyAsString}")
  191. }
  192. }
  193. })
  194. }
  195. subAss = []
  196. //=======================================================================================
  197. }
  198. })
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement