Log phase transitions (draft->public)
authorDavid Douard <david.douard@logilab.fr>
Wed, 05 Mar 2014 12:16:46 +0100
changeset 373 a286d6c6b19c
parent 372 80f78674c56e
child 374 7a1d6b228af6
child 375 a41e4382ea6e
child 376 d503d5a786f3
Log phase transitions (draft->public) Ensure phase transitions are logged in the mercurial-server.log file
src/init/conf/remote-hgrc.d/logging.rc
src/mercurialserver/servelog.py
--- a/src/init/conf/remote-hgrc.d/logging.rc	Mon Nov 03 11:12:45 2014 +0100
+++ b/src/init/conf/remote-hgrc.d/logging.rc	Wed Mar 05 12:16:46 2014 +0100
@@ -3,3 +3,4 @@
 [hooks]
 changegroup.aaaaa_servelog = python:mercurialserver.servelog.hook
 outgoing.aaaaa_servelog = python:mercurialserver.servelog.hook
+pushkey.aaaaa_servelog = python:mercurialserver.servelog.phaseshook
--- a/src/mercurialserver/servelog.py	Mon Nov 03 11:12:45 2014 +0100
+++ b/src/mercurialserver/servelog.py	Wed Mar 05 12:16:46 2014 +0100
@@ -18,26 +18,44 @@
 
 from mercurialserver import ruleset, changes
 
+def writelog(repo, op, **kw):
+    """Write a log entry in the mercurial-server log file as a JSON
+    formatted dict.
+
+    op is the operation (typically 'push', 'pull' or 'publish') 
+
+    any element in the kw argument is also added in the log entry
+    dictionnary.
+    """
+    logentry = kw.copy()
+    logentry.update(dict(
+        timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
+        op=op,
+        key=ruleset.rules.get('user'),
+        ssh_connection=os.environ['SSH_CONNECTION'],))
+    with open(repo.join("mercurial-server.log"), "a+") as log:
+        fcntl.flock(log.fileno(), fcntl.LOCK_EX)
+        log.seek(0, os.SEEK_END)
+        # YAML log file format
+        log.write("- %s\n" % json.dumps(data))
+    
 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
     if hooktype == 'changegroup':
         op = "push"
     elif hooktype == 'outgoing':
         op = "pull"
     else:
-        raise mercurial.util.Abort(_('servelog installed as wrong hook type,'
-            ' must be changegroup or outgoing but is %s') % hooktype)
-    log = open(repo.join("mercurial-server.log"), "a+")
-    try:
-        fcntl.flock(log.fileno(), fcntl.LOCK_EX)
-        log.seek(0, os.SEEK_END)
-        # YAML log file format
-        log.write("- %s\n" % json.dumps(dict(
-            timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
-            op=op,
-            key=ruleset.rules.get('user'),
-            ssh_connection=os.environ['SSH_CONNECTION'],
-            nodes=[mercurial.node.hex(ctx.node())
-                for ctx in changes.changes(repo, node)],
-         )))
-    finally:
-        log.close()
+        raise mercurial.util.Abort(_('servelog.hook installed as wrong hook type,'
+                                     ' must be changegroup or outgoing but is %s') % hooktype)
+    writelog(repo, op, 
+             nodes=[mercurial.node.hex(ctx.node())
+                    for ctx in changes.changes(repo, node)])
+
+def phaseshook(ui, repo, hooktype, node=None, source=None, **kwargs):
+    if hooktype == 'pushkey':
+        if kwargs.get('namespace') == 'phases':
+            writelog(repo, 'publish', nodes=[kwargs['key']])
+    else:
+        raise mercurial.util.Abort(_('servelog.phaseshook installed as wrong hook type,'
+                                     ' must be pushkey but is %s') % hooktype)
+