JIRA REST API – Create task and subtask using python

Posted: February 6, 2019 in Linux, Scripts

In previous posts we created subtasks using Python and bash, in this one we’ll use python to do following:

  • Create task which will have variable in name
  • Script will searh if that task already exists and it’s status (resolved/opened)
  • if there is no task with status DONE, it will create it
  • it will check if there is sub tasks for that task, if there is no any it will create subtask
  • if task already exists (with status different than DONE), it will check if there is subtasks, if no it will create it
  • scripts uses client variable as parameter which will be used to build task/sub task name
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import json
import requests
import os
import urllib2
import argparse
from getpass import getpass
from json_tricks import dump,dumps

username = 'user'
password = 'Pass'

client = 'Newbie'

query = "Managed Services On-boarding " + client

def create_subtask(summary, key, line, user, password):

    headers = {"Content-Type": "application/json"}
    data = {"fields": {"project": {"key": key},"parent": {"key": line},"summary": summary,
   "issuetype": {"name": "Sub-task"}}}
    response = requests.post("https://jira.corp.company.com/rest/api/latest/issue/",
    headers=headers,data=json.dumps(data),auth=(user, password))
    out= response.json()
    print out

def jql_query(query, username, password):

    headers = {
   'Content-Type': 'application/json',}
    params = (
    ('jql', 'project="Managed Services" AND summary~"'+query+'" AND issuetype="Task" AND status!="DONE"'),
    )
    response = requests.get('https://jira.corp.company.com/rest/api/2/search', headers=headers, params=params, auth=(username, password))
    data =  response.json()
    return data

data = jql_query(query, username, password)

if data["total"] == 0:

   headers = {"Content-Type": "application/json"}
   data = {"fields":{"labels":["SERVICE_MANAGEMENT"],"reporter":{"name":"user"},"project":{"key":"MS"},"summary":"Managed Services On-boarding {client}".format(**locals()),"description":"Managed service onboarding task for {client} client".format(**locals()),"issuetype":{"name":"Task"}}}
   response = requests.post("https://jira.corp.company.com/rest/api/latest/issue/",
   headers=headers, data=json.dumps(data), auth=(username, password))
   out= response.json()
   print out
   data = jql_query(query, username, password)
   for issue in data['issues']:
      if len(issue['fields']['subtasks']) == 0:
         line = issue['key']
         create_subtask(client + ":CRM – Set up Client kick off meeting.", "MS", line, username, password)

else:
   data = jql_query(query, username, password)
   for issue in data['issues']:
      if len(issue['fields']['subtasks']) == 0:
         line = issue['key']
         create_subtask(client + ":CRM – Set up Client kick off meeting.", "MS", line, username, password)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s