Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import time
- import shutil
- from jira import JIRA, JIRAError
- from datetime import datetime
- import pytz
- # Set up JIRA connection options for the server
- jira_options = {'server': 'INSERT_SERVER_HERE'}
- # Authenticate using your JIRA token (replace 'INSERT_TOKEN_HERE' with your actual token)
- jira = JIRA(options=jira_options, token_auth='INSERT_TOKEN_HERE')
- # Define the base directory where folders will be created
- base_dir = r"INSERT_PATH_HERE"
- # Paths to the template PSD files that will be copied to the 'Source Files' subfolder
- template_dir = r'INSERT_PATH_HERE'
- template_1 = os.path.join(template_dir, "1.psd")
- template_2 = os.path.join(template_dir, "2.psd")
- template_3 = os.path.join(template_dir, "3.psd")
- # Define your JIRA username
- username = 'INSERT_USERNAME_HERE'
- # Function to sanitize folder names by replacing invalid characters with an underscore
- def sanitize_folder_name(name):
- # Replace any characters that are not allowed in Windows folder names
- return re.sub(r'[\\/*?:"<>|]', "_", name)
- # Main function to create folders for JIRA tickets
- def create_ticket_folders():
- print("Attempting to fetch open tickets...")
- try:
- # Fetch all open issues assigned to the user from JIRA
- issues = jira.search_issues(f'assignee={username} AND status = "Open"')
- print(f"Found {len(issues)} open tickets.")
- except JIRAError as e:
- # Handle error if the connection to JIRA fails
- print(f"Failed to fetch issues from JIRA: {e}")
- return
- for issue in issues:
- ticket_number = issue.key # Get the ticket number (e.g., ABC-1234)
- prefix = ticket_number.split('-')[0] # Get the prefix (e.g., ABC)
- assigner = issue.fields.reporter.name # Get the assigner's name
- project_title = issue.fields.summary # Get the project title (summary field)
- # Sanitize the project title to ensure it's a valid folder name
- sanitized_project_title = sanitize_folder_name(project_title)
- # Get the ticket's creation date and format it into year and month
- created_date = datetime.strptime(issue.fields.created[:10], '%Y-%m-%d')
- year_folder = created_date.strftime('%Y')
- month_folder = created_date.strftime('%B')
- # Create the year folder if it doesn't exist
- year_folder_path = os.path.join(base_dir, year_folder)
- if not os.path.exists(year_folder_path):
- os.makedirs(year_folder_path, exist_ok=True)
- # Create the month folder within the year folder if it doesn't exist
- month_folder_path = os.path.join(year_folder_path, month_folder)
- if not os.path.exists(month_folder_path):
- os.makedirs(month_folder_path, exist_ok=True)
- # Construct the full folder name including the ticket number and sanitized project title
- folder_name = f"{ticket_number} - {sanitized_project_title}"
- ticket_folder = os.path.join(month_folder_path, folder_name)
- if not os.path.exists(ticket_folder):
- # Create the ticket folder if it doesn't exist
- os.makedirs(ticket_folder, exist_ok=True)
- # Determine subfolders and template PSD file based on the assigner and ticket prefix
- if assigner == 'username' or prefix == 'ABC':
- subfolders = ['Final Assets', 'Source Files', 'Raw Renders']
- psd_template = template_1
- elif prefix == 'DEF':
- subfolders = ['Sony Final Assets', 'Microsoft Final Assets', 'Source Files']
- psd_template = template_2
- else:
- subfolders = ['Final Assets', 'Source Files']
- psd_template = template_3
- # Create each subfolder and copy the appropriate PSD template into the 'Source Files' folder
- for subfolder in subfolders:
- path = os.path.join(ticket_folder, subfolder)
- os.makedirs(path, exist_ok=True)
- # Copy the PSD template to the 'Source Files' subfolder and rename it to the ticket number
- if subfolder == 'Source Files':
- psd_dest_path = os.path.join(path, f"{ticket_number}.psd")
- # Check if the file already exists to avoid overwriting
- if not os.path.exists(psd_dest_path):
- shutil.copy(psd_template, psd_dest_path)
- print(f"Copied {psd_template} to {psd_dest_path}")
- else:
- print(f"File {psd_dest_path} already exists. Skipping copy to avoid overwriting.")
- # Create a .md file with the project details from the JIRA ticket
- md_file_path = os.path.join(ticket_folder, f"{ticket_number}.md")
- # Check if the .md file already exists to avoid overwriting
- if not os.path.exists(md_file_path):
- project_details = issue.fields.description or "No project details provided."
- with open(md_file_path, 'w', encoding='utf-8') as md_file:
- md_file.write(project_details)
- print(f"Created {md_file_path} with project details.")
- else:
- print(f"Markdown file {md_file_path} already exists. Skipping creation to avoid overwriting.")
- print(f"Created folders for {ticket_number} under {year_folder}/{month_folder} with subfolders: {subfolders}")
- # Run the function once when the script is executed
- if __name__ == "__main__":
- create_ticket_folders()
- # The following code is commented out to allow the script to run only once per invocation.
- # You can uncomment it if you want to revert back to continuous running mode.
- # def run_continuously():
- # try:
- # print("Running... Press Ctrl+C to stop. Script will run again in 60 minutes.")
- # while True:
- # create_ticket_folders()
- # time.sleep(3600)
- # except KeyboardInterrupt:
- # print("Script stopped by user.")
- # run_continuously()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement