Bhavya-org / Scripts

0 stars 0 forks source link

DSAR: request approval from Legal 2 #7

Open sani-d opened 1 month ago

sani-d commented 1 month ago

import groovy.json.JsonBuilder

def issueKey = issue.key

// Function to make REST API GET requests def getRequest(String url) { return get(url) .header('Content-Type', 'application/json') .asObject(Map) }

// Function to make REST API PUT requests def putRequest(String url, Map body) { return put(url) .header('Content-Type', 'application/json') .body(body) .asObject(Map) }

// Function to make REST API POST requests def postRequest(String url, Map body) { return post(url) .header('Content-Type', 'application/json') .body(body) .asObject(Map) }

// Fetch the issue details def issueDetails = getRequest('/rest/api/2/issue/' + issueKey)

if (issueDetails.status == 200) { def fields = issueDetails.body.fields def requestType = fields.customfield_10848?.requestType?.name

// Check conditions
if (requestType == "DSAR request" &&
    !fields.labels?.any { it.contains("dsar_legal_first_signoff_requested") } &&
    !fields.labels?.contains("dsar_legal_first_signoff_approved")) {
    // Update the request participants field with the correct structure
    def newRequestParticipants = [
        [
            "accountId": "60ce94bef650540069272b23"
        ],
        [
            "accountId": "5db2e8387b20d80c2a25cbba"
        ]
    ]

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

    // Update the issue fields
    def updateResult = putRequest('/rest/api/2/issue/' + issueKey, [
        fields: [
            customfield_10127: newRequestParticipants,
            customfield_10993: [value: "Financial Crime"],
            customfield_11382: [value: "GDPR (DSAR/deletion)"],
            duedate: dueDate.format('yyyy-MM-dd')
        ]
    ])

    if (updateResult.status == 204) {
        // Define the comment text to be added
        def commentText = """
        Legal: Please review this request for validity and comment when approved to action for PSO!
        """

        // Get existing comments for the issue
        def commentsResult = getRequest('/rest/api/2/issue/' + issueKey + '/comment')

        if (commentsResult.status == 200 && commentsResult.body.containsKey('comments')) {
            def comments = commentsResult.body.comments

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

            // Add the new comment only if it doesn't already exist
            if (!commentAlreadyExists) {
                def addCommentResult = postRequest('/rest/api/2/issue/' + issueKey + '/comment', ["body": commentText.trim()])
                if (addCommentResult.status == 201) {
                    // Transition the issue
                    def transitionResult = postRequest("/rest/api/3/issue/${issueKey}/transitions", ["transition": ["id": "1061"]])
                    if (transitionResult.status != 204) {
                        // Handle failed transition
                        // Add any necessary error handling here
                    }
                }
            }
        }
    }
}

}