I am using the Rest v3 API to bulk create a component that needs to exist in several (a few dozen) projects. I am trying to automate this using Python by looping over a .csv. I am getting a 400 error, but I cannot figure out how to get this to work. Any help would be greatly appreciated.
import requests
from requests.auth import HTTPBasicAuth
import json
import csv
from dotenv import load_dotenv
import os
# Jira Cloud URL - update this value to your cloud url
# API endpoint for adding users to a group
api_endpoint = f"{jira_url}/rest/api/3/component"
# Authentication credentials - update the email and api_token for your user and token combination
# these should be added to a .env file and also excuded from .gitignore
email = os.getenv("email")
api_token = os.getenv("api_token")
# Read the CSV file with user and group IDs
csv_file_path = "Admin Automation/bulk_component_add.csv"
auth = HTTPBasicAuth(email, api_token)
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
}
# Process the CSV file
with open(csv_file_path, mode="r") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
project = row["project"] # Change to match your CSV column names
name = row["name"] # Change to match your CSV column names
assigneeType = row["assigneeType"] # Change to match your CSV column names
projectId = row["projectId"] # Change to match your CSV column names
# Construct the JSON payload
payload = json.dumps({"project": project})
# Set the query parameters for the project
query = {"projectId": projectId}
# Make the API request to add the component to the project
response = requests.post(
api_endpoint,
data=payload,
headers=headers,
params=query,
auth=auth,
)
if response.status_code == 201:
print(f"Added component to '{project}' with name '{name}'.")
else:
print(
f"Failed to add component to '{project}'. Status Code: {response.status_code}"
)
All I get back is a 400 and I have no idea what's failing, or if my script is just fundamentally broken.