o ]LbmP@sdZddlmZddlmZddlmZddl Z ddl Z ddl Z ddl Z ddl mZddlmZmZmZmZmZmZmZmZmZddlmZmZdZiZeeZedd dd edd dd edd d d edddd edddd edddd edddd edddd edddd edddd edddd eddd d edddd edddd edddd edddd edd dd edd!dd edd"dd edd#d d d$Z d%Z!d e!iZ"Gd&d'd'e#Z$d,d(d)Z%d*d+Z&dS)-ahooks for sending email push notifications This extension implements hooks to send email notifications when changesets are sent from or received by the local repository. First, enable the extension as explained in :hg:`help extensions`, and register the hook you want to run. ``incoming`` and ``changegroup`` hooks are run when changesets are received, while ``outgoing`` hooks are for changesets sent to another repository:: [hooks] # one email for each incoming changeset incoming.notify = python:hgext.notify.hook # one email for all incoming changesets changegroup.notify = python:hgext.notify.hook # one email for all outgoing changesets outgoing.notify = python:hgext.notify.hook This registers the hooks. To enable notification, subscribers must be assigned to repositories. The ``[usersubs]`` section maps multiple repositories to a given recipient. The ``[reposubs]`` section maps multiple recipients to a single repository:: [usersubs] # key is subscriber email, value is a comma-separated list of repo patterns user@host = pattern [reposubs] # key is repo pattern, value is a comma-separated list of subscriber emails pattern = user@host A ``pattern`` is a ``glob`` matching the absolute path to a repository, optionally combined with a revset expression. A revset expression, if present, is separated from the glob by a hash. Example:: [reposubs] */widgets#branch(release) = qa-team@example.com This sends to ``qa-team@example.com`` whenever a changeset on the ``release`` branch triggers a notification in any repository ending in ``widgets``. In order to place them under direct user management, ``[usersubs]`` and ``[reposubs]`` sections may be placed in a separate ``hgrc`` file and incorporated by reference:: [notify] config = /path/to/subscriptionsfile Notifications will not be sent until the ``notify.test`` value is set to ``False``; see below. Notifications content can be tweaked with the following configuration entries: notify.test If ``True``, print messages to stdout instead of sending them. Default: True. notify.sources Space-separated list of change sources. Notifications are activated only when a changeset's source is in this list. Sources may be: :``serve``: changesets received via http or ssh :``pull``: changesets received via ``hg pull`` :``unbundle``: changesets received via ``hg unbundle`` :``push``: changesets sent or received via ``hg push`` :``bundle``: changesets sent via ``hg unbundle`` Default: serve. notify.strip Number of leading slashes to strip from url paths. By default, notifications reference repositories with their absolute path. ``notify.strip`` lets you turn them into relative paths. For example, ``notify.strip=3`` will change ``/long/path/repository`` into ``repository``. Default: 0. notify.domain Default email domain for sender or recipients with no explicit domain. It is also used for the domain part of the ``Message-Id`` when using ``notify.messageidseed``. notify.messageidseed Create deterministic ``Message-Id`` headers for the mails based on the seed and the revision identifier of the first commit in the changeset. notify.style Style file to use when formatting emails. notify.template Template to use when formatting emails. notify.incoming Template to use when run as an incoming hook, overriding ``notify.template``. notify.outgoing Template to use when run as an outgoing hook, overriding ``notify.template``. notify.changegroup Template to use when running as a changegroup hook, overriding ``notify.template``. notify.maxdiff Maximum number of diff lines to include in notification email. Set to 0 to disable the diff, or -1 to include all of it. Default: 300. notify.maxdiffstat Maximum number of diffstat lines to include in notification email. Set to -1 to include all of it. Default: -1. notify.maxsubject Maximum number of characters in email's subject line. Default: 67. notify.diffstat Set to True to include a diffstat before diff content. Default: True. notify.showfunc If set, override ``diff.showfunc`` for the diff content. Default: None. notify.merge If True, send notifications for merge changesets. Default: True. notify.mbox If set, append mails to this mbox file instead of sending. Default: None. notify.fromauthor If set, use the committer of the first changeset in a changegroup for the "From" field of the notification mail. If not set, take the user from the pushing repo. Default: False. notify.reply-to-predecessor (EXPERIMENTAL) If set and the changeset has a predecessor in the repository, try to thread the notification mail with the predecessor. This adds the "In-Reply-To" header to the notification mail with a reference to the predecessor with the smallest revision number. Mail threads can still be torn, especially when changesets are folded. This option must be used in combination with ``notify.messageidseed``. If set, the following entries will also be used to customize the notifications: email.from Email ``From`` address to use if none can be found in the generated email content. web.baseurl Root repository URL to combine with repository paths when making references. See also ``notify.strip``. )absolute_importN)_) encodingerror logcmdutilmailobsutilpatchpycompat registrarutil)dateutil stringutilsships-with-hg-corenotify changegroup)defaultconfigdiffstatTdomain messageidseed fromauthorsincomingmaxdiffi, maxdiffstat maxsubjectCmboxmergeoutgoingreply-to-predecessorFsourcessserveshowfuncstripstyletemplatetests Subject: changeset in {webroot}: {desc|firstline|strip} From: {author} changeset {node|short} in {root} details: {baseurl}{webroot}?cmd=changeset;node={node|short} description: {desc|tabindent|strip} sx changeset {node|short} in {root} details: {baseurl}{webroot}?cmd=changeset;node={node|short} summary: {desc|firstline} c@sReZdZdZddZddZddZdd Zd d Zd d Z ddZ dddZ dS)notifierzemail notification class.cCs~||_|jdd}|r|jj|ddgd||_t|jdd|_||jj|_|jdd|_|jdd|_ |j dd |_ t |j|_||_|j dd |_|j dd |_|jdd |_|j dd |_|jr||js|ttd|jdur|j dd |_d}|jd|p|jdd}|s|jdd}|s|st|pt}t||}t|j|j||_dS)Nrrusersubsreposubs)sectionsr"rrr%rr!rrs=notify.reply-to-predecessor used without notify.messageidseedsdiffr$r#)uiconfig readconfigrepoint stripcountstriprootdomainmbox configbooltestr _charsetscharsets subscriberssubsmergeshowfunc messageidseedreplyrAbortr deftemplatesgetsingle_templater templatespecchangesettemplatert)selfr*r-hooktypecfgmapfiletemplatespecrK./usr/lib/python3/dist-packages/hgext/notify.py__init__AsD    znotifier.__init__cCsTt|}|j}|dkr(|d}|dkr |S||dd}|d8}|dks |S)z?strip leading slashes from local path, turn into web-safe path.r/rN)r pconvertr/find)rEpathcountcrKrKrLr0is  znotifier.stripcCsLt|}|jr$|d}|dkr|d|}d|vr$|d|jS|S)z try to clean up email addresses. @localhostrN@)remailr0r2rQ)rEaddrarKrKrLfixmailvs  znotifier.fixmailcst}jdD]/\}}|dD]%}d|vr!|dd\}}nd}tjj|r7| ||fqq jdD]-\}}d|vrP|dd\}}nd}tjj|rl|dD] }| ||fq_q?fddt |DS) z;return list of email addresses of subscribers to this repo.r',#rONr(cs*g|]\}}tj|jj|fqSrK)r addressencoder*r7r5).0srrErKrL sz(notifier.subscribers..) setr* configitemssplitfnmatchr-r1r0addrZsorted)rEr9userpatspatrevsusersrKrarLr8s* znotifier.subscriberscKsP|js t|dkr dS|jj|f||jdd|jj |j d|dS)z6format one changeset, unless it is a suppressed merge.rOFswebsbaseurl)changesbaseurlr1webrootT) r:lenparentsrDshow changesetr*r+r-r1)rEctxpropsrKrKrLnodes z notifier.nodecCs|jdd}||vS)ztd t||ft j?|j t@A|dd |||jBd!dS)"z send message.Ns %r and %d:rs3notify: no subscribers to selected repo and revset FromSubject)decodes%a, %d %b %Y %H:%M:%S %1%2)formatDaterOs%s: %d new changesets s%s: %srrsemailsfromrVrUz changeset %szX-Hg-Notificationz Message-Idcs(g|]}|kr|r|qSrK)rw)r^ctx2ruhas_nodeunfirKrLrbs z!notifier.send..cSs|SN)rev)rurKrKrL sznotifier.send..)keyz In-Reply-Toz, Tos*notify: sending %d subscribers %d changes )r3)Crcr9rgr-rlrrRepoLookupErrorrqr*debugr parsebytes emailerrorsMessageParseErrorr> headdecode is_multipartitems get_payloadr ispy3 mimeencoder7r5r strfromlocalr datestrrr1 descriptionlstriprerstripr.r+rellipsis headencodeusernamerZr] messageidr2r<r= unfiltered changelogindexrrallpredecessorsobsstorerwminjoinrhas_bytes as_stringwriteendswithstatussendmail emailutils parseaddrr3)rErurSdatar9subrJrlmsginstsendersubjectheaderspayloadkvr_ maxsubject predecessorspredmsgtextrKrrLsends                     z notifier.sendNc Cst|jdd}|}|r|}n|}t|j}|j|_tj|j |||d}d | }|j ddrt|jdd}t |} | r|dkr| d|d kr| d} td } |j| t| d |f|jd | d|| d dn |jtd | |dkrdS|dkrt||krtd} |j| t||f|d|}n|r|jtdt||jd |dS)Nrr)optsrrrrrOs, diffstat (truncated from %d to %d lines): s diffstat: %ss) diffs (truncated from %d to %d lines): s diffs (%d lines): )r.r*r+p1rwr diffalloptsr;diffr-r splitlinesr4diffstatrSrerrrq) rErurefmaxdiffprevdiffoptschunks difflines maxdiffstatr_rrKrKrLr"s8     (z notifier.diffr) __name__ __module__ __qualname____doc__rMr0rZr8rwrzrrrKrKrKrLr&>s(  ur&c Kst|||}||}|js|d|jdS||r'|d|dS|d}d} d} |dks9|dkr|jj| dD]3} | || rX| d 7} | sW||  } qB|| 7}| td | || dd f|qB| r|||d n3| |vr| |s| | td | |dd fdS| d 7} ||| s| } || 7}|d d} | r| rdd| |g}| r||| |dSdS)zsend email notifications to interested subscribers. if used as changegroup hook, send one email for all changesets in changegroup. else send one email per changeset.s(notify: no subscribers to repository %s Ns,notify: changes have source "%s" - skipping rrrr)startrOs1notify: suppressing notification for merge %d:%s stiprrrsFrom: %s)r&rr9rr1rz pushbufferrrlrrwri popbuffernoterhexrr+rr) r*r-rFrwrxkwargsnrurrSauthorr fromauthorrKrKrLhookHsb           rcCs~|r|r|}ntt}|r)t||}dt| dd|f}nd|t t t | j|f}t|S)Ns @s)r strtolocalsocketgetfqdnhashlibsha512rr sysbytes hexdigestr.timehashr-r1r)rur2r<host messagehashrrKrKrLrs    r)NN)'r __future__r email.errorserrorsr email.utilsutilsrrfrrrmercurial.i18nr mercurialrrrrrr r r r mercurial.utilsr r testedwith configtable configitemrrAmultiple_templater?objectr&rrrKrKrKrLs    ,     9