Add timing keyword argument for hooks

Measure the duration of the sync operation in the Execute method of the
Sync command and pass it to post-sync hooks as a standard keyword
argument (`sync_duration_seconds`).

Updates based on code review:
- Update _API_ARGS in hooks.py to allow sync_duration_seconds for post-sync hooks.
- Do not cast sync_duration_seconds to int for better granularity.
- Update docs/repo-hooks.md to document sync_duration_seconds.
- Add unit test for argument validation in test_hooks.py.

Test: Ran run_tests using venv python, all 554 tests passed.
Bug: TBD
Change-Id: Ie29e002a5d283460d993ad96c224dbf4b6d7985c
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/575021
Tested-by: Arif Kasim <arifkasim@google.com>
Commit-Queue: Ram Peri <ramperi@google.com>
Reviewed-by: Arif Kasim <arifkasim@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Ram Peri
2026-04-21 00:02:47 +00:00
committed by gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com
parent baa281d99e
commit 5af71ce907
4 changed files with 58 additions and 5 deletions
+8 -3
View File
@@ -1986,17 +1986,19 @@ later is required to fix a server side protocol bug.
def Execute(self, opt, args):
errors = []
start_time = time.time()
try:
self._ExecuteHelper(opt, args, errors)
sync_duration_seconds = time.time() - start_time
except (RepoExitError, RepoChangedException):
raise
except (KeyboardInterrupt, Exception) as e:
raise RepoUnhandledExceptionError(e, aggregate_errors=errors)
# Run post-sync hook only after successful sync
self._RunPostSyncHook(opt)
self._RunPostSyncHook(opt, sync_duration_seconds=sync_duration_seconds)
def _RunPostSyncHook(self, opt):
def _RunPostSyncHook(self, opt, sync_duration_seconds=None):
"""Run post-sync hook if configured in manifest <repo-hooks>."""
hook = RepoHook.FromSubcmd(
hook_type="post-sync",
@@ -2004,7 +2006,10 @@ later is required to fix a server side protocol bug.
opt=opt,
abort_if_user_denies=False,
)
success = hook.Run(repo_topdir=self.client.topdir)
success = hook.Run(
repo_topdir=self.client.topdir,
sync_duration_seconds=sync_duration_seconds,
)
if not success:
print("Warning: post-sync hook reported failure.")