CyberKeeda In Social Media
Showing posts with label REST-API. Show all posts
Showing posts with label REST-API. Show all posts

Python Script to create new JIRA tickets using JIRA API

 


Python Script with below JIRA operations

  • Fetch JIRA ticket details using JIRA API.
  • Create JIRA ticket using JIRA API


Information to collect before using script.
  • Get your organization JIRA URL handy with you.
    • Can be retrieved from any JIRA ticket URL, respective URL has FQDN that's the JIRA URL dedicated for your organization.
  • Know your JIRA project/space name.
    • Login to any of your JIRA tickets.
    • From top navigation panel, select projects and check the name associated with the project, project name will be single word without any space.
  • Know JIRA field/mandatory fields within your ticket, before you create a ticket via API.
    • Will know how to fetch details about it from our python script, get_jita_details method can be used to get details for field

Script has one class and two methods, will know how and when to use one by one.
  • JiraHandler ( Class )
  • get_jira_details ( Method ) - Can be used to fetch JIRA ticket details.
  • create_jira_cr_ticket( Method ) - Can be used to create new JIRA ticket. 
Note : 
  • For simplicity, i have used basic authentication method to authenticate to JIRA servers, although for some sort of security instead of using plain text password, have encoded it using base64 authentication.
  • You need to ready with the Payload data json file, before you go ahead and create a new JIRA ticket, this file can be of any name but please content is in JSON format.
        payload_data.json
 

Script.

import requests
import json
import base64
from requests.auth import HTTPBasicAuth

# Inorder to encrypt/decrypt your credentials using base64 module as below.
# To encode --> base64.b64encode(bytes("random", "utf-8"))
# To decode --> base64.b64decode("cmFuZG9t").decode("utf-8")

class JiraHandler:
      
    def __init__(self, username):
        print('Loading Instnace variables...')
        self.username = username
        self.securestring = base64.b64decode("replaceItwithYourCredential").decode("utf-8")
        self.url = "https://jira.yourownjiradomain.com/rest/api/2/issue/"

    def get_jira_details(self,jira_ticket):

        try :
            
            auth = HTTPBasicAuth(self.username, self.securestring)
            get_details_url = self.url + jira_ticket

            headers = {
               "Accept": "application/json"
            }
            
            print("retrieveing", jira_ticket, "details...")
            response = requests.request(
               "GET",
               get_details_url,
               headers=headers,
               auth=auth
            )
            print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
            
        except Exception as e:
            return e



    def create_jira_cr_ticket(self, filename):
        get_details_url = self.url

        auth = HTTPBasicAuth(self.username, self.securestring)

        headers = {
           "Accept": "application/json",
           "Content-Type": "application/json"
        }
        try:
            with open(filename, "r") as re_read_file:
                payload = re_read_file.read()
                print("Creating new JIRA...")
                response = requests.request(
                   "POST",
                   get_details_url,
                   data=payload,
                   headers=headers,
                   auth=auth
                )
                print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
        except Exception as filenotfound:
            print("Can't load file..", filenotfound)



         


How to use it, 
  • Load Instance variable by calling class and provide your JIRA username as an input.
# Initiate Class and load instance variable.
d = JiraHandler("your_JIRA_Username")
  • Call method get_jira_details and add JIRA ticket as method input, for example if we want to get details about a specific JIRA ticket CKPROJ-6162, we must call the method as described below.
# Call below method to get JIRA ticket details.
d.get_jira_details("CKPROJ-6162")
  • Before calling method create_jira_cr_ticket, dump json content within a json file, for instance i have created a file named payload_data.json and it looks somehow like below.
    • Thinking how to find it, use method get_jira_details, it will give you an idea for the field used within your project's jira ticket.
{
        "fields": {
           "project":
           {
              "key": "CKPROJ"
           },
           "summary": "My Dummy Ticket created by REST API",
           "description": "Dummy CR for REST API Test",
           "confluence": "Dummy Confluence Page Link",
           "verification_steps": "Verification Plan",
           "issuetype": {
              "name": "Change Request"
               }
           }
        }
Once you are ready with the payload data, save it within a JSON file and call method providing payload_data.json file as an input to it, as an output script will return the JIRA ticket details.
# Call below method to create new JIRA ticket
d.create_jira_cr_ticket("C:\\Users\\cyberkeeda\\payload_data.json")

Hope this script can have help in any sort, for any help comment please.


Read more ...
Designed By Jackuna