Hi Vinay,
Try this code:
// Import necessary Jira and Confluence classes
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.FieldManager
import com.atlassian.sal.api.net.Request
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
// ============================ Configuration Section ============================
// Configuration variables for Jira project keys and Confluence settings
def projectKey1 = "PROJECT1" // Replace with the first project's key
def projectKey2 = "PROJECT2" // Replace with the second project's key
def createConfluencePage = true // Set to true to create a Confluence page
def confluenceSpaceKey = "SPACE" // Confluence space key (if enabled)
def confluencePageTitle = "Jira Fields Comparison of ${projectKey1} vs ${projectKey2}" // Confluence page title
// ============================ Project Setup Section ============================
// Fetch Jira project objects using the project manager
def projectManager = ComponentAccessor.getProjectManager()
def project1 = projectManager.getProjectByCurrentKey(projectKey1)
def project2 = projectManager.getProjectByCurrentKey(projectKey2)
// Validate project existence
if (!project1 || !project2) {
throw new IllegalArgumentException("Invalid project keys provided!")
}
// ============================ Field Manager Section ============================
// Retrieve the field manager to access fields for comparison
def fieldManager = ComponentAccessor.getFieldManager()
// Fetch all fields for both projects
def fieldsProject1 = fieldManager.getAllFields()
def fieldsProject2 = fieldManager.getAllFields()
// ============================ Field Comparison Section ============================
// Compare fields between the two projects
def fieldComparison = []
fieldsProject1.each { field1 ->
def matchingField = fieldsProject2.find { field2 -> field2.getId() == field1.getId() }
fieldComparison << [
field1.getName(),
field1.getFieldType().getName(), // Using getName() to retrieve the field type name
matchingField ? "Yes" : "No",
matchingField ? matchingField.getFieldType().getName() : "N/A"
]
}
// ============================ Confluence Page Generation Section ============================
// Generate Confluence page based on the comparison results
def generateConfluencePage = {
// Get application link to Confluence
def appLinkService = ComponentAccessor.getComponent(ApplicationLinkService)
def confluenceLink = appLinkService.getPrimaryApplicationLink("confluence")
if (!confluenceLink) {
throw new IllegalStateException("No Confluence application link configured!")
}
// Prepare table data for Confluence page
def tableContent = fieldComparison.collect { row ->
"| ${row[0]} | ${row[1]} | ${row[2]} | ${row[3]} |"
}.join("\n")
def confluenceContent = """
h1. Jira Fields Comparison: ${projectKey1} vs ${projectKey2}
|| Field Name (Project 1) || Field Type (Project 1) || Exists in Project 2? || Field Type (Project 2) ||
${tableContent}
"""
// Send a request to create a Confluence page using the API
def authenticatedRequestFactory = confluenceLink.createAuthenticatedRequestFactory()
authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "/rest/api/content")
.addHeader("Content-Type", "application/json")
.setRequestBody("""{
"type": "page",
"title": "${confluencePageTitle}",
"space": {"key": "${confluenceSpaceKey}"},
"body": {
"storage": {
"value": "${confluenceContent}",
"representation": "wiki"
}
}
}""")
.execute({ response ->
if (response.statusCode == 200 || response.statusCode == 201) {
println "Confluence page created successfully!"
} else {
println "Failed to create Confluence page: ${response.statusCode} - ${response.responseBodyAsString}"
}
})
}
// ============================ Main Execution Section ============================
// Execute the output based on the createConfluencePage flag
if (createConfluencePage) {
// Generate Confluence page
generateConfluencePage()
} else {
// Add code to generate Excel file if required
// generateExcelFile()
println "Excel generation functionality is not implemented in this version."
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.