Advertisement
Guest User

Untitled

a guest
Dec 26th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.05 KB | None | 0 0
  1. package ru.toolstrek.script_endpoints
  2.  
  3. import com.atlassian.jira.bc.issue.IssueService
  4. import com.atlassian.jira.component.ComponentAccessor
  5. import com.atlassian.jira.config.IssueTypeManager
  6. import com.atlassian.jira.config.PriorityManager
  7. import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
  8. import groovy.json.JsonException
  9. import groovy.transform.BaseScript
  10. import groovy.json.JsonSlurper
  11.  
  12. import javax.ws.rs.core.MultivaluedMap
  13. import javax.ws.rs.core.Response
  14.  
  15. import org.apache.log4j.Level
  16. import org.apache.log4j.Logger
  17.  
  18. def log = Logger.getLogger("ru.toolstrek.script_endpoints.inline.opsgenie")
  19. log.setLevel(Level.DEBUG)
  20.  
  21. def slurper = new JsonSlurper()
  22. def issueService = ComponentAccessor.getComponentOfType(IssueService)
  23. def cfField = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Test Field").first()
  24. def priorityId = ComponentAccessor.getComponentOfType(PriorityManager).priorities.find { priority ->
  25.     priority.name = "Критический"
  26. }.id
  27. def projectId = ComponentAccessor.projectManager.projects.find { project ->
  28.     project.key == "ITFORIT"
  29. }.id
  30. def issueTypeId = ComponentAccessor.getComponentOfType(IssueTypeManager).issueTypes.find { issueType ->
  31.     issueType.name == "Инцидент"
  32. }.id
  33.  
  34. def cfManager = ComponentAccessor.customFieldManager
  35. def cfTinyId = cfManager.getCustomFieldObject("customfield_19610")
  36. def cfAlertStart = cfManager.getCustomFieldObject("customfield_19500")
  37. def cfAlertEnd = cfManager.getCustomFieldObject("customfield_19501")
  38.  
  39. @BaseScript CustomEndpointDelegate delegate
  40.  
  41. opsgenie(httpMethod: "POST", groups: ["jira-administrators"]) { MultivaluedMap queryParams, String body ->
  42.     // Parse data
  43.     def data
  44.     try {
  45.         data = slurper.parseText(body)
  46.     } catch(JsonException ex) {
  47.         log.error("Error parsing data from OpsGenie: ${ex}")
  48.         return Response.serverError().build()
  49.     }
  50.  
  51.     // Prepare data for issue creation
  52.     def reporterKey = "admin"
  53.     def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey)
  54.     def summary = "test summary"
  55.     def cfValue = "test value"
  56.  
  57.     // Create new issue
  58.     def taskIssueParameters = issueService.newIssueInputParameters()
  59.     taskIssueParameters.setProjectId(projectId)
  60.     .setPriorityId(priorityId)
  61.     .setReporterId(reporterKey)
  62.     .setIssueTypeId(issueTypeId)
  63.     .setSummary(summary)
  64.     .addCustomFieldValue(cfField.id, cfValue)
  65.  
  66.     def createTaskValidationResult = issueService.validateCreate(reporter, taskIssueParameters)
  67.     if (createTaskValidationResult.isValid()) {
  68.         def createTaskResult = issueService.create(reporter, createTaskValidationResult)
  69.         if (!createTaskResult.isValid()) {
  70.             log.error("Error creating issue from OpsGenie data: ${data}")
  71.             return Response.serverError().build()
  72.         }
  73.     } else {
  74.         log.error("Error validating data from OpsGenie: ${createTaskValidationResult.errorCollection}")
  75.         return Response.serverError().build()
  76.     }
  77.     return Response.ok().build()
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement