python script to check Jira status

Viewed 548

I need a python script that will check Jira status in particular time interval (1 minutes) and once Jira ticket status is approved it will break the loop and proceed with further step (command: terraform apply). Could you please help out with code.

Using curl to fetch the Jira status:

curl -u username:password -X GET -H "Content-Type: application/json" 
     https://server-url/rest/api/2/issue/JRA-1?fields=status
2 Answers

You can refer to the following program this will resolve this issue

import time
import json, sys, requests, os

def getStatus():
    url = "https://server-url/rest/api/2/issue/JRA-1"
    headers = {
    "Accept": "application/json",
    "Authorization": "Basic base64encodethese(username:password)"
    }
    response = requests.request(
    "GET",
    url,
    headers=headers
    )
    jsonResponse = json.loads(response.text)
    return jsonResponse["status"]

def main():
    while(getStatus != "APPROVED"):
        time.sleep(50) 
    os.system("terraform apply")

Checking Jira status every single minute might be demanding for the server -mainly if there are other similar requests (on other issues' statuses) sent to the server.

Alternatively, you can discuss with Jira administrators to adjust the workflow. Once the issue is transitioned to APPROVED status, they can trigger/call terraform tasks (via webhooks). Only one call is sent.

Related