Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Auto create sub-task using python Edited

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

0 votes
Warren Community Leader Feb 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

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you