Bhavya-org / Scripts

0 stars 0 forks source link

DSAR -2 #55

Open Bhavya-ss opened 1 week ago

Bhavya-ss commented 1 week ago

def issueKey = issue.key def requestType = issue.fields.customfield_10010?.requestType?.name def labels = issue.fields.labels?.join(", ")

logger.info("Request Type: ${requestType}") logger.info("Labels: ${labels}")

for (item in changelog.items) { logger.info("Changelog field: ${item.field}") if (item.field == "Request Type") { logger.info("Detected change in customfield_10010")

    // Log current and expected values for clarity
    logger.info("Current request type: ${requestType}")
    logger.info("Expected request type: Fix an account problem")
    logger.info("Labels contain 'test': ${labels.contains("test")}")
    logger.info("Labels contain 'fix': ${labels.contains("fix")}")

    // Check conditions
    if (requestType == "Fix an account problem" && !labels.contains("test") && !labels.contains("fix")) {
        logger.info("Conditions met, proceeding with updates")

        // Update the request participants field with the correct structure
        def newRequestParticipants = [
            ["accountId": "6305b88c958ffd78c6e0e59e"],
            ["accountId": "630a0d15ec02b5f28b63a024"]
        ]

        // Calculate due date 30 days from today
        def today = new Date()
        def dueDate = today + 30

        // Update issue fields
        def result = put("/rest/api/2/issue/${issueKey}")
            .queryString("overrideScreenSecurity", Boolean.TRUE)
            .header('Content-Type', 'application/json')
            .body([
                fields: [
                    customfield_10127: newRequestParticipants,
                    customfield_10005: [value: "Normal"],
                    labels: ["dser_legal_sign_of_requested"],
                    dueDate: dueDate.format('yyyy-MM-dd') // Corrected field name
                ]
            ])
            .asString()

        logger.info("Issue fields updated. Response: ${result}")

        // Comment text
        def commentText1 = """
            AUTOMATED MESSAGE1:
            This ticket with a HIGH priority is in status "waiting for creator" for 1 day.
            The ticket will be deprioritized to medium after 5 days.
        """

        // Get existing comments for the issue
        def commentsResult = get("/rest/api/2/issue/${issueKey}/comment")
            .header('Content-Type', 'application/json')
            .asObject(Map)

        logger.info("Fetched existing comments. Response: ${commentsResult}")

        // Check for duplicate comments
        def comments = commentsResult.body.comments
        def commentAlreadyExists = comments.any { comment ->
            comment.body.contains(commentText1.trim())
        }

        if (!commentAlreadyExists) {
            // Add the new comment only if it doesn't already exist
            def addCommentResult = post("/rest/api/2/issue/${issueKey}/comment")
                .header('Content-Type', 'application/json')
                .body(["body": commentText1.trim()])
                .asObject(Map)

            logger.info("Comment added. Response: ${addCommentResult}")
        } else {
            logger.info("Duplicate comment found, not adding a new one.")
        }

        // Transition the issue
        def transition = post("/rest/api/2/issue/${issueKey}/transitions")
            .header("Content-Type", "application/json")
            .body([transition: [id: 11]])
            .asObject(Map)

        logger.info("Transitioned issue. Response: ${transition}")
    } else {
        logger.info("Condition not met, skipping the actions.")
    }
}

}