Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jira.client.JIRA.add_attachment - 500 Internal Server Error #1437

Closed
3 of 4 tasks
mtripoli opened this issue Jul 27, 2022 · 4 comments · Fixed by #1438
Closed
3 of 4 tasks

jira.client.JIRA.add_attachment - 500 Internal Server Error #1437

mtripoli opened this issue Jul 27, 2022 · 4 comments · Fixed by #1438
Labels
bug good first issue New contributors welcome !

Comments

@mtripoli
Copy link
Contributor

Bug summary

This problem occurs when the add_attachment method is called with a str value representing the file name assigned to the attachment parameter and no value assigned to the filename parameter.

Here is the salient message from the server log:

 Cannot create attachment without a filename - inline content? See http://jira.atlassian.com/browse/JRA-10825 (file=attachment-8292840129205496726.tmp).

(This also induces an NPE in the server)

diff that highlights the problem area:

diff --git a/jira/client.py b/jira/client.py
index de0a110..237cfca 100644
--- a/jira/client.py
+++ b/jira/client.py
@@ -1005,8 +1005,8 @@ class JIRA:
                 )

         fname = filename
-        if not fname and isinstance(attachment, BufferedReader):
-            fname = os.path.basename(attachment.name)
+        if not fname and isinstance(attachment_io, BufferedReader):
+            fname = os.path.basename(attachment_io.name)

         def generate_multipartencoded_request_args() -> Tuple[
             MultipartEncoder, CaseInsensitiveDict

The problem was introduced in 6e51395 but only recently merged and released.

Is there an existing issue for this?

  • I have searched the existing issues

Jira Instance type

Jira Server or Data Center (Self-hosted)

Jira instance version

v8.13.22#813022

jira-python version

3.3.0

Python Interpreter version

3.8

Which operating systems have you used?

  • Linux
  • macOS
  • Windows

Reproduction steps

# 1. Given a Jira client instance
client: jira.client.JIRA
issue: jira.resources.Issue
# 2. These calls throw JiraError the exceptions in the Stack trace section
client.add_attachment(issue, '/path/to/file.txt')
or this
client.add_attachment(issue, attachment='/path/to/file.txt')
# 3.
...

Stack trace

Traceback (most recent call last):
  File "/Users/mtripoli/.newt-cache/pyenv/versions/cpython-3.8.13/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/Users/mtripoli/src/NCO/ocjira/.venv/lib/python3.8/site-packages/jira/client.py", line 121, in wrapper
    result = func(*arg_list, **kwargs)
  File "/Users/mtripoli/src/NCO/ocjira/.venv/lib/python3.8/site-packages/jira/client.py", line 1039, in add_attachment
    r = self._session.post(
  File "/Users/mtripoli/src/NCO/ocjira/.venv/lib/python3.8/site-packages/requests/sessions.py", line 635, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
  File "/Users/mtripoli/src/NCO/ocjira/.venv/lib/python3.8/site-packages/jira/resilientsession.py", line 223, in request
    elif raise_on_error(response, **processed_kwargs):
  File "/Users/mtripoli/src/NCO/ocjira/.venv/lib/python3.8/site-packages/jira/resilientsession.py", line 69, in raise_on_error
    raise JIRAError(
jira.exceptions.JIRAError: JiraError HTTP 500 url: https://jira-corp-stg-api.dmz.build.netflix.net/rest/api/2/issue/NCO-15586/attachments
	
	response headers = {'Cache-Control': 'no-cache, no-store, no-transform', 'Content-Security-Policy': "frame-ancestors 'self'", 'Content-Type': 'application/json;charset=UTF-8', 'Date': 'Wed, 27 Jul 2022 23:34:45 GMT', 'Referrer-Policy': 'strict-origin-when-cross-origin', 'Server': 'Apache', 'Strict-Transport-Security': 'max-age=31536000', 'X-ANODEID': 'i-07245fc0b855c1640', 'X-AREQUESTID': '994x2743173x1', 'X-ASESSIONID': '1rbqsas', 'X-AUSERNAME': 'svc-ocpd', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-Seraph-LoginReason': 'OK', 'X-XSS-Protection': '1; mode=block; report=https://ichnaea-web.netflix.com/log/freeform/xssreport', 'Content-Length': '0', 'Connection': 'keep-alive'}
	response text =

Expected behaviour

When the add_attachment method is passed a str value in the attachment parameter and no value assigned to the optional filename parameter, the add_attachment method should derive the file name from the BufferedReader object rather than pass no file name to the server.

Additional Context

No response

@adehad adehad added bug good first issue New contributors welcome ! labels Jul 28, 2022
@adehad
Copy link
Contributor

adehad commented Jul 28, 2022

Will see if I can get this into a hotfix, thanks for the excellent report

@joegiglio
Copy link

I am trying to add an attachment and getting an exception. Is this related to this bug?

CODE:

jira.add_attachment(issue=issue, attachment="/Users/joegiglio/dev/mine/jira/storm.png")

STACKTRACE:

Traceback (most recent call last):
  File "/Users/joe/dev/mine/jira/run.py", line 41, in <module>
    jira.add_attachment(issue=issue, attachment="beef.jpg")
  File "/Users/joe/dev/mine/jira/venv/lib/python3.9/site-packages/jira/client.py", line 121, in wrapper
    result = func(*arg_list, **kwargs)
  File "/Users/joeg/dev/mine/jira/venv/lib/python3.9/site-packages/jira/client.py", line 1051, in add_attachment
    raise JIRAError(f"Unable to parse JSON: {js}. Failed to add attachment?")
jira.exceptions.JIRAError: JiraError HTTP None
        text: Unable to parse JSON: []. Failed to add attachment?

@mtripoli
Copy link
Contributor Author

Yes, it is. Add the filename parameter to work around the defect, like so:

jira.add_attachment(issue=issue, attachment="/Users/joegiglio/dev/mine/jira/storm.png", filename="storm.png")

@joegiglio
Copy link

I also found this other suggestion from the docs works OK:


with open('/Users/joe/dev/mine/jira/storm.png', 'rb') as f:
    jira.add_attachment(issue=issue, attachment=f)

@adehad adehad linked a pull request Aug 1, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue New contributors welcome !
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants