Created
September 19, 2018 17:19
-
-
Save tunelko/fe4989bfdcc27c15b3db77d9488193c5 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
#!/usr/bin/env python | |
import argparse | |
# from skeleton.users import models | |
# from sqlalchemy.exc import SQLAlchemyError | |
# from collections import Counter | |
import json | |
import sys | |
import requests | |
""" | |
usage: daemon.py [-h] -a ARGS [-v] operation | |
synopsis: | |
a Python JIRA Cloud REST API. | |
positional arguments: | |
operation An operation: | |
get_issue,get_transitions,do_transitions, ... | |
optional arguments: | |
-h, --help show this help message and exit | |
-a ARGS, --args ARGS Arguments: issue or MAX for MaxResults | |
-v, --verbose Print messages during requests. | |
additional arguments: | |
get_issue --args="<issue>" | |
get_transitions --args="<issue>" | |
do_transitions --args=<issue>" | |
examples: | |
python daemon.py get_issue --args="ISSUE-XXX" | |
""" | |
BASE_URL = 'https://yourdomain.atlassian.net/' | |
BASE_USERNAME = 'email_redacted@domain_redacted' | |
BASE_TOKEN = 'token_redacted' | |
MAX_RESULTS = 100 | |
def print_debug(options, msg): | |
"""Print a message if verbose is on.""" | |
if options.verbose: | |
print(msg) | |
def convert_params(params): | |
"""Convert parameters from string to dictionary.""" | |
paramdict = {} | |
params = params.split('&') | |
for param in params: | |
key, value = param.split('=') | |
paramdict[key] = value | |
return paramdict | |
def get_issue(options): | |
"""Get issues detail.""" | |
project_key = options.args | |
url = BASE_URL + 'rest/api/3/issue/' + project_key + '?fields=summary,-comment' | |
if project_key == 'MAX': | |
url = BASE_URL + '/rest/api/3/search?maxResults='+str(MAX_RESULTS) | |
headers = {'Accept': 'application/json'} | |
response = requests.get(url, headers=headers, auth=(BASE_USERNAME, BASE_TOKEN)) | |
parsed = response.json(), | |
json_content = json.dumps(parsed, indent=4, sort_keys=True) | |
print(u'{}'.format(json_content)) | |
print_debug(options, 'json_content: {}'.format(json_content)) | |
def get_transitions(options): | |
"""Get issues detail.""" | |
project_key = options.args | |
url = BASE_URL + 'rest/api/3/issue/' + project_key + '/transitions?expand=transitions.fields' | |
headers = {'Accept': 'application/json'} | |
response = requests.get(url, headers=headers, auth=(BASE_USERNAME, BASE_TOKEN)) | |
parsed = response.json(), | |
json_content = json.dumps(parsed, indent=4, sort_keys=True) | |
print(u'{}'.format(json_content)) | |
print_debug(options, 'json_content: {}'.format(json_content)) | |
def do_transitions(options): | |
"""Do changes on issue: operation type by id of transaction.""" | |
project_key = convert_params(options.args) | |
url = BASE_URL + 'rest/api/3/issue/' + project_key + '/transitions?expand=transitions.fields' | |
headers = {'Accept': 'application/json'} | |
response = requests.post(url, headers=headers, data=project_key) | |
content = response.text | |
json_content = response.json() | |
print('content: {}'.format(content)) | |
print_debug(options, 'action: {}'.format(json_content['action'])) | |
# Map command (string) to corresponding function. | |
fun_map = { | |
'get_issue': get_issue, | |
'get_transitions': get_transitions, | |
'do_transitions': do_transitions | |
} | |
def main(): | |
description = """\ | |
synopsis: | |
a Python JIRA Cloud REST API. | |
""" | |
epilog = """\ | |
additional arguments: | |
get_issue --args="<issue>" | |
get_transitions --args="<issue>" | |
do_transitions --args=<issue>" | |
examples: | |
python daemon.py get_issue --args="ISSUE-XXX" | |
""" | |
parser = argparse.ArgumentParser( | |
description=description, | |
epilog=epilog, | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
) | |
parser.add_argument( | |
"operation", | |
help="An operation: get_issue,get_transitions,do_transitions, ... " | |
) | |
parser.add_argument( | |
"-a", "--args", | |
default=None, | |
required=True, | |
help="Arguments: issue or MAX for MaxResults" | |
) | |
parser.add_argument( | |
"-v", "--verbose", | |
action="store_true", | |
help="Print messages during requests.", | |
) | |
options = parser.parse_args() | |
fn = fun_map.get(options.operation) | |
if fn is not None: | |
fn(options) | |
else: | |
sys.exit(__doc__) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment