Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.atlassian.sal.api.component.ComponentLocator
- import com.atlassian.confluence.pages.CommentManager
- import com.atlassian.confluence.pages.PageManager
- import com.atlassian.confluence.spaces.SpaceManager
- import com.atlassian.confluence.pages.Page
- import com.atlassian.confluence.user.UserAccessor
- import com.atlassian.sal.api.user.UserKey
- import com.atlassian.sal.api.user.UserManager
- import com.atlassian.confluence.core.DefaultSaveContext
- import com.atlassian.confluence.core.SaveContext
- import com.atlassian.confluence.event.events.content.page.PageEvent
- import com.atlassian.applinks.api.ApplicationLinkService
- import com.atlassian.applinks.api.application.jira.JiraApplicationType
- import com.atlassian.sal.api.net.Response
- import com.atlassian.sal.api.net.ResponseException
- import com.atlassian.sal.api.net.ResponseHandler
- import com.onresolve.scriptrunner.runner.customisers.WithPlugin
- import groovy.json.JsonBuilder
- import groovy.json.JsonSlurper
- import org.apache.http.util.EntityUtils
- import org.jsoup.Jsoup
- import org.jsoup.nodes.Document
- import org.jsoup.nodes.Element
- import static com.atlassian.sal.api.net.Request.MethodType.POST
- import org.apache.log4j.Level
- import org.apache.log4j.Logger
- log = Logger.getLogger("com.test.Page")
- log.setLevel(Level.DEBUG)
- def pageManager = ComponentLocator.getComponent(PageManager)
- def entityUtils = ComponentLocator.getComponent(EntityUtils)
- def profile = ComponentLocator.getComponent(UserManager)
- def userAccessor = ComponentLocator.getComponent(UserAccessor)
- def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
- def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType)
- def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()
- def page = pageManager.getPage('${page.space}','${page.title}')
- def text_page = page.getBodyAsString()
- /*
- def list
- SpaceKey - KeyPorject - Issuetype - reporter
- Law - LAW - Task bashirov_in
- HR HR Task shakhova_ia
- */
- // ==================
- def doc = Jsoup.parse(text_page)
- //ArrayList<String> subAss = new ArrayList<>();
- def subAss = []
- def table = doc.select("table").get(1) // Выбираем первую таблицу
- def rows = table.select("tr")
- def q
- def t
- def body
- def ser
- def dateNew
- for (int i = 1; i < rows.size(); i++) {
- def row = rows.get(i)
- def cols = row.select("td")
- def summary = cols.get(1).text()
- def description = cols.get(2).text()
- def subAssignee = cols.get(4).select('ac|link')
- //log.debug(cols)
- //======================================================
- log.debug(subAssignee.size())
- subAssignee.each{
- def user = it.select('ri|user').attr('ri:userkey')
- q = userAccessor.getUserByKey(new UserKey(user))
- subAss.add(q.name)
- //log.debug(q)
- //log.debug(subAss)
- }
- //======================================================
- def assignee = cols.get(3).select('ac|link')
- //log.debug(assignee)
- if(assignee.size() != 0){
- def user = assignee.select('ri|user').attr('ri:userkey')
- t = userAccessor.getUserByKey(new UserKey(user))
- ser = t.name
- //log.debug(t.name)
- } else { ser = null}
- //=====================================================
- def date = cols.get(6).select('time')
- //log.debug(date)
- if (date.size() != 0 ){
- dateNew = date.attr('datetime')
- //log.debug(dateNew)
- } else { dateNew =null}
- //=====================================================
- //=====================================================
- /*
- def bodyOnlyTM = [fields: [
- project : [key: "TM"],
- issuetype : [name: "Техническая консультация"],
- summary : summary,
- description: description,
- reporter : [name: "bazhenov_av"],
- assignee : [name: ser],
- //priority : [result[i+4]],
- security: [id: "10400"],
- customfield_11801 : dateNew
- ]
- ]
- def bodyOnOtherTM = [fields: [
- project : [key: "QQ"],
- issuetype : [name: "Story"],
- summary : summary,
- description: description,
- reporter : [name: "vorobiev"],
- assignee : [name: ser],
- //priority : [result[i+4]],
- customfield_11801 : dateNew
- ]
- ]
- if( page.space == "QQ"){
- body = new JsonBuilder([
- bodyOnlyTM
- ]).toString()
- } else {
- body = new JsonBuilder([
- bodyOnOtherTM
- ]).toString()
- }
- */
- body = new JsonBuilder(
- body
- ).toString()
- def request = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue")
- .addHeader("Content-Type", "application/json")
- .setEntity(body)
- request.execute(new ResponseHandler<Response>() {
- @Override
- void handle(Response response) throws ResponseException {
- if (response.statusCode != 201) {
- log.error("Creating Jira issue failed: ${response.responseBodyAsString}")
- }
- //========================================================================================
- // Создание ссылки на WIKI
- //========================================================================================
- def issueData = (new JsonSlurper()).parseText(response.getResponseBodyAsString())
- def linkWiki = new JsonBuilder([
- globalId: "appId=af34f21d-fdf9-3a94-b4fe-70ae558c9259&pageId=${page.id}",
- application: [type: "com.atlassian.confluence"],
- relationship:"Wiki Page",
- object: [
- url:"https://wiki.sands.local/pages/viewpage.action?pageId=${page.id}",
- title:"Wiki Page",
- ]
- ]).toString()
- def linkW = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue/${issueData.key}/remotelink")
- .addHeader("Content-Type", "application/json")
- .setEntity(linkWiki)
- linkW.execute(new ResponseHandler<Response>() {
- @Override
- void handle(Response responseLink) throws ResponseException {
- if (responseLink.statusCode != 201) {
- log.error("Creating Link failed: ${responseLink.responseBodyAsString}")
- }
- }
- })
- //========================================================================================
- // Добавление наблюдателей
- //========================================================================================
- subAss.each { val ->
- def watcher = new JsonBuilder(val).toString()
- log.debug(subAss)
- def issueW = applicationLinkRequestFactory.createRequest(POST, "/rest/api/2/issue/${issueData.key}/watchers")
- .addHeader("Content-Type", "application/json")
- .setEntity(watcher)
- issueW.execute(new ResponseHandler<Response>() {
- @Override
- void handle(Response responseWatchers) throws ResponseException {
- if (responseWatchers.statusCode != 204) {
- log.error("Add Watchers failed: ${responseWatchers.responseBodyAsString}")
- }
- }
- })
- }
- subAss = []
- //=======================================================================================
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement