← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~ilasc/turnip:add-repack-data-notify into turnip:master

 

Ioana Lasc has proposed merging ~ilasc/turnip:add-repack-data-notify into turnip:master.

Commit message:
Add repack data to notify call

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~ilasc/turnip/+git/turnip/+merge/397972
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~ilasc/turnip:add-repack-data-notify into turnip:master.
diff --git a/turnip/pack/git.py b/turnip/pack/git.py
index e871091..b683f7b 100644
--- a/turnip/pack/git.py
+++ b/turnip/pack/git.py
@@ -679,8 +679,12 @@ class PackBackendProtocol(PackServerProtocol):
         if self.command == b'turnip-set-symbolic-ref':
             if reason.check(error.ProcessDone):
                 try:
+                    loose_object_count, pack_count= (
+                        self.factory.hookrpc_handler.repack_data(
+                            self.path))
                     yield self.factory.hookrpc_handler.notify(
-                        self.raw_pathname)
+                        self.raw_pathname, loose_object_count, pack_count,
+                        self.factory.hookrpc_handler.auth_params)
                     self.sendPacket(b'ACK %s\n' % self.symbolic_ref_name)
                 except Exception as e:
                     message = str(e)
diff --git a/turnip/pack/hookrpc.py b/turnip/pack/hookrpc.py
index 15f5187..42431b6 100644
--- a/turnip/pack/hookrpc.py
+++ b/turnip/pack/hookrpc.py
@@ -1,4 +1,4 @@
-# Copyright 2015-2018 Canonical Ltd.  This software is licensed under the
+# Copyright 2015-2021 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
 """RPC server for Git hooks.
@@ -23,8 +23,10 @@ from __future__ import (
 import base64
 import json
 
+import os
 import six
 from six.moves import xmlrpc_client
+import subprocess
 from twisted.internet import (
     defer,
     protocol,
@@ -229,20 +231,40 @@ class HookRPCHandler(object):
              for ref in paths})
 
     @defer.inlineCallbacks
-    def notify(self, path):
+    def notify(self, path, loose_object_count, pack_count, auth_params):
         proxy = xmlrpc.Proxy(self.virtinfo_url, allowNone=True)
-        yield proxy.callRemote('notify', six.ensure_str(path)).addTimeout(
+        statistics = dict([("loose_object_count", loose_object_count),
+        ("pack_count", pack_count)])
+        yield proxy.callRemote('notify', six.ensure_str(path),
+                               statistics,
+                               auth_params).addTimeout(
             self.virtinfo_timeout, self.reactor)
 
+    def repack_data(self, path):
+        # curdir = os.getcwd()
+        # os.chdir(path)
+        count = subprocess.check_output(
+            ['git', 'count-objects', '-v']).decode("utf-8")
+
+        packs = int(count[count.find('packs: ')+len('packs: '):count.find('packs: ')+len('packs: ')+1])
+        objects = int(count[count.find('\n')-1:count.find('\n')])
+
+        # os.chdir(curdir)
+        return objects, packs
+
     @defer.inlineCallbacks
     def notifyPush(self, proto, args):
         """Notify the virtinfo service about a push."""
         log_context = HookRPCLogContext(self.auth_params[args['key']])
         path = self.ref_paths[args['key']]
+        auth_params = self.auth_params[args['key']]
         log_context.log.info(
             "notifyPush request received: ref_path={path}", path=path)
         try:
-            yield self.notify(path)
+            # get the number of loose objects and packs
+            loose_object_count, pack_count = self.repack_data(path)
+            yield self.notify(path, loose_object_count,
+                              pack_count, auth_params)
         except defer.TimeoutError:
             log_context.log.info(
                 "notifyPush timed out: ref_path={path}", path=path)
diff --git a/turnip/pack/tests/fake_servers.py b/turnip/pack/tests/fake_servers.py
index 92b77b9..14625c2 100644
--- a/turnip/pack/tests/fake_servers.py
+++ b/turnip/pack/tests/fake_servers.py
@@ -114,8 +114,9 @@ class FakeVirtInfoService(xmlrpc.XMLRPC):
         self.authentications.append((username, password))
         return {'user': username}
 
-    def xmlrpc_notify(self, path):
-        self.push_notifications.append(path)
+    def xmlrpc_notify(self, path, statistics, auth_params):
+        self.push_notifications.append((path, statistics, 
+                                        auth_params))
 
     def xmlrpc_checkRefPermissions(self, path, ref_paths, auth_params):
         self.ref_permissions_checks.append((path, ref_paths, auth_params))
diff --git a/turnip/pack/tests/test_functional.py b/turnip/pack/tests/test_functional.py
index 02bf771..6d090fa 100644
--- a/turnip/pack/tests/test_functional.py
+++ b/turnip/pack/tests/test_functional.py
@@ -731,8 +731,8 @@ class FrontendFunctionalTestMixin(FunctionalTestMixin):
         yield self.assertCommandSuccess(
             (b'git', b'push', b'origin', b'master'), path=clone1)
         self.assertEqual(
-            [six.ensure_text(self.internal_name)],
-            self.virtinfo.push_notifications)
+            six.ensure_text(self.internal_name),
+            self.virtinfo.push_notifications[0][0])
 
     @defer.inlineCallbacks
     def test_unicode_fault(self):
@@ -849,8 +849,8 @@ class TestSmartHTTPFrontendFunctional(FrontendFunctionalTestMixin, TestCase):
         head_target = yield self.get_symbolic_ref(repo, b'HEAD')
         self.assertEqual(b'refs/heads/new-head', head_target)
         self.assertEqual(
-            [six.ensure_text(self.internal_name)],
-            self.virtinfo.push_notifications)
+            six.ensure_text(self.internal_name),
+            self.virtinfo.push_notifications[0][0])
 
     @defer.inlineCallbacks
     def test_turnip_set_symbolic_ref_error(self):
diff --git a/turnip/pack/tests/test_hookrpc.py b/turnip/pack/tests/test_hookrpc.py
index a4b0d19..61ea968 100644
--- a/turnip/pack/tests/test_hookrpc.py
+++ b/turnip/pack/tests/test_hookrpc.py
@@ -11,6 +11,7 @@ import base64
 import contextlib
 import uuid
 
+import os
 from six.moves import xmlrpc_client
 from testtools import (
     ExpectedException,
@@ -327,8 +328,17 @@ class TestHookRPCHandler(TestCase):
     @defer.inlineCallbacks
     def test_notifyPush(self):
         with self.registeredKey('/translated') as key:
+        #with self.registeredKey(os.getcwd()) as key:
             yield self.hookrpc_handler.notifyPush(None, {'key': key})
-        self.assertEqual(['/translated'], self.virtinfo.push_notifications)
+
+        # notify will now return in this format:
+        # [('/translated', '1035 objects, 2298 kilobytes', 2)]
+        # with the numbers being different of course for each
+        # repository state
+        self.assertEquals('/translated',
+                          self.virtinfo.push_notifications[0][0])
+        # self.assertEquals(0, self.virtinfo.push_notifications[0][1])
+        # self.assertEquals(0, self.virtinfo.push_notifications[0][2])
 
     def test_notifyPush_timeout(self):
         clock = task.Clock()

Follow ups