Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Hi all, I want python script to get all the active users list from Jira thanks

prasad Biddika February 28, 2023

Hi all, I want python script to get all the active users list from Jira.

We are using data  center.

thanks

1 answer

0 votes
Suneel Kumar February 17, 2025

Hi Prasad,

You can use below script to fetch jira active users.

 

import requests
import csv

# Jira Data Center credentials and base URL
JIRA_BASE_URL = "https://jira-url" # Replace with your Jira Data Center URL
USERNAME = "USER.Name" # Replace with your Jira username
API_TOKEN = "API TOKEN" # Replace with your Personal Access Token (PAT)

# Endpoint to fetch active users
USERS_ENDPOINT = f"{JIRA_BASE_URL}/rest/api/2/user/search"

# Headers with API token for authentication
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {API_TOKEN}" # Use the API token for authentication
}

def fetch_all_active_users():
start_at = 0
max_results = 1000 # Max allowed by Jira API
all_users = []
total_users = None # To store the total count (if available)

while True:
params = {
"username": ".", # Fetch all users (wildcard search)
"startAt": start_at,
"maxResults": max_results,
"includeActive": True,
"includeInactive": False,
}

try:
response = requests.get(USERS_ENDPOINT, headers=headers, params=params)
response.raise_for_status()
data = response.json()

users = data # Jira returns an array of users directly

if not users:
break # No more users

all_users.extend(users)
start_at += len(users)

except requests.exceptions.RequestException as e:
print(f"Error fetching users: {e}")
return None

return all_users

# Save the results to a CSV file
def save_users_to_csv(users, filename='active_jira_users.csv'):
if users is None: # Check if fetch_all_active_users returned an error
return

with open(filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)

# Write the header row
writer.writerow(['Name', 'Email', 'Username'])

# Write user details to the CSV file
for user in users:
name = user.get('displayName', 'N/A')
email = user.get('emailAddress', 'N/A')
username = user.get('name', 'N/A')
writer.writerow([name, email, username])

print(f"Active Jira users have been saved to '{filename}'.")

# Main script
if __name__ == "__main__": #Good practice to enclose main code
all_users = fetch_all_active_users()
save_users_to_csv(all_users)

Suggest an answer

Log in or Sign up to answer