How to Fix the Automation Creating a Confluence Page and 5 Tasks for Approved transition

Deepak Rai
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 24, 2024

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.

1 answer

0 votes
Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 24, 2024

Hello @Deepak Rai 

How are you triggering this code?

Where does this code exist? Are you using a third party app added to Jira to contain this code?

What is the result of the code execution? 

Deepak Rai
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 24, 2024

Thank you Trudy for response. I am triggering this code via Jira Automation actions.

I do have script runner for Jira installed in my instance however I am calling via send web request action. Every time I am getting {confluenceBaseUrl}, {authString} not declared or some parathensis error.

Let me know if you need any further details.

Best Regards,

Deepak R.

Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 24, 2024

Thank you for that additional information.

Please show us screen images that show your entire rule, and images that show the details of each step in the rule.

Is there a particular reason you are doing this with scripting rather than using native Automation Rule actions for creating issues and Confluence pages?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events