
    Je	$                         d Z ddlZddlZddlZddlZ	 ej
                  j                  d      ddlZddl	m
Z
 ddlmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ d
 Z G d de      Z G d de      Zy# e$ r Y Iw xY w)z
Backend to open issues automatically on Github.

Adapted from qcrash/backends/base.py and qcrash/backends/github.py of the
`QCrash Project <https://github.com/ColinDuquesnoy/QCrash>`_.
    NSSH_CONNECTION)Qt)QApplicationQMessageBox)CONF)_running_under_pytest)github)DlgGitHubLoginc                  4    t        j                  t              S N)logging	getLogger__name__     ?/usr/lib/python3/dist-packages/spyder/widgets/github/backend.py_loggerr   '   s    X&&r   c                   (    e Zd ZdZ	 ddZd ZddZy)BaseBackenda"  
    Base class for implementing a backend.

    Subclass must define ``button_text``, ``button_tooltip``and ``button_icon``
    and implement ``send_report(title, description)``.

    The report's title and body will be formatted automatically by the
    associated :attr:`formatter`.
    Nc                 X    || _         || _        || _        || _        || _        || _        y)aQ  
        :param formatter: the associated formatter (see :meth:`set_formatter`)
        :param button_text: Text of the associated button in the report dialog
        :param button_icon: Icon of the associated button in the report dialog
        :param button_tooltip: Tooltip of the associated button in the report
            dialog
        :param need_review: True to show the review dialog before submitting.
            Some backends (such as the email backend) do not need a review
            dialog as the user can already review it before sending the final
            report
        N)	formatterbutton_textbutton_tooltipbutton_iconneed_reviewparent_widget)selfr   r   r   r   r   r   s          r   __init__zBaseBackend.__init__5   s3     #&,&&*r   c                     || _         y)z
        Sets the formatter associated with the backend.

        The formatter will automatically get called to format the report title
        and body before ``send_report`` is being called.
        N)r   )r   r   s     r   set_formatterzBaseBackend.set_formatterI   s     #r   c                     t         )a<  
        Sends the actual bug report.

        :param title: title of the report, already formatted.
        :param body: body of the reporit, already formtatted.
        :param application_log: Content of the application log.
        Default is None.

        :returns: Whether the dialog should be closed.
        )NotImplementedError)r   titlebodyapplication_logs       r   send_reportzBaseBackend.send_reportR   s
     "!r   )NTNr   )r   
__module____qualname____doc__r   r!   r'   r   r   r   r   r   +   s     DH+(#"r   r   c                   F     e Zd ZdZd fd	Zd	dZd Zd
dZd Zd Z	 xZ
S )GithubBackendz
    This backend sends the crash report on a github issue tracker::

        https://github.com/gh_owner/gh_repo

    Usage::

        github_backend = spyder.widgets.github.backend.GithubBackend(
            'spyder-ide', 'spyder')
    c                 `    t         t        |   |ddd|       || _        || _        d| _        y)z
        :param gh_owner: Name of the owner of the github repository.
        :param gh_repo: Name of the repository on github.
        zSubmit on githubz/Submit the issue on our issue tracker on githubN)r   T)superr,   r   gh_ownergh_repo_show_msgbox)r   r/   r0   r   r   	__class__s        r   r   zGithubBackend.__init__k   s?    
 	mT+)=t' 	, 	) ! r   c                    t               j                  d||       | j                         }|d   }|d   }|yt               j                  d       |r| j                  |      }|d|z  z  }	 t	        j
                  |      } |j                  | j                        | j                        }	|	j                  j                  ||      }
|
d	   }| j                  r3t        j                  | j                  t        d
      t        d            }
|
t        j                   t        j"                  fv r/t%        j&                  d| j                  | j                  |fz         y# t        j(                  $ r}t               j+                  d|j,                         |j,                  j.                  dk(  r@| j                  rt        j*                  | j                  t        d      t        d             nV| j                  rJt        j*                  | j                  t        d      t        d      |j,                  j.                  z         Y d }~yd }~ww xY w)Nz-sending bug report on github
title=%s
body=%stokenremember_tokenFzgot user credentialsz
Application log: %s)access_token)r$   r%   numberzIssue created on GithubzQIssue successfully created. Would you like to open the issue in your web browser?z"https://github.com/%s/%s/issues/%dTz0Failed to send bug report on Github. response=%ri  zInvalid credentialsz5Failed to create Github issue, invalid credentials...zFailed to create issuez'Failed to create Github issue. Error %d)r   debugget_user_credentialsupload_log_filer
   GitHubreposr/   r0   issuespostr1   r   questionr   r   YesOk
webbrowseropenApiErrorwarningresponsecode)r   r$   r%   r&   credentialsr4   r5   urlghreporet	issue_nbres                r   r'   zGithubBackend.send_reportx   s   	It	% //1G$$%56=	./ &&7C+c11D"	E2B*288DMM*4<<8D++""T":C* HI  !**&&*C(D 3 45 {778t||Y<@ @A =  	I ,-.ZZ9 zz#%$$''**A.C,D 3 45 $$''**23CD

()
 %	s   .AE" "I5CIIc                 2    t        j                  dd      }|S )z"Get the stored credentials if any.mainreport_error/remember_token)r   get)r   r5   s     r   _get_credentials_from_settingsz,GithubBackend._get_credentials_from_settings   s    &*GHr   c                 
   |r|r	 t        j                  dd|       t        j                  dd|       y# t        $ rD | j                  r3t	        j
                  | j                  t        d      t        d             d}Y dw xY w)	zStore token for future use.r
   r4   zFailed to store tokenzIt was not possible to securely save your token. You will be prompted for your Github token next time you want to report an issue.FrP   rQ   N)
keyringset_password	Exceptionr1   r   rE   r   r   r   set)r   r4   remembers      r   _store_tokenzGithubBackend._store_token   s~    X!$$Xw> 	6A  	!$$''(:(:()*A(B() +6 )78 !	!s   5 A
BBc                    d}| j                         }|r	 t        j                  dd      }t               sZt        j                  | j                  ||      }|d   r2| j                  |d   |d          t        j                  dd|d          |S t        ||	      S # t        $ rB | j                  r3t        j                  | j                  t        d      t        d             Y w xY w)
z+Get user credentials with the login dialog.Nr
   r4   zFailed to retrieve tokenzFIt was not possible to retrieve your token. Please introduce it again.r5   rP   rQ   )r4   r5   )rS   rU   get_passwordrW   r1   r   rE   r   r   r	   r   loginrZ   r   rX   dict)r   r4   r5   rH   s       r   r9   z"GithubBackend.get_user_credentials   s    <<>	5,,Xw? $%(.."" K
 7#!!+g"6"-.>"?A!>$%568  e'57 7+  5$$''(:(:()*D(E() +3 )455s   B AC('C(c                 d   t        j                         }	 t        j                         }|j	                  t
        j                         |j                  j                  dddd|ii      }|j                          |d   S # t         j                  $ r t               j                  d       Y yw xY w)	NzSpyderIDE logTzSpyderIDE.logcontent)descriptionpublicfileshtml_urlz%Failed to upload log report as a gistz%"Failed to upload log file as a gist")r
   r;   r   instancesetOverrideCursorr   
WaitCursorgistsr>   restoreOverrideCursorrD   r   rE   )r   log_contentrJ   qApprL   s        r   r:   zGithubBackend.upload_log_file   s    ]]_	#((*D""2==1((--+D&K(@A   CC &&(
 z?"	  	;IEF:	;s   A%B   ,B/.B/)NNr   )F)r   r(   r)   r*   r   r'   rS   rZ   r9   r:   __classcell__)r2   s   @r   r,   r,   `   s)    	!3j
B$ D#r   r,   )r*   r   ossysrB   environrR   rU   rW   qtpy.QtCorer   qtpy.QtWidgetsr   r   spyder.config.managerr   spyder.config.baser   r	   spyder.utils.externalr
   spyder.widgets.github.gh_loginr   r   objectr   r,   r   r   r   <module>rw      s     	 
 		zz~~&'/  4 ' 6 ( 9'2"& 2"jS#K S#M  		s   A5 5A=<A=