z4r / python-rtkit

Python Api for Request Tracker's REST interface
http://z4r.github.com/python-rtkit/
Other
68 stars 44 forks source link

Getting ValueError: need more than 1 value to unpack #31

Closed avorum closed 11 years ago

avorum commented 11 years ago

I have code in which I'm attempting to create a single RT ticket as follows:

from rtkit.resource import RTResource
from rtkit.authenticators import BasicAuthenticator, CookieAuthenticator
from rtkit.errors import RTResourceError

from rtkit import set_logging
import logging
set_logging('debug')
logger = logging.getLogger('rtkit')

resource = RTResource('https://rt.example.com/REST/1.0/', 'user', 'pass, CookieAuthenticator)

#create a ticket
content = {
    'content': {
        'Queue': 'Vulnerabilities',
        'Subject' : 'Test Vulnerability for Script Dev',
        'Text' : 'I\'m a thing',
        'CF-Priority' : 'Low -Needs to be completed in > 30',
        'CF-Device or Area' : 'Agile Arrow',
        'CF-Time Estimated' : 'Days',
        #'CF-Is This Maintenance?' : 'Not_maintenance',
        #'CF-Type of change/risk' : 'Non-Prod Change/Notes (requires post-approval)',
        'CF-CVSS Score' : '2.6',
        #'CF-Approved?' : 'No',
        'CF-Vulnerability Risk' : 'Low',
        'CF-Discovered By (Company)' : 'MyCompany',
        #'CF-Discovered By (Program)' : 'MyProgram',
        }
    }
try:
    print content
    response = resource.post(path='ticket/new', payload=content,)
    #response = resource.post(path='ticket/4862')
    logger.info(response.parsed)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)

All the commented fields in the content dict don't work. If I submit that request I get the following error message:

Traceback (most recent call last):
  File "resttest.py", line 33, in <module>
    response = resource.post(path='ticket/new', payload=content,)
  File "build\bdist.win32\egg\rtkit\resource.py", line 29, in post
  File "build\bdist.win32\egg\rtkit\resource.py", line 49, in request
  File "build\bdist.win32\egg\rtkit\resource.py", line 104, in __init__
  File "build\bdist.win32\egg\rtkit\parser.py", line 49, in parse
  File "build\bdist.win32\egg\rtkit\parser.py", line 79, in decode_comment
ValueError: need more than 1 value to unpack

Additionally, I get this error if I omit the CF- at the beginning of any the fields that work otherwise. However, other mispellings of the field name (ie omitting a ?) I get the error for an invalid field name. I'd try and put a print line in decode_comment to show what's in the lines variable that being parsed and throwing the error but it doesn't seem to work.Do you know why this problem happens and how to fix it?

z4r commented 11 years ago

Hi @avorum, do you have any log of this call?

avorum commented 11 years ago

Do you mean a log of the request sent to the RT server as a result of this script?

z4r commented 11 years ago

yes, the output log of the script

avorum commented 11 years ago

My company seems to only have error logs for our RT Server and they have no entries corresponding to the requests made by my script. Is there a way to modify any of the files in the module or some way to interact with the module to figure out what's going on? Given the nature of my corporate environment it would be highly difficult to get the logging policies changed for debugging this issue.

avorum commented 11 years ago

@z4r I'm going to try and get some proper logging for this issue from the people who manage our RT service, post if you have another idea of how to figure out what's going on.

avorum commented 11 years ago

after running some tests ( where i changed some custom field names) I've found that the code only breaks on fields with special characters (ie () / or ?)

z4r commented 11 years ago

Anyway i'm talking about python log, not server one. I need to reproduce it to add a new test.

avorum commented 11 years ago

Here's what I got from logfiles:

access_log:

172.23.80.35 - - [30/Jul/2013:09:08:11 -0400] "POST /REST/1.0/ HTTP/1.1" 200 79 "-" "Python-urllib/2.7"
172.25.80.200 - - [30/Jul/2013:09:08:11 -0400] "POST /REST/1.0/ticket/new HTTP/1.1" 200 341 "-" "Python-urllib/2.7"

No corresponding entries in the error_log

avorum commented 11 years ago

