JIRA REST API – Create Task with assigned Sprint using PYTHON

Posted: April 10, 2019 in Scripts

In previous article we created JIRA task, In this one we’ll assign sprint (first future one) and assign original estimate (4h)

Basic error handling is created,in case wrong sprint is provided, in that case Task will be created without sprint.

 

#!/usr/bin/python3

import sys
import json
import requests
import argparse
import datetime
import argparse
from dateutil.relativedelta import *
from json_tricks import dump,dumps

month = datetime.datetime.now()

this_month = month.strftime("%B")
parser = argparse.ArgumentParser()
parser.add_argument('-pass', '-password', dest='password', help='password.')
args = parser.parse_args()

username = 'rundeck'
password = args.password
assignee = 'user1'
reporter = 'user2'

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

params = (('state', 'active'),)

response = requests.get('https://jira.company.com//rest/agile/1.0/board/62/sprint', headers=headers, params=params, auth=(username, password))

data = response.json()
for i in range(0,len(data['values'])):
name = data['values'][i]['name']
if 'some sprint' in name:
sprintid = data['values'][i]['id']
sprintid = sprintid + 1

def create_task(reporter, assignee, user, passwd, this_month, summary):

headers = {"Content-Type": "application/json"}
data = {"fields":{"project":{"key":"CP"},"issuetype":{"name":"Task"},"reporter":{"name":reporter},"assignee":{"name":assignee},
"summary":summary,
"description":"some description",
"customfield_10107":{"id":"10400"},
"customfield_11300":{"value":"Engineering"},
"customfield_11301":{"value":"Zemoon"},
"timetracking":{"originalEstimate":240},
"customfield_10005":sprintid}}
response = requests.post('https://jira.company.com/rest/api/latest/issue/', data = json.dumps(data),auth=(user, passwd), headers= headers)
print (response.content)
out = response.content

if b'errorMessages' in out:
headers = {"Content-Type": "application/json"}
data = {"fields":{"project":{"key":"CP"},"issuetype":{"name":"Task"},"reporter":{"name":reporter},"assignee":{"name":assignee},
"summary":summary,
"description":"description",
"customfield_10107":{"id":"10400"},
"customfield_11300":{"value":"Engineering"},
"customfield_11301":{"value":"Zemoon"},
"timetracking":{"originalEstimate":240}}}
response = requests.post('https://jira.company.com/rest/api/latest/issue/', data = json.dumps(data),auth=(user, passwd), headers= headers)
print (response.content)
create_task(reporter, assignee, username, password, this_month, "Sales - 1/5 {this_month} - {assignee}".format(**locals()))

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