I am working on setting up automation in Jira that will trigger after a transition is completed. The automation should create a Confluence page and generate 5 associated Jira tasks automatically. Has anyone implemented something similar or have suggestions on how to achieve this effectively?
My code Looks Likes this:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.entity.StringEntity
import org.apache.http.util.EntityUtils
// Jira and Confluence Credentials
final String jiraBaseUrl = "Jira Base URL"
final String confluenceBaseUrl = "Confluence Base URL"
final String apiToken = "Token"
final String email = "Email id"
final String authString = "${email}:${apiToken}".bytes.encodeBase64().toString()
// Issue key for the current issue
final String issueKey = issue.key
// Helper function to make REST API calls
def callRestApi(String url, String method, Map body = null)
{ CloseableHttpClient client = HttpClients.createDefault() def request if (method == "POST")
{ request = new HttpPost(url) }
else if (method == "GET") { request = new HttpGet(url) }
else { throw new UnsupportedOperationException("HTTP method ${method} is not supported in this script.") }
request.addHeader("Authorization", "Basic "" ${authString}") request.addHeader("Content-Type", "application/json") if (body != null && method == "POST")
{ request.setEntity(new StringEntity(new JsonBuilder(body).toString())) } def response = client.execute(request) def responseBody = EntityUtils.toString(response.entity) client.close()
if (response.statusLine.statusCode < 200 || response.statusLine.statusCode >= 300)
{ throw new RuntimeException("Failed API call: ${response.statusLine.statusCode} - ${responseBody}") }
return new JsonSlurper().parseText(responseBody) } // Step 1: Get Issue Details from Jira
def issueDetailsUrl = "${jiraBaseUrl}/rest/api/3/issue/${issueKey}"
def issueDetailsResponse = callRestApi(issueDetailsUrl, "GET") def issueSummary = issueDetailsResponse.fields.summary def createConfluencePage(String folderTitle)
{
// Step 2: Create a Page in Confluence
def apiUrl = "${confluenceBaseUrl}/rest/api/content" def pageData = [ type: "page", title: folderTitle, ancestors: [[id: "123456"]], space: [key: "MYSPACE"], body: [ storage: [ value: "
Tasks for issue: ${issueKey}
", representation: "storage" ] ] ] return callRestApi(apiUrl, "POST", pageData) } def addTasksToPage(String pageId) { // Step 3: Add Tasks to the Page def tasks = [ "Initial Analysis", "Stakeholder Discussion", "Documentation Draft", "Review and Feedback", "Final Submission" ] def apiUrl = "${confluenceBaseUrl}/rest/api/content/${pageId}/child/comment" tasks.each { task -> def taskData = [ body: [ storage: [ value: "incomplete${task}", representation: "storage" ] ] ] callRestApi(apiUrl, "POST", taskData) } } // Execution try { def folderTitle = "Tasks for Issue ${issueKey} - ${issueSummary}" def pageResponse = createConfluencePage(folderTitle) addTasksToPage(pageResponse.id) log.info("Confluence folder and tasks created successfully for issue ${issueKey}.") } catch (Exception e) { log.error("Error creating Confluence folder and tasks: ${e.message}", e) }
Can anyone help me to fix the issue in code. Your response will be much appreciated. Thank you in advance.