0

I have following code which creates subtask in JIRA

inf = open('/var/lib/rundeck/output.txt')
for line in inf:
                print line
                headers = {'Content-Type': 'application/json',}
                data = '{"fields":{"project":{"key":"TECH"},"parent":{"key":line},"summary":"Create AD List ","description":"","issuetype":{"name":"Sub-task"},"customfield_10107":{"id":"10400"}}}'
                response = requests.post('https://jira.company.com/rest/api/latest/issue/', headers=headers, data=data, auth=('user', 'pass'))

inf.close()

i have a file (output.txt), python for every line found (TECH-XXX) printss all lines, it should trigger script above.

when i hard-code key "key":"TECH-1147" instead of "key":line script generates subtask, but when substituting variable (line), nothing happens

Ouptut.txt:

TECH-1234
TECH-1345
.........

i convereted this code:

curl -D- -u: user:Pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"Create AD List of all Active Users\",\"description\":\"some description\",\"issuetype\":{\"name\":\"Sub-task\"},\"customfield_10107\":{\"id\":\"10400\"}}}" -H "Content-Type:application/json" https://company.com/rest/api/latest/issue/

using this https://curl.trillworks.com/

tried also {"key":'"' + line + '"'}

and getting {u'errorMessages': [u'The issue no longer exists.'], u'errors': {}}

Issue is TECH-1247 (variable) which definitely exists

6
  • you are treating line as part of the string, not as a variable Commented Apr 2, 2018 at 17:11
  • okay,how to convert it to variable ? Commented Apr 2, 2018 at 17:19
  • using string formatting is one way Commented Apr 2, 2018 at 17:24
  • okay i tried data = "%s" % line and put it "key":data but the same, when hard-code it it works Commented Apr 2, 2018 at 17:31
  • After making the string formatting update, have you tried printing response.json() (or json.dumps(resp_json)) to see what the response looks like? Maybe it will show an error. Commented Apr 2, 2018 at 17:47

2 Answers 2

3

Maybe try using rstrip() to trip any trailing whitespace/newlines and json.dumps() so the data isn't passed as form-encoded...

import requests
import json

with open("output.txt", "rb") as infile:
    for line in infile:
        headers = {"Content-Type": "application/json"}
        data = {"fields": {
                "project": {"key": "TECH"},
                "parent": {"key": line.rstrip()},
                "summary": "Create AD List ",
                "description": "",
                "issuetype": {"name": "Sub-task"},
                "customfield_10107": {"id": "10400"}
                }}

        response = requests.post("https://jira.company.com/rest/api/latest/issue/",
                                 headers=headers,
                                 data=json.dumps(data),
                                 auth=("user", "pass"))

Like another answer said, if you use json param instead of the data param, the dict will automatically be encoded for you and the Content-Type set to application/json.

See here for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer, because python does not strip the trailing newline from lines. That is, the key is meant to "TECH-X" and not "TECH-X\n".
1

line is not interpreted as a variable. It's just a string. One solution is to use the % operator for string formatting:

inf = open('/var/lib/rundeck/output.txt')
for line in inf:
    print line
    headers = {'Content-Type': 'application/json',}
    data = '{"fields":{"project":{"key":"TECH"},"parent":{"key":%s},"summary":"Create AD List ","description":"","issuetype":{"name":"Sub-task"},"customfield_10107":{"id":"10400"}}}' % line
    response = requests.post('https://jira.corp.hentsu.com/rest/api/latest/issue/', headers=headers, data=data, auth=('user', 'pass'))

inf.close()

Note that line was replaced with %s and then % line was added to the end. This will replace the %s with the value of the variable line.

6 Comments

doesn't work, it prints line but not passing to curl
What do you mean by "doesn't work"? Also, I don't see where you're using cURL anywhere. You may want to print the value of data to make sure it's correct.
i mean, subtask is not created, it's CURL command just converted to works with python, it prints TECH-1147 (which is parameter for above command)
data should not print TECH-1147 since it contains a string of JSON...Also, I'm not sure what you mean by "subtask." I'm assuming requests is the Python requests library in which case, requests.post is just an ordinary Python function.
it prints because i put print line i just updated question-it's bash conmmand converted to work under python
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.