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.96k stars 873 forks source link

maxResult should allow values >= 0 #1823

Open jstafford5380 opened 9 months ago

jstafford5380 commented 9 months ago

Bug summary

The API allows for specifying fewer than 50 results, so should the SDK. For example, I want to even be able to set 0 if I just want to get the count for the query result, which currently, I cannot.

Is there an existing issue for this?

Jira Instance type

Jira Cloud (Hosted by Atlassian)

Jira instance version

cloud

jira-python version

3.6.0

Python Interpreter version

3.10.6

Which operating systems have you used?

Reproduction steps

# 1. Use `client.search(maxResults=0)`
# 2. Observe the request URL in logs, the maxResults param has been changed to 100
# 3. Execute the same request in Postman and observe the server responds with an empty array but with counts.

Example code

 unplanned = client.search_issues(
        jql_str=f'project = {project} '
                f'AND type != subtask '
                f'AND priority = Highest '
                f'AND created >= {start} AND created >= {end}',
        maxResults=0
    )

I believe the problem is here:

https://github.com/pycontribs/jira/blob/09750538a6f10cfe38fd6d1b284bf26f11728b86/jira/client.py#L732-L735

There is a statement if maxResults: which evaluates to false when zero and the fo.lowing elif statement sets it to a batch size of 100. I find it strange to use an int as a boolean in this way, so I wonder if this was intended.

Stack trace

In my case it was revealed during an unrelated error (notice maxResults at the end of the URL)

jira.exceptions.JIRAError: JiraError HTTP 400 url: https://redacted.net/rest/api/2/search?jql=project+%3D+PENG+AND+type+%21%3D+subtask+AND+priority+%3D+Highest+AND+created+%3E%3D+2024-01-01+AND+created+%3E%3D+2024-02-16&startAt=0&validateQuery=True&fields=%2Aall&maxResults=100
    text: The value 'PENG' does not exist for the field 'project'., Field 'type' does not exist or this field cannot be viewed by anonymous users., Field 'priority' does not exist or this field cannot be viewed by anonymous users.

Expected behaviour

When I set maxResults=0 then that is what should be sent to the server.

Additional Context

No response

rynkk commented 8 months ago

I agree that the way this is handled probably is not the most elegant, but maybe this workaround can alleviate the pain, while people check if this misbehaviour is worth adjusting:

Set maxResult to "0" instead of 0, e.g.:

 unplanned = client.search_issues(
        jql_str=f'project = {project} '
                f'AND type != subtask '
                f'AND priority = Highest '
                f'AND created >= {start} AND created >= {end}',
        maxResults="0"
    )

Jannik Meinecke jannik.meinecke@mercedes-benz.com on behalf of MBition GmbH.

Provider Information