[docs]defsend_exception_issue(E:Exception,lastinput:str):subprocess.run(["gh","auth","login"])# user loginauth_out=subprocess.run(["gh","auth","status","-t"],capture_output=True)# retrieve user's auth token# if stdout not there, parse stderr for Tokeniflen(auth_out.stdout)==0:# parse stderr for Tokenoutput=str(auth_out.stderr).split()found_token=Falseforiinoutput:iffound_token:AUTH_TOKEN=i[0:-2]breakifi=="Token:":found_token=True# if token not found, print errorifnotfound_token:print("No token found.")print(str(auth_out.stderr)[2:len(str(auth_out.stdout))-1])returnelse:# parse stdoutoutput=str(auth_out.stdout).split()found_token=Falseforiinoutput:iffound_token:AUTH_TOKEN=i[0:-2]breakifi=="Token:":found_token=Trueprint(AUTH_TOKEN)# if token not found, print errorifnotfound_tokenandAUTH_TOKEN==None:print("No token found.")print(str(auth_out.stderr)[2:len(str(auth_out.stdout))-1])returnurl="https://api.github.com/repos/%s/%s/issues"%(REPO_OWNER,REPO_NAME)# Create an authenticated session to create the issuesession=requests.Session()# Create our issue and headers to authenticate/formatheaders={"Authorization":"token %s"%AUTH_TOKEN,"Accept":"application/vnd.github+json",}# Building the message for the body of the GitHub Issuedt_only=datetime.now().strftime("%d/%m/%Y %H:%M:%S")datetime_str="Date and time of issue: "+dt_onlyexception_str="Exception message: "+str(E)lastinput_str="Last Input: "+(lastinputif(lastinput!="")else"not provided.")# include log information herebody_message=(datetime_str+"\n\n\n"+exception_str+"\n\n\n"+lastinput_str)issue={"title":"New issue: "+dt_only,"body":body_message,"labels":["Reported Issue"],}# Post the request to create an issuer=session.post(url,data=json.dumps(issue),headers=headers)ifr.status_code==201:print("Created issue!")else:print("Couldn't create issue.")print("Response:",r.status_code,r.content)
# previous email reporting:# import smtplib, ssl# from datetime import datetime# smtp_server = "smtp.gmail.com"# port = 587 # For starttls# sender_email = "ersilia.errors@gmail.com"# password = 'xjaluhzxtrnazpro' # insecure: the password will be visible from the code. Is this permissible?# # note - turn on 2 step verification, generate an "app password", and replace the password with this app pass# sender_email = "ersilia.errors@gmail.com"# receiver_email = "ersilia.errors@gmail.com"# def send_exception_report_email(E:Exception, log_text:str = ""):# # get date/time info for message# datetime_str = datetime.now().strftime("%d/%m/%Y %H:%M:%S")# # Create a secure SSL context# context = ssl.create_default_context()# # Try to log in to server and send email# try:# server = smtplib.SMTP(smtp_server,port)# server.starttls(context=context) # Secure the connection# server.login(sender_email, password)# message = f"Subject: Exception {type(E)}: {datetime_str}\n\n"# message += f"{str(E)}\n\n"# message += log_text# server.sendmail(sender_email, receiver_email, message)# except Exception as e:# # Print any error messages to stdout# print(e)# finally:# server.quit()