Bhavya-org / Scripts

0 stars 0 forks source link

update watcher n request particpants #5

Open Bhavya-ss opened 1 month ago

Bhavya-ss commented 1 month ago

def issueKey = 'JP-105'

// Fetch the issue details def result = get('/rest/api/2/issue/' + issueKey) .header('Content-Type', 'application/json') .asObject(Map)

// Return the result if the GET request is successful if (result.status == 200) { def fields = result.body.fields def requestType = fields.customfield_10010?.requestType?.name println(requestType) def changeType = fields.customfield_10005?.value

// Check conditions
if (['IT Support', 'Get IT help', 'Fix an account problem'].contains(requestType) &&
        !['Normal', 'Standard'].contains(changeType)) {
    def watcherAccountId = '6305b88c958ffd78c6e0e59e' // Use the account ID of the watcher
    def addWatcher = post('/rest/api/3/issue/' + issueKey + '/watchers')
            .header('Content-Type', 'application/json')
            .body("\"${watcherAccountId}\"")
            .asString()

    def newRequestParticipants = [
        [
            "accountId": "6305b88c958ffd78c6e0e59e"
        ],
        [
            "accountId": "630a0d15ec02b5f28b63a024"
        ]
    ]

    def updateResult = put('/rest/api/2/issue/' + issueKey)
            .header('Content-Type', 'application/json')
            .body([
                    fields: [
                            customfield_10127: newRequestParticipants // Use the correct custom field ID
                    ]
            ])
            .asString()

    // Return a message if the update is successful
    if (updateResult.status == 204) {
        return "Request participants updated successfully."
    }
} else if (['Get a guest wifi account', 'Request admin access'].contains(requestType)) {
    def newRequestParticipants = [
        [
            "accountId": "6305b88c958ffd78c6e0e59e"
        ],
        [
            "accountId": "630a0d15ec02b5f28b63a024"
        ]
    ]

    def updateResult = put('/rest/api/2/issue/' + issueKey)
            .header('Content-Type', 'application/json')
            .body([
                    fields: [
                            customfield_10127: newRequestParticipants // Use the correct custom field ID
                    ]
            ])
            .asString()

    // Return a message if the update is successful
    if (updateResult.status == 204) {
        return "Request participants updated successfully."
    }
}

}

// Return an error message if the GET request fails return "Failed to find issue: Status: ${result.status} ${result.body}"