I wrote a script with python to automate some things with JIRA and I used the module Requests:
#!/usr/bin/python # m.ortiz # Requiere modulo requests: http://docs.python-requests.org/en/latest/ # Documentacion de este script:FIXME # modulos import sys,requests
The thing is, when there was not network connectivity this error raised:
Traceback (most recent call last): File "ot_conectividad_jira.py", line 56, in <module> main() File "ot_conectividad_jira.py", line 34, in main r = requests.get(jiraURL+TICKET, auth=(user, pasw),timeout=5) File "/usr/lib/python2.7/site-packages/requests-2.13.0-py2.7.egg/requests/api.py", line 70, in get return request('get', url, params=params, **kwargs) File "/usr/lib/python2.7/site-packages/requests-2.13.0-py2.7.egg/requests/api.py", line 56, in request return session.request(method=method, url=url, **kwargs) File "/usr/lib/python2.7/site-packages/requests-2.13.0-py2.7.egg/requests/sessions.py", line 488, in request resp = self.send(prep, **send_kwargs) File "/usr/lib/python2.7/site-packages/requests-2.13.0-py2.7.egg/requests/sessions.py", line 609, in send r = adapter.send(request, **kwargs) File "/usr/lib/python2.7/site-packages/requests-2.13.0-py2.7.egg/requests/adapters.py", line 487, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='myjirala.jira.com', port=443): Max retries exceeded with url: /rest/api/latest/issue/TICKET-123 (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0xffb675ec>: Failed to establish a new connection: [Errno 8] Name or service not known',))
So I had to add some error handling for that. Error handling is well explained here.
The first thing to do was to import the exception from the error:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='myjirala.jira.com', port=443): Max retries exceeded with url: /rest/api/latest/issue/ESB-7483 (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0xffb675ec>: Failed to establish a new connection: [Errno 8] Name or service not known',))
Like this:
#!/usr/bin/python # m.ortiz # Requiere modulo requests: http://docs.python-requests.org/en/latest/ # Documentacion de este script:FIXME # modulos import sys,requests from requests import ConnectionError
Then define a Rise statement with a try, except block. Here «Rise» will force the error (so I don’t need to unplug my cable):
# Request Json from JIRA API try : r = requests.get(jiraURL+TICKET, auth=(user, pasw),timeout=5) rlink = requests.get(jiraURL+TICKET+jiraRlink, auth=(user,pasw), timeout=5) raise ConnectionError('Fake Error to be called') except ConnectionError : print 'Error de conectividad, verifique su conexion a internet.' exit()
Although it worked I fully tested removing the raise statement and unplugging the cable, everything worked as expected.
$ python ot_conectividad_jira.py ABC-123 [ Estas Analizando el ticket: ABC-123] Error de conectividad, verifique su conexion a internet.