Last active
June 30, 2023 15:16
-
-
Save lonniev/e41591a82d8662d4d25cdb5afca1cc1e to your computer and use it in GitHub Desktop.
Extract from Jira Service Desk a complete set of Customer Names and Emails in JSON format
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%%script echo skipping\n", | |
"# remove the magic above to force this block to update your conda environment\n", | |
"\n", | |
"%%capture importslog\n", | |
"\n", | |
"import sys\n", | |
"%conda install --yes --prefix {sys.prefix} pyyaml\n", | |
"%conda install -c conda-forge --yes --prefix {sys.prefix} atlassian-python-api\n", | |
"\n", | |
"import yaml\n", | |
"import atlassian" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# retrieve credentials\n", | |
"import yaml\n", | |
"\n", | |
"with open(\"secrets.yml\", 'r') as ymlfile:\n", | |
" secrets = yaml.safe_load(ymlfile)\n", | |
"\n", | |
"assert secrets, \"Unable to read expected secrets file for credentials.\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import atlassian\n", | |
"\n", | |
"from atlassian import Jira\n", | |
"from atlassian import ServiceDesk\n", | |
"\n", | |
"service_desk = ServiceDesk(\n", | |
" url=secrets['jira']['url'],\n", | |
" username=secrets['jira']['userid'],\n", | |
" password=secrets['jira']['token'],\n", | |
" cloud=True\n", | |
" )\n", | |
"\n", | |
"# service_desk.get_customers(1)\n", | |
"service_desk.get_info()[\"_links\"][\"self\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ourSD = service_desk.get_service_desk_by_id(\n", | |
" service_desk.get_service_desks()[0][\"id\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"service_desk.get_customers( service_desk_id=3 )[\"values\"][0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import itertools\n", | |
"from typing import Callable, Union\n", | |
"\n", | |
"def findSomeCustomersByPredicate(\n", | |
" predicate: Callable[[str],str], \n", | |
" sdId: str,\n", | |
" sd: ServiceDesk\n", | |
" ) -> Union[None, dict]:\n", | |
"\n", | |
" # despite pleas otherwise, Jira will only give us 50 records at most\n", | |
" def getOnePage( pageIndex: int ) -> Union[None, list[dict]]:\n", | |
"\n", | |
" return sd.get_customers( service_desk_id = sdId, start = pageIndex * 50, limit = 50 )[ \"values\" ]\n", | |
"\n", | |
" # keep fetching pages until we exhaust the collection at the Jira service\n", | |
" return list( filter( predicate, \n", | |
" itertools.chain.from_iterable( itertools.takewhile( lambda x: x, map( \n", | |
" getOnePage, itertools.count(1) ) ) ) ) )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import re\n", | |
"\n", | |
"customers = sorted( list( map( lambda c: { 'name': c[\"displayName\"], 'email': c[\"emailAddress\"] }, \n", | |
" findSomeCustomersByPredicate( lambda x: re.match( r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$', x[\"emailAddress\"] ) is not None, \"3\", service_desk ) ) ),\n", | |
" key = lambda c: c['name'] )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"\n", | |
"json_object = json.dumps(customers, indent=2)\n", | |
" \n", | |
"with open(\"customers.json\", \"w\") as outfile:\n", | |
" outfile.write(json_object)\n", | |
"\n", | |
"customers[1]" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "sdk35", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.7" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
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
jira: | |
url: https://yours.atlassian.net | |
userid: [email protected] | |
token: BIGLONGATLASSIANAPITOKEN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment