Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import csv
- # JIRA configuration
- JIRA_BASE_URL = 'https://your-domain.atlassian.net' # Replace with your JIRA instance URL
- JIRA_API_URL = f'{JIRA_BASE_URL}/rest/api/2/search'
- JQL_QUERY = 'project = "YOUR_PROJECT_KEY" AND status = "Open"' # Replace with your JQL query
- API_TOKEN = 'your_api_token' # Replace with your JIRA API token
- OUTPUT_CSV = 'jira_issues.csv'
- def get_issues(jql, start_at=0, max_results=50):
- """
- Fetches issues from JIRA based on a JQL query.
- """
- headers = {
- 'Authorization': f'Basic {USERNAME}:{API_TOKEN}',
- 'Content-Type': 'application/json'
- }
- params = {
- 'jql': jql,
- 'startAt': start_at,
- 'maxResults': max_results
- }
- response = requests.get(JIRA_API_URL, headers=headers, params=params)
- response.raise_for_status()
- return response.json()
- def fetch_all_issues(jql_query):
- """
- Fetches all issues by iterating through paginated results.
- """
- all_issues = []
- start_at = 0
- max_results = 50
- while True:
- data = get_issues(jql_query, start_at, max_results)
- issues = data.get('issues', [])
- if not issues:
- break
- all_issues.extend(issues)
- start_at += len(issues)
- # If fewer issues are returned than requested, we're at the last page
- if len(issues) < max_results:
- break
- return all_issues
- def issues_to_csv(issues, filename):
- """
- Writes JIRA issues to a CSV file.
- """
- with open(filename, mode='w', newline='', encoding='utf-8') as csvfile:
- fieldnames = ['Key', 'Summary', 'Status', 'Assignee', 'Reporter', 'Created', 'Updated']
- writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
- writer.writeheader()
- for issue in issues:
- fields = issue.get('fields', {})
- writer.writerow({
- 'Key': issue.get('key'),
- 'Summary': fields.get('summary'),
- 'Status': fields.get('status', {}).get('name'),
- 'Assignee': fields.get('assignee', {}).get('displayName') if fields.get('assignee') else 'Unassigned',
- 'Reporter': fields.get('reporter', {}).get('displayName'),
- 'Created': fields.get('created'),
- 'Updated': fields.get('updated')
- })
- def main():
- print("Fetching issues...")
- issues = fetch_all_issues(JQL_QUERY)
- print(f"Total issues fetched: {len(issues)}")
- print("Writing issues to CSV...")
- issues_to_csv(issues, OUTPUT_CSV)
- print(f"Issues written to {OUTPUT_CSV}")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment