
    G@d	$                        d Z ddlZddlZddlZddlZ	 ej                            d          ddlZn# e$ r Y nw xY w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dS )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__     =lib/python3.11/site-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dS )	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`.
    NTc                 Z    || _         || _        || _        || _        || _        || _        dS )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   s9     #&,&&*r   c                     || _         dS )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')
    Nc                     t          t          |                               |ddd|           || _        || _        d| _        dS )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   sX    
 	mT""++)=t' 	, 	) 	) 	) ! r   c                    t                                          d||           |                                 }|d         }|d         }|dS t                                          d           |r|                     |          }|d|z  z  }	 t	          j        |          } |                    | j                  | j                  }	|	j	        
                    ||          }
|
d	         }| j        r5t          j        | j        t          d
          t          d                    }
|
t          j        t          j        fv r$t%          j        d| j        | j        |fz             dS # t          j        $ r}t                                          d|j                   |j        j        dk    r=| j        r5t          j        | j        t          d          t          d                     nI| j        rBt          j        | j        t          d          t          d          |j        j        z             Y d }~dS 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1G$$%56=5		.///  	2&&77C+c11D"	E222B*288DM**4<88D+""T"::C* HI  5!*&*C(D(D 3 4 45 5 {7778t|Y<@ @A A A 4=  	 	 	II ,-.Z9 9 9 z#%%$ 5'*A.C,D,D 3 4 45 5 5 $ )'*233CDD
() ) )
 55555%	s   AE* *I9B=H<<Ic                 0    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Hr   Fc                    |rh|rf	 t          j        dd|           nN# t          $ rA | j        r5t	          j        | j        t          d          t          d                     d}Y nw xY wt          j	        dd|           dS )	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>>>> 	! 	! 	!$ 8'(:()*A(B(B() +6 )7 )78 8 8 !	! 	6AAAAAs    AA('A(c                    d}|                                  }|rc	 t          j        dd          }nL# t          $ r? | j        r5t          j        | j        t          d          t          d                     Y nw xY wt                      sbt          j        | j        ||          }|d         r>|                     |d         |d                    t          j        dd|d                    nt          ||	          S |S )
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   sJ   <<>> 	5	5,Xw?? 5 5 5$ 5'(:()*D(E(E() +3 )4 )45 5 55 $%% 	7(."   K
 7# 8!!+g"6"-.>"?A A A!>$%568 8 8 e'57 7 7 7 s   0 AA98A9c                 v   t          j                    }	 t          j                    }|                    t
          j                   |j                            dddd|ii          }|	                                 |d         S # t           j
        $ r% t                                          d           Y dS 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11(--+D&K(@A   C CC &&(((
 z?"	  	; 	; 	;IIEFFF:::	;s   A'B 0B87B8)NNr   )F)r   r(   r)   r*   r   r'   rS   rZ   r9   r:   __classcell__)r2   s   @r   r,   r,   `   s        	 	! ! ! ! ! !3 3 3 3j  
B B B 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      sp     				 



    		z~~&''/ 	 	 	D	       4 4 4 4 4 4 4 4 ' & & & & & 6 6 6 6 6 6 6 6 ( ( ( ( ( ( 9 9 9 9 9 9' ' '2" 2" 2" 2" 2"& 2" 2" 2"jS# S# S# S# S#K S# S# S# S# S#s   3 ;;