Search code examples
pythonpython-jira

f-string in Python with the atlassian-python package. Gives bad request


I'm trying to fetch all issues in JIRA for all projects. When doing the call one at a time, it works perfect. When trying to run it in a for loop I'm prompted with a 400 Client error.

The way that works:

results = jira_instance.jql("project = FJA", limit = 100, fields=["issuetype", "status", "summary"])

The way that does not work:

projects = ["ADV", "WS", "FJA", "FOIJ", "QW", "UOI"]

for key in projects:
  results = jira_instance.jql(f"project = {key})", limit = 100, fields=["issuetype", "status", "summary"])

The error:

Traceback (most recent call last):
  File "C:\jira-api-python\jira.py", line 24, in <module>
    results = jira_instance.jql("project = {key}", limit = 100, fields=["issuetype", "status", "summary"])
  File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\jira.py", line 2271, in jql
    return self.get("rest/api/2/search", params=params)
  File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\rest_client.py", line 264, in get
    response = self.request(
  File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\rest_client.py", line 236, in request
    response.raise_for_status()
  File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\requests\models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://stuff.atlassian.net/rest/api/2/search?startAt=0&maxResults=100&fields=issuetype%2Cstatus%2Csummary&jql=project+%3D+%7Bkey%7D

My guess is that I'm not using the f-string correct. But when I print the value of {key} it is correct.

Any pointers would be greatly appreciated.

Thank you for your time.

Edit:

Added the full traceback, only removed the path to my machine and changed the URL to the endpoint. Below is the full file with credentials and endpoint redacted. The ideas is to create a csv for each project.

The full code:

from atlassian import Jira
import pandas as pd
import time

jira_instance = Jira(
    url = "https://stuff.atlassian.net/",
    username = "user",
    password = "pass",
)



projects = ["ADV", "WS", "FJA", "FOIJ", "QW", "UOI"]
FIELDS_OF_INTEREST = ["key", "fields.summary", "fields.status.name"]
timestamp = time.strftime("%Y%m%d-%H%M%S")
file_ending = ".csv"

for key in projects:


    print(f"stuff = {key}")
    results = jira_instance.jql(f"project = {key})", limit = 1000, fields=["issuetype", "status", "summary"])

Solution

  • I found the very simple solution.

    In this snippet: results = jira_instance.jql(f"project = {key})", limit = 1000, fields=["issuetype", "status", "summary"])

    The ) after {key} was not supposed to be there.

    Thank you for the help