PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/groovy/com/ceilfors/jenkins/plugins/jiratrigger/integration/JiraSetupRule.groovy

https://gitlab.com/github-cloud-corp/jira-trigger-plugin
Groovy | 106 lines | 81 code | 14 blank | 11 comment | 8 complexity | f1fcca2185f68b36406f0c6ae331d3e3 MD5 | raw file
  1. package com.ceilfors.jenkins.plugins.jiratrigger.integration
  2. import com.ceilfors.jenkins.plugins.jiratrigger.JiraTriggerGlobalConfiguration
  3. import com.ceilfors.jenkins.plugins.jiratrigger.jira.*
  4. import com.ceilfors.jenkins.plugins.jiratrigger.webhook.JiraWebhook
  5. import groovyx.net.http.HTTPBuilder
  6. import jenkins.model.GlobalConfiguration
  7. import org.junit.rules.ExternalResource
  8. /**
  9. * @author ceilfors
  10. */
  11. class JiraSetupRule extends ExternalResource {
  12. public static final String CUSTOM_FIELD_NAME = "My Customer Custom Field"
  13. public static String CUSTOM_FIELD_ID
  14. String jiraRootUrl = "http://localhost:2990/jira"
  15. String jiraUsername = "admin"
  16. String jiraPassword = "admin"
  17. JenkinsRunner jenkinsRunner
  18. JiraSetupRule(JenkinsRunner jenkinsRunner) {
  19. this.jenkinsRunner = jenkinsRunner
  20. }
  21. protected void before() throws Throwable {
  22. configureJenkinsWithNormalUser()
  23. ExtendedJiraRestClient jiraRestClient = new JrjcJiraClient(new JiraTriggerGlobalConfiguration(jiraRootUrl, "admin", "admin")).jiraRestClient
  24. configureWebhook(jiraRestClient.webhookRestClient)
  25. configureCustomField()
  26. }
  27. /**
  28. * This method hits JIRA Test Kit plugin REST API as the official REST API doesn't
  29. * support listing screen ids. The Test Kit client is however not used due to dependency hell.
  30. * There are a lot of transitive dependencies
  31. * that are dependent on by the test kit but not being declared explicitly. Trying to pull them manually seems
  32. * almost pull the entire Atlassian SDK which is huge. Because of the issue, http builder is
  33. * used instead.
  34. */
  35. private configureCustomField() {
  36. def http = new HTTPBuilder(jiraRootUrl + "/rest/testkit-test/1.0/")
  37. http.auth.basic 'admin', 'admin'
  38. def customFieldAlreadyAdded = false
  39. http.get(path: 'customFields/get') { resp, json ->
  40. def customField = json.find { it.name == CUSTOM_FIELD_NAME }
  41. if (customField) {
  42. CUSTOM_FIELD_ID = customField.id
  43. customFieldAlreadyAdded = true
  44. } else {
  45. customFieldAlreadyAdded = false
  46. }
  47. }
  48. if (!customFieldAlreadyAdded) {
  49. http.post(path: 'customFields/create', requestContentType: 'application/json', body: [
  50. name : CUSTOM_FIELD_NAME,
  51. description: "A custom field that contains customer name",
  52. type : "com.atlassian.jira.plugin.system.customfieldtypes:textarea"
  53. ]) { resp, json ->
  54. CUSTOM_FIELD_ID = json.id
  55. }
  56. }
  57. List<String> screensWithoutCustomField = []
  58. http.get(path: 'screens') { resp, screens ->
  59. screens.each { screen ->
  60. boolean fieldAlreadyAdded = screen.tabs.find { tab -> tab.fields.find {it.name == CUSTOM_FIELD_NAME } }
  61. if (!fieldAlreadyAdded) {
  62. screensWithoutCustomField.add(screen.name)
  63. }
  64. }
  65. }
  66. for (String screen : screensWithoutCustomField) {
  67. http.get(path: 'screens/addField', query: [screen: screen, field: CUSTOM_FIELD_NAME])
  68. }
  69. }
  70. def configureWebhook(WebhookRestClient webhookRestClient) {
  71. Iterable<Webhook> webhooks = webhookRestClient.getWebhooks().claim()
  72. webhooks = webhooks.findAll { it.name.contains("Acceptance Test") || it.name.contains("Local Jenkins") }
  73. webhooks.each { webhook ->
  74. webhookRestClient.unregisterWebhook(webhook.selfUri).claim()
  75. }
  76. webhookRestClient.registerWebhook(new WebhookInput(name: "Acceptance Test", events: [JiraWebhook.WEBHOOK_EVENT],
  77. url: jenkinsRunner.webhookUrl)).claim()
  78. webhookRestClient.registerWebhook(new WebhookInput(name: "Acceptance Test (Vagrant)", events: [JiraWebhook.WEBHOOK_EVENT],
  79. url: jenkinsRunner.webhookUrl.replace("localhost", "10.0.2.2"))).claim()
  80. webhookRestClient.registerWebhook(new WebhookInput(name: "Local Jenkins (gradlew server)", events: [JiraWebhook.WEBHOOK_EVENT],
  81. url: "http://localhost:8080/${jenkinsRunner.jiraWebhook.urlName}/")).claim()
  82. webhookRestClient.registerWebhook(new WebhookInput(name: "Local Jenkins (gradlew server) (Vagrant)", events: [JiraWebhook.WEBHOOK_EVENT],
  83. url: "http://localhost:8080/${jenkinsRunner.jiraWebhook.urlName}/".replace("localhost", "10.0.2.2"))).claim()
  84. }
  85. def configureJenkinsWithNormalUser() {
  86. JiraTriggerGlobalConfiguration configuration = GlobalConfiguration.all().get(JiraTriggerGlobalConfiguration)
  87. configuration.jiraRootUrl = jiraRootUrl
  88. configuration.jiraUsername = jiraUsername
  89. configuration.jiraPassword = jiraPassword
  90. configuration.save()
  91. }
  92. }