I apologize then, I'm rather new to python. What exactly are you looking for? Is there a log file made by python automatically? Or are you looking for some specific information? There is some logging included in my output in the original post.

z4r commented 11 years ago

When you run the script did you have something before the traceback output?

avorum commented 11 years ago

Wow, I thought I included that, my bad.

{'content': {'Queue': 'Vulnerabilities', 'CF-Priority': 'Low -Needs to be c
ompleted in > 30', 'CF-Is This Maintenance?': 'Not_maintenance', 'CF-Device or A
rea': 'My Device', 'Owner': 'jgreen', 'Text': "I'm a thing", 'CF-Vulnerability
 Risk': 'Low', 'CF-Time Estimated': 'Days', 'CF-CVSS Score': '2.6', 'Subject': '
Test Vulnerability for Script Dev'}}
[DEBUG] POST ticket/new
[DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Ac
cept': 'text/plain'}
[DEBUG] "content=Queue: Vulnerabilities\nCF-Priority: Low -Needs to be comp
leted in > 30\nCF-Is This Maintenance?: Not_maintenance\nCF-Device or Area: My Device\nOwner: jgreen\nText: I'm a thing\nCF-Vulnerability Risk: Low\nCF-Time E
stimated: Days\nCF-CVSS Score: 2.6\nSubject: Test Vulnerability for Script Dev"
[INFO] POST
[INFO] https://rt.myrtserver.com/REST/1.0/ticket/new
[DEBUG] HTTP_STATUS: 200 OK
[DEBUG] "RT/3.6.6 409 Syntax Error\n\n# Syntax error.\n\nQueue: Vulnerabilities\
nCF-Priority: Low -Needs to be completed in > 30\n>> CF-Is This Maintenance
?: Not_maintenance\nCF-Device or Area: My Device\nOwner: jgreen\nText: I'm a t
hing\nCF-Vulnerability Risk: Low\nCF-Time Estimated: Days\nCF-CVSS Score: 2.6\nS
ubject: Test Vulnerability for Script Dev\n\n"
Traceback (most recent call last):
  File "resttest.py", line 34, in <module>
    response = resource.post(path='ticket/new', payload=content,)
  File "build\bdist.win32\egg\rtkit\resource.py", line 29, in post
  File "build\bdist.win32\egg\rtkit\resource.py", line 49, in request
  File "build\bdist.win32\egg\rtkit\resource.py", line 104, in __init__
  File "build\bdist.win32\egg\rtkit\parser.py", line 49, in parse
  File "build\bdist.win32\egg\rtkit\parser.py", line 79, in decode_comment
ValueError: need more than 1 value to unpack
titilambert commented 11 years ago

Hello !

Maybe you could check this pull request #32.

Bye

avorum commented 11 years ago

@titilambert does updating the line in the module downloaded on my system and running python setup.py install accomplish what's needed to fix the install on my system as regarding the commit in request #32?

titilambert commented 11 years ago

Normally yes :)

z4r commented 11 years ago

HI @avorum, could you try it again?

avorum commented 11 years ago

After including those updates I now get the following output:

{'content': {'Queue': 'Vulnerabilities', 'CF-Priority': 'Low -Needs to be c
ompleted in > 30', 'CF-Is This Maintenance?': 'Not_maintenance', 'CF-Device or A
rea': 'My Device', 'Owner': 'jgreen', 'Text': "I'm a thing", 'CF-Vulnerability
 Risk': 'Low', 'CF-Time Estimated': 'Days', 'CF-CVSS Score': '2.6', 'Subject': '
Test Vulnerability for Script Dev'}}
[DEBUG] POST ticket/new
[DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Ac
cept': 'text/plain'}
[DEBUG] "content=CF-CVSS%20Score: 2.6\nCF-Device%20or%20Area: My%20Device\nTex
t: I'm a thing\nCF-Time%20Estimated: Days\nQueue: Vulnerabilities\nCF-Vulnerabil
ity%20Risk: Low\nOwner: jgreen\nCF-HDMS%20Priority: Low%20-Needs%20to%20be%20com
pleted%20in%20%3E%2030\nCF-Is%20This%20Maintenance%3F: Not_maintenance\nSubject:
 Test Vulnerability for Script Dev"
[INFO] POST
[INFO] https://rt.hdms.com/REST/1.0/ticket/new
[DEBUG] HTTP_STATUS: 200 OK
[DEBUG] "RT/3.6.6 409 Syntax Error\n\n# Syntax error.\n\nCF-CVSS Score: 2.6\nCF-
Device or Area: My Device\nText: I'm a thing\nCF-Time Estimated: Days\nQueue:
Vulnerabilities\nCF-Vulnerability Risk: Low\nOwner: jgreen\nCF-Priority: Lo
w -Needs to be completed in > 30\n>> CF-Is This Maintenance?: Not_maintenance\nS
ubject: Test Vulnerability for Script Dev\n\n"
[DEBUG] RESOURCE_STATUS: 409 Syntax Error
[INFO] [[('CF-Is This Maintenance?', 'Not_maintenance')]]
[INFO] [[('CF-Is This Maintenance?', 'Not_maintenance')]]

This is the results for uncommenting the "Is This Maintenance?" field in the code. Swap out the references to that for whatever one is uncommented. This error has problems on the same five fields as before (Is This Maintenenance, Approved, Type of change/risk, Discovered by (Company), Discovered by (Program)).

avorum commented 11 years ago

@z4r do you know what might cause an ERROR 409 from RT?

z4r commented 11 years ago

Ok! I found the problem and it's not a problem of encoding. In the RT REST interface CF-Is This Maintenance? must be written CF.{Is This Maintenance?}

I got it reading a tkt:

[INFO] id: ticket/102
[INFO] Queue: Developers
[INFO] Owner: Nobody
[INFO] Creator: john.foo
[INFO] Subject: W00t
[INFO] Status: new
    ...
[INFO] CF.{Priority?}: aaa

Now I need a while to understand how to hide this to end-dev.

Could you try to format your CFs?

avorum commented 11 years ago

Changed "CF-Is This Maintenance?" to "CF.{Is This Maintenance?}" in my script yields the same error.

z4r commented 11 years ago

It could be a problem with the RT version....

from rtkit.resource import RTResource
from rtkit.authenticators import CookieAuthenticator
from rtkit.errors import RTResourceError

from rtkit import set_logging
import logging
set_logging('debug')
logger = logging.getLogger('rtkit')
#resource = RTResource('http://rt.easter-eggs.org/demos/stable/REST/1.0/', 'admin', 'admin', CookieAuthenticator)
resource = RTResource('http://rt.easter-eggs.org/demos/oldstable/REST/1.0/', 'admin', 'admin', CookieAuthenticator)

content = {
    'content': {
        'CF.{Is This Maintenance?}': 'ccc',
        }
    }
try:
    response = resource.post(path='ticket/8', payload=content,)
    #response = resource.get(path='ticket/8')
    for k, v in response.parsed[0]:
        logger.info('{0}: {1}'.format(k, v))
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)

Here the output with RT/4.0

[DEBUG] POST ticket/102
[DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'text/plain'}
[DEBUG] 'content=CF.{Is This Maintenance?}: ccc'
[INFO] POST
[INFO] http://rt.easter-eggs.org/demos/stable/REST/1.0/ticket/102
[DEBUG] HTTP_STATUS: 200 OK
[DEBUG] 'RT/4.0.13-48-g793e087 200 Ok\n\n# Ticket 102 updated.\n\n'
[DEBUG] RESOURCE_STATUS: 200 Ok
[INFO] [[]]

And here the same code on RT/3.8

[DEBUG] POST ticket/8
[DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'text/plain'}
[DEBUG] 'content=CF.{Is This Maintenance?}: ccc'
[INFO] POST
[INFO] http://rt.easter-eggs.org/demos/oldstable/REST/1.0/ticket/8
[DEBUG] HTTP_STATUS: 200 OK
[ERROR] "Can't use an undefined value as an ARRAY reference at /home/rt-demo/3.8/rt/share/html/REST/1.0/dhandler line 189.

Stack:
  [/home/rt-demo/3.8/rt/share/html/REST/1.0/dhandler:189]
  [/home/rt-demo/3.8/rt/share/html/REST/1.0/autohandler:54]
  [/home/rt-demo/3.8/rt/bin/../lib/RT/Interface/Web.pm:547]
  [/home/rt-demo/3.8/rt/bin/../lib/RT/Interface/Web.pm:266]
  [/home/rt-demo/3.8/rt/share/html/autohandler:53]
" is not valid
[DEBUG] "Can't use an undefined value as an ARRAY reference at /home/rt-demo/3.8/rt/share/html/REST/1.0/dhandler line 189.\n\nStack:\n  [/home/rt-demo/3.8/rt/share/html/REST/1.0/dhandler:189]\n  [/home/rt-demo/3.8/rt/share/html/REST/1.0/autohandler:54]\n  [/home/rt-demo/3.8/rt/bin/../lib/RT/Interface/Web.pm:547]\n  [/home/rt-demo/3.8/rt/bin/../lib/RT/Interface/Web.pm:266]\n  [/home/rt-demo/3.8/rt/share/html/autohandler:53]\n"
[DEBUG] RESOURCE_STATUS: Can't use an undefined value as an ARRAY reference at /home/rt-demo/3.8/rt/share/html/REST/1.0/dhandler line 189.

Stack:
  [/home/rt-demo/3.8/rt/share/html/REST/1.0/dhandler:189]
  [/home/rt-demo/3.8/rt/share/html/REST/1.0/autohandler:54]
  [/home/rt-demo/3.8/rt/bin/../lib/RT/Interface/Web.pm:547]
  [/home/rt-demo/3.8/rt/bin/../lib/RT/Interface/Web.pm:266]
  [/home/rt-demo/3.8/rt/share/html/autohandler:53]

[INFO] [[]]

Can you adapt my script (url, user, password and tkt_number) to verify what's happening with RT/3.6?

z4r commented 11 years ago

Anyway only ? seems to break the code...

avorum commented 11 years ago

When I adapt your code it throws an index error on the line "for k, v in response.parsed[0]". And in addition to the ?, / and () seem to break it as well.

z4r commented 11 years ago

download the new code from this repo and change from the script

    for k, v in response.parsed[0]:
        logger.info('{0}: {1}'.format(k, v))

with

logger.info(response.parsed)
avorum commented 11 years ago

Alright it's working now. What exactly are you looking for? logger.info(response.parsed) is what I had before so the output is just the same as before. Here's the output from running it regardless, current code is shown as well. Your output clearly has some stack information but I'm not sure how to get that output out of this code.

from rtkit.resource import RTResource
from rtkit.authenticators import BasicAuthenticator, CookieAuthenticator
from rtkit.errors import RTResourceError

from rtkit import set_logging
import logging
set_logging('debug')
logger = logging.getLogger('rtkit')

resource = RTResource('https://rt.myurl.com/REST/1.0/', 'jgreen', 'pass', CookieAuthenticator)

#create a ticket
content = {
    'content': {
        'CF-Is This Maintenance?' : 'ccc',
        }
    }
try:
    response = resource.post(path='ticket/8501', payload=content,)
    logger.info(response.parsed)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)
[DEBUG] POST ticket/8501
[DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Ac
cept': 'text/plain'}
[DEBUG] 'content=CF-Is This Maintenance?: ccc'
[INFO] POST
[INFO] https://rt.myurl.com/REST/1.0/ticket/8501
[DEBUG] HTTP_STATUS: 200 OK
[DEBUG] 'RT/3.6.6 409 Syntax Error\n\n# Syntax error.\n\n>> CF-Is This Maintenan
ce?: ccc\n\n'
[DEBUG] RESOURCE_STATUS: 409 Syntax Error
[INFO] [[('CF-Is This Maintenance?', 'ccc')]]
[INFO] [[('CF-Is This Maintenance?', 'ccc')]]
z4r commented 11 years ago

Could you try with a CF without ? ?

avorum commented 11 years ago

I did some testing and it works fine with any CF that doesn't include ?, /, or ().

z4r commented 11 years ago

With CF.{...} form too?

avorum commented 11 years ago

No I hadn't. Changing to CF.{...} form triggers the Error 409 in a payload that otherwise runs without issues.

z4r commented 11 years ago

I guess 3.6 it's top old and top buggy to ask to patch.

avorum commented 11 years ago

Ok, it's a shame, I guess we'll have to upgrade.