Guest User

Untitled

a guest
Oct 29th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import requests
  2. import csv
  3.  
  4. # JIRA configuration
  5. JIRA_BASE_URL = 'https://your-domain.atlassian.net'  # Replace with your JIRA instance URL
  6. JIRA_API_URL = f'{JIRA_BASE_URL}/rest/api/2/search'
  7. JQL_QUERY = 'project = "YOUR_PROJECT_KEY" AND status = "Open"'  # Replace with your JQL query
  8. USERNAME = '[email protected]'  # Replace with your JIRA account email
  9. API_TOKEN = 'your_api_token'  # Replace with your JIRA API token
  10. OUTPUT_CSV = 'jira_issues.csv'
  11.  
  12. def get_issues(jql, start_at=0, max_results=50):
  13.     """
  14.    Fetches issues from JIRA based on a JQL query.
  15.    """
  16.     headers = {
  17.         'Authorization': f'Basic {USERNAME}:{API_TOKEN}',
  18.         'Content-Type': 'application/json'
  19.     }
  20.     params = {
  21.         'jql': jql,
  22.         'startAt': start_at,
  23.         'maxResults': max_results
  24.     }
  25.     response = requests.get(JIRA_API_URL, headers=headers, params=params)
  26.     response.raise_for_status()
  27.     return response.json()
  28.  
  29. def fetch_all_issues(jql_query):
  30.     """
  31.    Fetches all issues by iterating through paginated results.
  32.    """
  33.     all_issues = []
  34.     start_at = 0
  35.     max_results = 50
  36.  
  37.     while True:
  38.         data = get_issues(jql_query, start_at, max_results)
  39.         issues = data.get('issues', [])
  40.         if not issues:
  41.             break
  42.  
  43.         all_issues.extend(issues)
  44.         start_at += len(issues)
  45.  
  46.         # If fewer issues are returned than requested, we're at the last page
  47.         if len(issues) < max_results:
  48.             break
  49.  
  50.     return all_issues
  51.  
  52. def issues_to_csv(issues, filename):
  53.     """
  54.    Writes JIRA issues to a CSV file.
  55.    """
  56.     with open(filename, mode='w', newline='', encoding='utf-8') as csvfile:
  57.         fieldnames = ['Key', 'Summary', 'Status', 'Assignee', 'Reporter', 'Created', 'Updated']
  58.         writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  59.         writer.writeheader()
  60.  
  61.         for issue in issues:
  62.             fields = issue.get('fields', {})
  63.             writer.writerow({
  64.                 'Key': issue.get('key'),
  65.                 'Summary': fields.get('summary'),
  66.                 'Status': fields.get('status', {}).get('name'),
  67.                 'Assignee': fields.get('assignee', {}).get('displayName') if fields.get('assignee') else 'Unassigned',
  68.                 'Reporter': fields.get('reporter', {}).get('displayName'),
  69.                 'Created': fields.get('created'),
  70.                 'Updated': fields.get('updated')
  71.             })
  72.  
  73. def main():
  74.     print("Fetching issues...")
  75.     issues = fetch_all_issues(JQL_QUERY)
  76.     print(f"Total issues fetched: {len(issues)}")
  77.     print("Writing issues to CSV...")
  78.     issues_to_csv(issues, OUTPUT_CSV)
  79.     print(f"Issues written to {OUTPUT_CSV}")
  80.  
  81. if __name__ == '__main__':
  82.     main()
  83.  
Advertisement
Add Comment
Please, Sign In to add comment