Debian Codegraph @

hgext/hooklib/changeset_published.py

raw
1 # Copyright 2020 Joerg Sonnenberger <[email protected]>
2 #
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
5 """changeset_published is a hook to send a mail when an
6 existing draft changeset is moved to the public phase.
7
8 Correct message threading requires the same messageidseed to be used for both
9 the original notification and the new mail.
10
11 Usage:
12 [notify]
13 messageidseed = myseed
14
15 [hooks]
16 txnclose-phase.changeset_published = \
17 python:hgext.hooklib.changeset_published.hook
18 """
19
20 from __future__ import annotations
21
22 import email.errors as emailerrors
23 import email.utils as emailutils
24
25 from mercurial.i18n import _
26 from mercurial import (
27 encoding,
28 error,
29 formatter,
30 logcmdutil,
31 mail,
32 pycompat,
33 registrar,
34 )
35 from mercurial.utils import (
36 dateutil,
37 stringutil,
38 )
39 from .. import notify
40
41 configtable = {}
42 configitem = registrar.configitem(configtable)
43
44 configitem(
45 b'notify_published',
46 b'domain',
47 default=None,
48 )
49 configitem(
50 b'notify_published',
51 b'messageidseed',
52 default=None,
53 )
54 configitem(
55 b'notify_published',
56 b'template',
57 default=b'''Subject: changeset published
58
59 This changeset has been published.
60 ''',
61 )
62
63
64 def _report_commit(ui, repo, ctx):
65 domain = ui.config(b'notify_published', b'domain') or ui.config(
66 b'notify', b'domain'
67 )
68 messageidseed = ui.config(
69 b'notify_published', b'messageidseed'
70 ) or ui.config(b'notify', b'messageidseed')
71 template = ui.config(b'notify_published', b'template')
72 spec = formatter.literal_templatespec(template)
73 templater = logcmdutil.changesettemplater(ui, repo, spec)
74 ui.pushbuffer()
75 n = notify.notifier(ui, repo, b'incoming')
76
77 subs = set()
78 for sub, spec in n.subs:
79 if spec is None:
80 subs.add(sub)
81 continue
82 revs = repo.revs(b'%r and %d:', spec, ctx.rev())
83 if len(revs):
84 subs.add(sub)
85 continue
86 if len(subs) == 0:
87 ui.debug(
88 b'notify_published: no subscribers to selected repo and revset\n'
89 )
90 return
91
92 templater.show(
93 ctx,
94 changes=ctx.changeset(),
95 baseurl=ui.config(b'web', b'baseurl'),
96 root=repo.root,
97 webroot=n.root,
98 )
99 data = ui.popbuffer()
100
101 try:
102 msg = mail.parsebytes(data)
103 except emailerrors.MessageParseError as inst:
104 raise error.Abort(stringutil.forcebytestr(inst))
105
106 msg['In-reply-to'] = notify.messageid(ctx, domain, messageidseed)
107 msg['Message-Id'] = notify.messageid(
108 ctx, domain, messageidseed + b'-published'
109 )
110 msg['Date'] = encoding.strfromlocal(
111 dateutil.datestr(format=b"%a, %d %b %Y %H:%M:%S %1%2")
112 )
113 if not msg['From']:
114 sender = ui.config(b'email', b'from') or ui.username()
115 if b'@' not in sender or b'@localhost' in sender:
116 sender = n.fixmail(sender)
117 msg['From'] = mail.addressencode(ui, sender, n.charsets, n.test)
118 msg['To'] = ', '.join(sorted(subs))
119
120 msgtext = msg.as_bytes()
121 if ui.configbool(b'notify', b'test'):
122 ui.write(msgtext)
123 if not msgtext.endswith(b'\n'):
124 ui.write(b'\n')
125 else:
126 ui.status(_(b'notify_published: sending mail for %d\n') % ctx.rev())
127 mail.sendmail(
128 ui, emailutils.parseaddr(msg['From'])[1], subs, msgtext, mbox=n.mbox
129 )
130
131
132 def hook(ui, repo, hooktype, node=None, **kwargs):
133 if hooktype != b"txnclose-phase":
134 raise error.Abort(
135 _(b'Unsupported hook type %r') % pycompat.bytestr(hooktype)
136 )
137 ctx = repo.unfiltered()[node]
138 if kwargs['oldphase'] == b'draft' and kwargs['phase'] == b'public':
139 _report_commit(ui, repo, ctx)