
    xh2              
       h   S SK JrJrJr  S SKJr  S SKrS SKJr  S SK	J
r
  S SKJr  SSKJr  \(       a  SS	KJr  \ " S
 S5      5       r\ " S S5      5       r\ " S S5      5       r\ " S S5      5       r  S/S\S\S\\   S-  S\\   S-  4S jjrS0S\S\4S jjr\ " S S5      5       r\ " S S5      5       r\ " S S5      5       rS1S\S\S \\   S-  4S! jjrS\S\S"\\   4S# jr\ " S$ S%5      5       r\ " S& S'5      5       r S\S(\\   4S) jr!S\4S* jr" S2S+S,S\S-\S \\   S-  4S. jjr#g)3    )asdict	dataclassfield)TYPE_CHECKINGN)settings)F)ReadTimeout   )RecipientType)Notificationc                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)WebhookAttachmentField   titlevalueshort N)__name__
__module____qualname____firstlineno__str__annotations__bool__static_attributes__r       /code/apps/alerts/webhooks.pyr   r      s    JJKr   r   c                       \ rS rSr% \\S'   \\S'   \\S'   Sr\S-  \S'   Sr\S-  \S'   Sr\	\
   S-  \S'   Sr\	\   S-  \S	'   S
rg)WebhookAttachment   r   
title_linktextN	image_urlcolorfields
mrkdown_inr   )r   r   r   r   r   r   r#   r$   r%   listr   r&   r   r   r   r   r   r      sU    JO
I IsTz E3:26FD'(4/6#'JS	D 'r   r   c                   .    \ rS rSr% Sr\\S'   \\S'   Srg)MSTeamsSection!   z
Similar to WebhookAttachment but for MS Teams
https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL
activityTitleactivitySubtitler   N)r   r   r   r   __doc__r   r   r   r   r   r   r)   r)   !   s    
 r   r)   c                   J    \ rS rSr% \\S'   \\S'   \\   \S'   \\   \S'   Sr	g)WebhookPayload,   aliasr"   attachmentssectionsr   N)
r   r   r   r   r   r   r'   r   r)   r   r   r   r   r/   r/   ,   s#    J
I'((>""r   r/   urlmessager2   r3   c                     U(       d  / nU(       d  / n[        SXUS9n [        R                  " U [        U5      SS0SS9$ ! [         a     g f = f)N	GlitchTip)r1   r"   r2   r3   zContent-typezapplication/json
   )jsonheaderstimeout)r/   requestspostr   r	   )r4   r5   r2   r3   datas        r   send_webhookr?   4   se     8D	}}#%78	
 	
  s   !A 
AAissuesissue_countc                 "   / n/ nU GHh  n[        SUR                  R                  SS9/nUR                  R	                  SS9R                  [        S5      S9R                  5       nU(       a  UR                  [        SUS	   SS95        UR                  R	                  S
S9R                  [        S5      S9R                  5       n	U	(       a  UR                  [        SU	S	   SS95        UR                  R	                  SS9R                  [        S5      S9R                  5       n
U
(       a  UR                  [        SU
S	   SS95        UR                  S/ 5      nU(       az  U Ht  nUR                  R	                  US9R                  [        S5      S9R                  5       nU(       d  MI  UR                  [        UR                  5       US	   SS95        Mv     UR                  [        S/[        U5      UR                  5       UR                  UR                  5       US95        UR                  [!        [        U5      SUR"                   SUR                  5        S3S95        GMk     SnUS:  a	  USU S3-  n[%        XXE5      $ )z
Notification about issues via webhook.
url: Webhook URL
issues: This should be only the issues to send as attachment
issue_count - total issues, may be greater than len(issues)
kwargs: Additional parameters
ProjectT)r   r   r   environmenttag_key__keytag_value__valuer   Environmentr   server_nameServer NamereleaseReleaseFtags_to_addr"   )r&   r   r!   r"   r$   r%   z[View Issue z]())r+   r,   GlitchTip Alertr
    ( issues))r   projectnameissuetag_setfiltervaluesr   firstappendget
capitalizer   r   get_detail_urlculpritget_hex_colorr)   short_id_displayr?   )r4   r@   rA   kwargsr2   r3   issuer%   rD   rJ   rL   rN   tagtag_contentr5   s                  r   send_issue_as_webhookrd   M   s    ,.K%'H"mm((
 %%=%AV!./V0UW 	
 MM&'%g. %%=%AV!./V0UW 	
 MM&'%g. %%9%=V!./V0UW 	
 MM&#!'* jj3"&&--3-?V!$6"7V8UW 
 ;MM."%.."2"-g"6"' # 	"8%j //1]]))+		
 	!%j#/0F0F/Gr%J^J^J`Iaab!c	
W b  GQR}H--k<<r   c                   8    \ rS rSr% \\S'   \\S'   Sr\\S'   Srg)DiscordField   rT   r   Finliner   N)	r   r   r   r   r   r   rh   r   r   r   r   r   rf   rf      s    
IJFDr   rf   c                   N    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\   \S'   Sr	g)	DiscordEmbed   r   descriptionr$   r4   r%   r   N)
r   r   r   r   r   r   intr'   rf   r   r   r   r   rj   rj      s"    JJ	Hr   rj   c                   0    \ rS rSr% \\S'   \\   \S'   Srg)DiscordWebhookPayload   contentembedsr   N)	r   r   r   r   r   r   r'   rj   r   r   r   r   ro   ro      s    Lr   ro   rN   c                    Uc  / n/ nU GH3  n[        SUR                  R                  SS9/nUR                  R	                  SS9R                  [        S5      S9R                  5       nU(       a  UR                  [        SUS	   SS95        UR                  R	                  S
S9R                  [        S5      S9R                  5       nU(       a  UR                  [        SUS	   SS95        UR                  R	                  SS9R                  [        S5      S9R                  5       n	U	(       a  UR                  [        SU	S	   SS95        U(       az  U Ht  n
UR                  R	                  U
S9R                  [        S5      S9R                  5       nU(       d  MI  UR                  [        U
R                  5       US	   SS95        Mv     UR                  [        [        U5      UR                  UR                  5       b  [        UR                  5       SS  S5      OS UR                  5       US95        GM6     SnUS:  a	  USU S3-  n[!        XU5      $ )NrC   T)rT   r   rh   rD   rE   rG   rH   rI   r   rL   rM   FrJ   zServer namer
      )r   rl   r$   r4   r%   rP   rQ   rR   )rf   rS   rT   rU   rV   rW   r   rX   rY   r[   rj   r   r]   r^   rm   r\   send_discord_webhook)r4   r@   rA   rN   rr   ra   r%   rD   rL   rJ   rb   rc   r5   s                r   send_issue_as_discord_webhookrv      s]   !#Fmm((
 %%=%AV!./V0UW 	
 MM&%g. %%9%=V!./V0UW 	
 MM"!'*  %%=%AV!./V0UW 	
 MM&%g.  "&&--3-?V!$6"7V8UW 
 ;MM$!$!1"-g"6#( # 	%j!MM&&(4 %--/3R8((*
	
A X  GQR}H--f55r   rr   c                 R    [        XS9n[        R                  " U [        U5      SS9$ )N)rq   rr   r8   r9   r;   )ro   r<   r=   r   )r4   r5   rr   payloads       r   ru   ru     s#    #GCG==6'?B??r   c                       \ rS rSr% Sr\S-  \S'   Sr\\   S-  \S'   S\	S\	S\	S\	4S	 jr
SS\	S
\\	   S-  4S jjrSrg)GoogleChatCardi"  Nheaderr3   r   subtitler"   r4   c                     [        UUS9U l        [        [        [        U[        S[        [        US9S9S9S9S9/S9/U l        U $ )	Nr   r}   Viewr4   openLinkr"   onClick)r"   buttondecoratedText)widgets)dictr|   r3   )selfr   r}   r"   r4   s        r   construct_uptime_card$GoogleChatCard.construct_uptime_card'  s\    

 &*!%#'%+T4C=5Q$'	
 r   rN   c                 N   Uc  / n[        XR                  R                  S9U l        SR	                  UR                  5       [        U5      5      n/ nUR                  [        [        SUR                  S9S95        UR                  R                  SS9R                  [        S5      S	9R                  5       nU(       a#  UR                  [        [        S
US   S9S95        UR                  R                  SS9R                  [        S5      S	9R                  5       nU(       a#  UR                  [        [        SUS   S9S95        UR                  R                  SS9R                  [        S5      S	9R                  5       nU(       a#  UR                  [        [        SUS   S9S95        U(       a  U Hz  n	UR                  R                  U	S9R                  [        S5      S	9R                  5       n
U
(       d  MI  UR                  [        [        U	R                  5       U
S   S9S95        M|     UR                  [        [        [        SR	                  UR                  5      [        [        UR!                  5       S9S9S9/S9S95        [        XES9/U l        U $ )Nr   z<font color='{}'>{}</font>Culprit)topLabelr"   r   rD   rE   rG   rH   rI   r   rJ   rK   rL   rM   zView Issue {}r   r   r   )buttons)
buttonList)r|   r   )r   rS   rT   r|   formatr^   r   rY   r]   rU   rV   rW   r   rX   r[   r_   r\   r3   )r   r   ra   rN   section_headerr   rD   rJ   rL   rb   rc   s              r   construct_issue_card#GoogleChatCard.construct_issue_card<  se   K1C1CD5<<!3u:
 t$	*VWX%%=%AV!./V0UW 	
 NN"&!.[5I# %%=%AV!./V0UW 	
 NN"&!.[5I# %%9%=V!./V0UW 	
 NN4AQ#RS "&&--3-?V!$6"7V8UW 
 ;NN49IP[\cPd+ef # 	!0!7!78N8N!O$($5;O;O;Q2R$S		
 ^EFr   )r|   r3   N)r   r   r   r   r|   r   r   r3   r'   r   r   r   r   r   r   r   r{   r{   "  si    FD4K"&Hd4j4&3 # S s *G# G49tCS G Gr   r{   c                   B    \ rS rSr% \" \S9r\\\\	4      \
S'   S rSrg)GoogleChatWebhookPayloadi  )default_factorycardsV2c                 H    U R                   R                  [        SUS95      $ )NcreateCardMessage)cardIdcard)r   rY   r   )r   r   s     r   add_card!GoogleChatWebhookPayload.add_card  s    ||""4/B#NOOr   r   N)r   r   r   r   r   r'   r   r   r   r{   r   r   r   r   r   r   r   r     s&    /4T/JGT$sN*+,JPr   r   cardsc                     [        5       nU Vs/ s H  o2R                  U5      PM       n[        R                  " U [	        U5      SS9$ s  snf )zk
Send Google Chat compatible message as documented in
https://developers.google.com/chat/messages-overview
r8   rx   )r   r   r<   r=   r   )r4   r   ry   r   s       r   send_googlechat_webhookr     sC    
 '(G(-.d.==6'?B?? /s   A
c           	          / nU H<  n[        5       R                  SXBR                  S/ 5      S9nUR                  U5        M>     [	        X5      $ )NrP   rN   )r   ra   rN   )r{   r   rZ   rY   r   )r4   r@   r`   r   ra   r   s         r    send_issue_as_googlechat_webhookr     sU    E44#5jjXZ>[ 5 
 	T	 
 #3..r   notificationr   recipient_typec                    U R                   R                  5       nU R                   R                  5       S [        R                   nU[
        R                  :X  a  [        XXCS9  g U[
        R                  :X  a  [        XUS9  g [        XXCS9  g )N)rN   )r@   countallr   MAX_ISSUES_PER_ALERTr   DISCORDrv   GOOGLE_CHATr   rd   )r   r4   r   rN   rA   r@   s         r   send_webhook_notificationr     sv     %%++-K  $$&'F)F)FGF ...%c;X	=44	4(+Nc;Pr   )NN)r
   )r
   Nr   )$dataclassesr   r   r   typingr   r<   django.confr   django.db.modelsr   requests.exceptionsr	   	constantsr   modelsr   r   r   r)   r/   r   r'   r?   rm   rd   rf   rj   ro   rv   ru   r{   r   r   r   r   r   r   r   <module>r      s   0 0       + $$    ( ( (    # # # 37,0		 '(4/ >"T)	2^=t ^=# ^=B         
V6t V6# V6X\]`XadhXh V6r@c @C @l9K @
 ` ` `F P P P@ @T.-A @/$ / bfQ Q'*Q<?QNRSViZ^N^Qr   