Last active
March 28, 2025 13:36
-
-
Save vikrantyadav11/fb0e11ac85f899f1e0693eb18ef80ea5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
import csv | |
# Confluence credentials and domain | |
JIRA_DOMAIN = "https://your_domain.atlassian.net" | |
EMAIL = os.getenv("JIRA_EMAIL", "EMAIL_ADDRESS") | |
API_TOKEN = os.getenv("JIRA_API_TOKEN", "API_TOKEN") | |
# Headers for authentication | |
HEADERS = { | |
"Accept": "application/json", | |
"Content-Type": "application/json", | |
"User-Agent": "Python Jira Client", | |
} | |
auth = (EMAIL, API_TOKEN) | |
session = requests.Session() | |
session.headers.update(HEADERS) | |
def fetch_all_pages(url): | |
"""Fetches paginated data with limit=250.""" | |
results = [] | |
while url: | |
try: | |
response = session.get(url, auth=auth, timeout=20, verify=True) | |
response.raise_for_status() | |
data = response.json() | |
results.extend(data.get("results", [])) | |
next_link = data.get("_links", {}).get("next") | |
url = f"{JIRA_DOMAIN}{next_link}" if next_link else None | |
except requests.RequestException as e: | |
print(f"Error fetching data from {url}: {e}") | |
break | |
return results | |
def get_spaces(): | |
"""Fetches all Confluence spaces.""" | |
url = f"{JIRA_DOMAIN}/wiki/api/v2/spaces?limit=250" | |
return fetch_all_pages(url) | |
def get_space_permissions(space_id): | |
"""Fetches all permissions for a space.""" | |
url = f"{JIRA_DOMAIN}/wiki/api/v2/spaces/{space_id}/permissions?limit=250" | |
return fetch_all_pages(url) | |
def get_user_details(account_id): | |
"""Fetch user details (Name & Email) from Jira.""" | |
url = f"{JIRA_DOMAIN}/rest/api/3/user" | |
params = {"accountId": account_id} | |
try: | |
response = session.get(url, headers=HEADERS, params=params, auth=auth, timeout=20) | |
response.raise_for_status() | |
data = response.json() | |
return data.get("displayName", "Unknown"), data.get("emailAddress", "Unknown") | |
except requests.RequestException: | |
return "Unknown", "Unknown" | |
def get_group_details(group_id): | |
"""Fetch group name from Jira.""" | |
url = f"{JIRA_DOMAIN}/rest/api/3/group" | |
params = {"groupId": group_id} | |
try: | |
response = session.get(url, headers=HEADERS, params=params, auth=auth, timeout=20) | |
response.raise_for_status() | |
data = response.json() | |
return data.get("name", "Unknown") | |
except requests.RequestException: | |
return "Unknown" | |
def main(): | |
spaces = get_spaces() | |
if not spaces: | |
print("No spaces found.") | |
return | |
csv_file = "Sprinklr_Filtered_Confluence_Space_Permissions.csv" | |
with open(csv_file, mode='w', newline='', encoding='utf-8') as file: | |
writer = csv.writer(file) | |
writer.writerow(["Space Name", "Space Type", "Space Key", "Status", "Principal Type", "Principal ID", "User Name (or Group Name)", "User Email", "Operation Key", "Target Type"]) | |
for space in spaces: | |
space_name = space.get("name", "Unknown") | |
space_type = space.get("type", "Unknown") | |
space_key = space.get("key", "Unknown") | |
space_status = space.get("status", "Unknown") | |
if space_type.lower() == "personal": | |
continue # Skip personal spaces | |
space_id = space.get("id", "Unknown") | |
permissions = get_space_permissions(space_id) | |
for perm in permissions: | |
principal = perm.get("principal", {}) | |
principal_type = principal.get("type", "Unknown") | |
principal_id = principal.get("id", "Unknown") | |
# Default values | |
user_name_or_group_name = "Unknown" | |
user_email = "Unknown" | |
if principal_type == "user": | |
user_name_or_group_name, user_email = get_user_details(principal_id) | |
elif principal_type == "group": | |
user_name_or_group_name = get_group_details(principal_id) | |
operation = perm.get("operation", {}) | |
operation_key = operation.get("key", "Unknown") | |
target_type = operation.get("targetType", "Unknown") | |
writer.writerow([space_name, space_type, space_key, space_status, principal_type, principal_id, user_name_or_group_name, user_email, operation_key, target_type]) | |
print(f"CSV file '{csv_file}' has been created successfully.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment