Created
August 27, 2018 16:19
-
-
Save mazz/419243fe49f4c002f2c8ab8f7f86d9eb to your computer and use it in GitHub Desktop.
jira.py
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
| #!/usr/bin/env python | |
| # encoding=utf8 | |
| from __future__ import print_function | |
| import json | |
| import time | |
| import datetime | |
| import argparse | |
| import sys | |
| reload(sys) | |
| sys.setdefaultencoding('utf8') | |
| import collections | |
| import os | |
| import ConfigParser | |
| import requests | |
| from requests.auth import HTTPBasicAuth | |
| from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
| from requests import RequestException | |
| requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
| import logging | |
| logging.basicConfig(level=logging.ERROR) | |
| logger = logging.getLogger(__name__) | |
| k_app_name = 'mitt' | |
| k_settings_path = os.path.expanduser('~/.{0}'.format(k_app_name)) | |
| k_settings_file = os.path.join(k_settings_path, '{0}.ini'.format(k_app_name)) | |
| k_settings_default_username = 'default_username' | |
| k_settings_default_password = 'default_password' | |
| k_api_url_prefix = 'https://.com/rest/api/2/' | |
| k_search_param_assignee = 'search?jql=assignee=' | |
| # Red = '\033[91m' | |
| # Green = '\033[92m' | |
| # Blue = '\033[94m' | |
| # Cyan = '\033[96m' | |
| # White = '\033[97m' | |
| # Yellow = '\033[93m' | |
| # Magenta = '\033[95m' | |
| # Grey = '\033[90m' | |
| # Black = '\033[90m' | |
| # Default = '\033[99m' | |
| k_default_prefix = '\033[99m' | |
| k_yellow_prefix = '\033[93m' | |
| k_blue_prefix = '\033[94m' | |
| k_cyan_prefix = '\033[96m' | |
| k_green_prefix = '\033[92m' | |
| k_red_prefix = '\033[91m' | |
| k_colourize_postfix = '\033[0m' | |
| def initialize_settings(): | |
| if not os.path.exists(k_settings_path): | |
| os.makedirs(k_settings_path) | |
| if not os.path.exists(k_settings_file): | |
| parser = ConfigParser.SafeConfigParser() | |
| parser.add_section('user') | |
| parser.set('user', 'username', k_settings_default_username) | |
| parser.set('user', 'password', k_settings_default_password) | |
| with open(k_settings_file, 'w') as file: | |
| parser.write(file) | |
| def user_settings(): | |
| parser = ConfigParser.SafeConfigParser() | |
| parser.read(k_settings_file) | |
| user_items = parser.items('user') | |
| return {'username': parser.get('user', 'username'), 'password': parser.get('user', 'password')} | |
| def main(): | |
| initialize_settings() | |
| user = user_settings() | |
| if user['username'] == k_settings_default_username or user['password'] == k_settings_default_password: | |
| print('Please edit your JIRA credentials at {0}'.format(k_settings_file)) | |
| sys.exit(0) | |
| parser = argparse.ArgumentParser( | |
| prog='{0}.py'.format(k_app_name), | |
| formatter_class=argparse.RawDescriptionHelpFormatter | |
| ) | |
| subparser = parser.add_subparsers() | |
| assigned_parser = subparser.add_parser('assigned', description="Used to display issues that are assigned.") | |
| assigned_parser.add_argument('-a', '--assignee', help='Search for issues assigned to this user. Default is username from ~/.mitt/mitt.ini.', type=str) | |
| assigned_parser.add_argument('-c', '--include-closed', help='Include `Closed` issues', action='store_true') | |
| assigned_parser.set_defaults(func=assigned) | |
| args = parser.parse_args() | |
| args.func(args) | |
| def assigned(args): | |
| user = user_settings() | |
| if args.assignee is None: | |
| args.assignee = user['username'] | |
| if args.include_closed is None: | |
| args.include_closed = False | |
| full_url = '{0}{1}{2}'.format(k_api_url_prefix, k_search_param_assignee, args.assignee) | |
| search_response_json = do_search(full_url, user['username'], user['password']) | |
| column_formatters = ['{: <15}', '{: <15}', '{: <65}', '{: <15}'] | |
| title_final = '' | |
| # add the line formatters | |
| for formatter in column_formatters: | |
| title_final += formatter | |
| print(title_final.format('Key', 'Status', 'Summary', 'Priority')) | |
| issues = search_response_json['issues'] | |
| for issue in issues: | |
| key = issue['key'] | |
| status = issue['fields']['status']['name'] | |
| summary = issue['fields']['summary'][0:60] | |
| priority = issue['fields']['priority']['name'] | |
| should_print = status != 'Closed' | |
| if status == 'In Progress': | |
| colour_prefix = k_yellow_prefix | |
| elif status == 'Closed': | |
| colour_prefix = k_default_prefix | |
| elif status == 'Open': | |
| colour_prefix = k_cyan_prefix | |
| elif status == 'Resolved': | |
| colour_prefix = k_green_prefix | |
| else: | |
| colour_prefix = k_default_prefix | |
| # add main colour of line | |
| line_final = '' | |
| line_final += colour_prefix | |
| # add the line formatters | |
| for formatter in column_formatters: | |
| line_final += formatter | |
| # final step, add the colourize postfix | |
| line_final += k_colourize_postfix | |
| line_final_formatted = '' | |
| if isinstance(line_final_formatted, basestring): | |
| line_final_formatted = line_final.format(key, status, summary, priority) | |
| else: | |
| line_final_formatted = unicode(line_final.format(key, status, summary, priority)) | |
| if should_print or args.include_closed: | |
| print(line_final_formatted) | |
| def do_search(full_url, username, password): | |
| try: | |
| r = requests.get(full_url, auth=HTTPBasicAuth(username, password), params=None, verify=False) | |
| logger.info('using: ' + r.url) | |
| json_response = json.loads(r.text) | |
| return json_response | |
| except requests.exceptions.RequestException as e: | |
| # catastrophic error. bail. | |
| logger.error(e) | |
| sys.exit(1) | |
| except ValueError as e: | |
| logger.error(e) | |
| sys.exit(1) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment