Auto create sub-task using python

Sradha Suman February 15, 2021

Hello,

 I was trying to automate jira like ( project name, key , type, templates, issues) but not able to automate sub-task as it's asking for parent key which is automated using python.

Anyone knows how to automate the sub-task in restApi using python?

THIS IS THE CODE SO FAR.

import requests
import json
import sys
import pandas as pd
from openpyxl import load_workbook
import xlrd


loc_data = ('data.json')
with open(loc_data) as json_file:
data_dict = json.load(json_file)


loc_creds = ('creds.json')
with open(loc_creds) as f:
creds_dict = json.load(f)
# print(creds_dict.get("userName"))
# print(creds_dict.get("passWord"))
userName = creds_dict.get("userName")
passWord = creds_dict.get("passWord")
jiraUrl = creds_dict.get("jiraUrl")


username=f"{userName}"
password=f"{passWord}"
JIRA_URL = f"{jiraUrl}"

url=JIRA_URL + '/rest/api/2/project'
url_issues=JIRA_URL +'/rest/api/2/issue/bulk'

headers={
"Accept": "application/json",
"Content-Type": "application/json"
}

projName = []
projType = []
projTemplate = []
issueName = []
issueSumm = []
issueDescrip = []
# subSumm = []
num = 0
x = 0

for data in data_dict['projects']:
projName.append(data['name'])
projType.append(data["type"])
projTemplate.append(data["template"])

# for i in range(sheet.nrows):
# temp = sheet.cell_value(i, 4)
# subSumm.append(temp)


for pointer in projName:
for data in data_dict['projects'][num]['issues']:
issueName.append(data['name'])
issueSumm.append(data['summary'])
issueDescrip.append(data['description'])

key = projName[num][0:2].upper() + projName[num][-1].upper()

if projType[num] == "software":
template = str(f"com.pyxis.greenhopper.jira:gh-simplified-{projTemplate[num]}")
elif projType[num] == "service_desk":
template = str(f"com.atlassian.servicedesk:simplified-{projTemplate[num]}")
elif projType[num] == "business":
template = str(f"com.atlassian.jira-core-project-templates:jira-core-simplified-{projTemplate[num]}")
else:
print("invalid...")

payload=json.dumps(
{
# "notificationScheme": 10021,
"description": "Project description goes here...",
"leadAccountId": "5ff2c46fa9d0300107bc320a",
"url": "http://atlassian.net",
"projectTemplateKey": f"{template}",
"avatarId": 10200,
# "issueSecurityScheme": 10001,
"name": f"{projName[num]}",
# "permissionScheme": 10011,
"assigneeType": "PROJECT_LEAD",
"projectTypeKey": f"{projType[num]}",
"key": f"{key}",
# "categoryId": 10120

}
)
response=requests.post(url,headers=headers,data=payload,auth=(username,password))

for data in data_dict['projects'][num]['issues']:
totalIssues = len(data_dict['projects'][num]['issues'])

payload_issues=json.dumps(
{
#for issues
"issueUpdates":[
{
"update":{},
"fields":
{
"project":
{
"key": f"{key}"
},
"summary": f"{issueSumm[x]}",
"description": f"{issueDescrip[x]}",
"issuetype": {
"name": f"{issueName[x]}"
}
}
},
{
"update":{},
"fields":
{
"project":
{
"key": f"{key}"
},
"customfield_10011": "Random epic",
"summary": "summary goes here..",
"description": "description goes here..",
"issuetype": {
"name": "Epic"
}
}
}
]
}
)
response_issues=requests.post(url_issues,headers=headers,data=payload_issues,auth=(username,password))
if x == totalIssues-1:
break
else:
x += 1



# response_subtasks=requests.post(url_issues,headers=headers,data=payload_subtasks,auth=(username,password))

print(response.text)
num += 1

 

1 answer

1 vote
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2021

Hi @Sradha Suman 

The model I follow is :

  1. Create the parent - the return JSON has the Jira key of this parent
  2. Create the subtask(s), using the key from 1 above

I'm not sure if it's as simple as I've described, or if you've got some complication which you haven't mentioned here. To be honest, I've never used Python, I use C#, so tracing through your code isn't easy, so I haven't tried yet. You really need a way of keeping the parent key when it's created.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events