pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.94k stars 860 forks source link

edit_issue returns 400 Bad Request #1649

Closed oxeye-daniel closed 1 year ago

oxeye-daniel commented 1 year ago

Bug summary

When triggering the edit_issue function, I get back 400 Bad Request. The HTTP request that is being sent looks like this:

PUT /rest/api/2/issue/<ID> HTTP/2
Host: oxeye.atlassian.net
User-Agent: python-requests/2.28.1
Accept-Encoding: gzip, deflate
Accept: application/json
Content-Type: application/json
Content-Length: 55
Authorization: Basic <REDACTED>

{"update": {"summary": "new summary"}}

After some research, it turns out the endpoint accepts the data without the update key. it should look like this:

PUT /rest/api/2/issue/<ID> HTTP/2
Host: oxeye.atlassian.net
User-Agent: python-requests/2.28.1
Accept-Encoding: gzip, deflate
Accept: application/json
Content-Type: application/json
Content-Length: 52
Authorization: Basic <REDACTED>

{"fields": {"summary": "new summary"}}

Is there an existing issue for this?

Jira Instance type

Jira Cloud (Hosted by Atlassian)

Jira instance version

No response

jira-python version

main

Python Interpreter version

3.10

Which operating systems have you used?

Reproduction steps

# 1. Given a Jira client instance
jira: JIRA
# 2. When I call the function with argument x
jira.edit_issue(x)
# 3. I get back 400 Bad Request
...

Stack trace

<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/urllib3/connectionpool.py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
<Response [400]>
Traceback (most recent call last):
  File "<REDACTED>/get-tickets.py", line 167, in <module>
    main()
  File "<REDACTED>/get-tickets.py", line 139, in edit_done_epics
    jira.edit_issue('1111', fields={"summary": "new summary"})
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/jira.py", line 1233, in edit_issue
    return self.put(url, data=data, params=params)
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/rest_client.py", line 370, in put
    response = self.request(
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/rest_client.py", line 257, in request
    self.raise_for_status(response)
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/rest_client.py", line 443, in raise_for_status
    raise HTTPError(error_msg, response=response)
requests.exceptions.HTTPError: Invalid request payload. Refer to the REST API documentation and try again.

Expected behaviour

The issue should have been updated

Additional Context

No response

alanmxll commented 1 year ago

Bug summary

When triggering the edit_issue function, I get back 400 Bad Request. The HTTP request that is being sent looks like this:

PUT /rest/api/2/issue/<ID> HTTP/2
Host: oxeye.atlassian.net
User-Agent: python-requests/2.28.1
Accept-Encoding: gzip, deflate
Accept: application/json
Content-Type: application/json
Content-Length: 55
Authorization: Basic <REDACTED>

{"update": {"summary": "new summary"}}

After some research, it turns out the endpoint accepts the data without the update key. it should look like this:

PUT /rest/api/2/issue/<ID> HTTP/2
Host: oxeye.atlassian.net
User-Agent: python-requests/2.28.1
Accept-Encoding: gzip, deflate
Accept: application/json
Content-Type: application/json
Content-Length: 52
Authorization: Basic <REDACTED>

{"fields": {"summary": "new summary"}}

Is there an existing issue for this?

  • [x] I have searched the existing issues

Jira Instance type

Jira Cloud (Hosted by Atlassian)

Jira instance version

No response

jira-python version

main

Python Interpreter version

3.10

Which operating systems have you used?

  • [ ] Linux
  • [x] macOS
  • [ ] Windows

Reproduction steps

# 1. Given a Jira client instance
jira: JIRA
# 2. When I call the function with argument x
jira.edit_issue(x)
# 3. I get back 400 Bad Request
...

Stack trace

<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/urllib3/connectionpool.py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
<Response [400]>
Traceback (most recent call last):
  File "<REDACTED>/get-tickets.py", line 167, in <module>
    main()
  File "<REDACTED>/get-tickets.py", line 139, in edit_done_epics
    jira.edit_issue('1111', fields={"summary": "new summary"})
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/jira.py", line 1233, in edit_issue
    return self.put(url, data=data, params=params)
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/rest_client.py", line 370, in put
    response = self.request(
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/rest_client.py", line 257, in request
    self.raise_for_status(response)
  File "<REDACTED>/.pyenv/versions/3.10.6/lib/python3.10/site-packages/atlassian/rest_client.py", line 443, in raise_for_status
    raise HTTPError(error_msg, response=response)
requests.exceptions.HTTPError: Invalid request payload. Refer to the REST API documentation and try again.

Expected behaviour

The issue should have been updated

Additional Context

No response

Hi,

I took the liberty of doing some tests with the functionality you are trying to use, according to this documentation.

The code below worked perfectly:

from jira import JIRA

jira = JIRA()

issue = jira.issue("ISSUE_KEY")
issue.update(fields={"summary": "YOUR_SUMMARY"})

I didn't find any method called "edit_issue", just this "update" that appears in the issue returned by the "issue" method called in the JIRA instance itself.

I hope this way works for you too.

oxeye-daniel commented 1 year ago

Thanks for taking your time and finding a solution :) Although this solves the problem, I don't understand why the code exposes such API where the documentation tries to stir towards a more object-oriented approach. Is that the way the JIRA API used to work before?

alanmxll commented 1 year ago

Thanks for taking your time and finding a solution :) Although this solves the problem, I don't understand why the code exposes such API where the documentation tries to stir towards a more object-oriented approach. Is that the way the JIRA API used to work before?

From what I know of the official JIRA API, this approach only exists in this python package (maybe another language package too), given that POST and PUT requests from the official API normally expect a "schema" (which can be an object literal containing specific data from that request).

For example, this request for your problem would be something like:

import json
import requests

issue_key = "ISSUE_KEY"
url = f"http://yourdomain/rest/api/2/issue/{issue_key}"

schema = json.dumps({
    "updates": {
        "fields": {
            "summary": "YOUR SUMMARY HERE",
        }
    }
})

r = requests.put(url, data=schema)
...

Here are some links if you want to consult the official documentation:

studioj commented 1 year ago

@oxeye-daniel Thank you for reporting this issue, although I must say that it seems the issue you are reporting should be reported to the appropriate GitHub project namely: https://github.com/atlassian-api/atlassian-python-api You are always allowed to try our jira package by installing it via pip install jira docs can be found here. https://jira.readthedocs.io/ Good luck