Creating JIRA issue (task) with bash and Python using REST API

Posted: June 8, 2018 in Linux, Scripts

In one of previous posts we created JIRA subtasks using REST API, in this example we’ll see how to create new JIRA task with Epic link,label, assignee and reporter

Bash:

curl -D- -u user:pass -X POST --data "{\"fields\":{\"labels\":[\"SERVICES\"],\"assignee\":{\"name\":\"emergencyadmin\"},\"reporter\":{\"name\":\"user\"},\"project\":{\"key\":\"AA\"},\"summary\":\"Create user account in Local AD.\",\"description\":\"Create user account in Local AD.\",\"issuetype\":{\"name\":\"Managed Service\"},\"customfield_10107\":{\"id\":\"10505\"},\"customfield_10006\":{\"CP-3289\"}}}" -H "Content-Type:application/json" https://jira.company.com/rest/api/latest/issue/

customfield_10006-epic link
customfield_10107-client account

Python:

 

#!/usr/bin/python3

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

parser = argparse.ArgumentParser()
parser.add_argument('-pass', '-password', dest='password', help='svc-rundeck password.')
args = parser.parse_args()


username = 'svc'
password = args.password



def create_subtask(summary, description, key, line, user, password):
      headers = {"Content-Type": "application/json"}
      data = {"fields": {"project": {"key": key},"parent": {"key": line},"summary":summary, "description": description,
     "issuetype": {"name": "Sub-task"},"customfield_10107": {"id": "10400"}}}
      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)

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

params = (
    ('jql', 'project="Technology" AND summary~"New User*" 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()

for issue in data['issues']:
    if len(issue['fields']['subtasks']) == 0:
         line = issue['key']

         #1

         create_subtask("Create user account in Local AD.", "Create user account in Local AD.", "TECH", 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