Last active
July 13, 2025 16:22
-
-
Save vikrantyadav11/d1fb60f4571f48afdb4b95edc2f699db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import groovy.json.JsonOutput | |
| import java.text.SimpleDateFormat | |
| // Step 1: Get version ID from the event | |
| def versionData = binding.variables.version as Map | |
| def versionId = versionData?.id | |
| if (!versionId) { | |
| logger.warn("⚠️ Version ID not found in binding. Skipping sync.") | |
| return | |
| } | |
| // Step 2: Get full version details from API | |
| def versionResponse = get("/rest/api/3/version/${versionId}") | |
| .header('Accept', 'application/json') | |
| .asObject(Map) | |
| if (versionResponse.status != 200) { | |
| logger.error("❌ Failed to fetch version details from ID ${versionId}: ${versionResponse.status}") | |
| return | |
| } | |
| def versionDetails = versionResponse.body as Map | |
| // Step 3: Extract fields from fetched version | |
| def name = versionDetails['name'] ?: "N/A" | |
| def description = versionDetails['description'] ?: "" | |
| def released = versionDetails['released'] ?: false | |
| def archived = versionDetails['archived'] ?: false | |
| def overdue = versionDetails['overdue'] | |
| def projectId = versionDetails['projectId'] | |
| def rawReleaseDate = versionDetails['userReleaseDate'] as String | |
| def rawStartDate = versionDetails['userStartDate'] as String | |
| // Step 4: Convert dates (dd/MMM/yy → yyyy-MM-dd) | |
| def convertToIsoDate = { String dateStr -> | |
| try { | |
| def inputFormat = new SimpleDateFormat("dd/MMM/yy", Locale.ENGLISH) | |
| def outputFormat = new SimpleDateFormat("yyyy-MM-dd") | |
| return outputFormat.format(inputFormat.parse(dateStr)) | |
| } catch (Exception e) { | |
| logger.warn("⚠️ Failed to parse date: '${dateStr}'") | |
| return null | |
| } | |
| } | |
| def releaseDate = rawReleaseDate ? convertToIsoDate(rawReleaseDate) : null | |
| def startDate = rawStartDate ? convertToIsoDate(rawStartDate) : null | |
| // Step 5: Set destination project(s) | |
| def destinationProjectIds = [10306] // Add more project IDs here as needed | |
| // Step 6: Loop through destination projects | |
| destinationProjectIds.each { destinationProjectId -> | |
| logger.info("🔁 Syncing version '${name}' to project ${destinationProjectId}") | |
| // Step 6.1: Check if version exists | |
| def versionsResponse = get("/rest/api/3/project/${destinationProjectId}/versions") | |
| .header('Accept', 'application/json') | |
| .asObject(List) | |
| if (versionsResponse.status != 200) { | |
| logger.error("❌ Failed to fetch versions from project ${destinationProjectId}: ${versionsResponse.status}") | |
| return | |
| } | |
| def existingVersion = (versionsResponse.body as List<Map>).find { it.name == name } | |
| def versionPayload = [ | |
| name : name, | |
| description : description, | |
| released : released, | |
| archived : archived | |
| ] | |
| if (releaseDate) versionPayload.releaseDate = releaseDate | |
| if (startDate) versionPayload.startDate = startDate | |
| if (existingVersion) { | |
| // 🔄 Update | |
| def updateResponse = put("/rest/api/3/version/${existingVersion.id}") | |
| .header('Content-Type', 'application/json') | |
| .body(JsonOutput.toJson(versionPayload)) | |
| .asObject(Map) | |
| if (updateResponse.status == 200) { | |
| logger.info("✅ Updated version '${name}' in project ${destinationProjectId}") | |
| } else { | |
| logger.error("❌ Failed to update version in project ${destinationProjectId}: ${updateResponse.status} - ${JsonOutput.prettyPrint(JsonOutput.toJson(updateResponse.body))}") | |
| } | |
| } else { | |
| // ➕ Create | |
| def createPayload = [:] | |
| createPayload.putAll(versionPayload) | |
| createPayload.projectId = destinationProjectId | |
| def createResponse = post("/rest/api/3/version") | |
| .header('Content-Type', 'application/json') | |
| .body(JsonOutput.toJson(createPayload)) | |
| .asObject(Map) | |
| if (createResponse.status == 201) { | |
| logger.info("✅ Created version '${name}' in project ${destinationProjectId}") | |
| } else { | |
| logger.error("❌ Failed to create version in project ${destinationProjectId}: ${createResponse.status} - ${JsonOutput.prettyPrint(JsonOutput.toJson(createResponse.body))}") | |
| } | |
| } | |
| } | |
| // Final Log | |
| logger.info("📦 Version Synced From Source Project:") | |
| logger.info(" - Name: ${name}") | |
| logger.info(" - Description: ${description}") | |
| logger.info(" - Released: ${released}") | |
| logger.info(" - Archived: ${archived}") | |
| logger.info(" - Overdue: ${overdue}") | |
| logger.info(" - Source Project ID: ${projectId}") | |
| logger.info(" - Raw Release Date: ${rawReleaseDate} ➡️ ${releaseDate}") | |
| logger.info(" - Raw Start Date: ${rawStartDate} ➡️ ${startDate}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment