From a810f92bb5b2276ba853080abbdc023b3d2f0a42 Mon Sep 17 00:00:00 2001 From: Johan Marcusson Date: Mon, 28 Oct 2024 13:03:10 +0100 Subject: [PATCH] Get commit message from branch instead of main when updating template branch sync status for devices --- src/cnaas_nms/db/git.py | 2 +- src/cnaas_nms/db/git_worktrees.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cnaas_nms/db/git.py b/src/cnaas_nms/db/git.py index 37d41cd4..775fb4f5 100644 --- a/src/cnaas_nms/db/git.py +++ b/src/cnaas_nms/db/git.py @@ -303,7 +303,7 @@ def _refresh_repo_task(repo_type: RepoType = RepoType.TEMPLATES, job_id: Optiona devtype: DeviceType for devtype, platform in updated_devtypes: Device.set_devtype_syncstatus(session, devtype, ret, "templates", platform, job_id) - refresh_existing_templates_worktrees(ret, job_id, get_group_settings_asdict(), get_device_primary_groups()) + refresh_existing_templates_worktrees(job_id, get_group_settings_asdict(), get_device_primary_groups()) return ret diff --git a/src/cnaas_nms/db/git_worktrees.py b/src/cnaas_nms/db/git_worktrees.py index ba810de3..322ae2e3 100644 --- a/src/cnaas_nms/db/git_worktrees.py +++ b/src/cnaas_nms/db/git_worktrees.py @@ -16,7 +16,7 @@ class WorktreeError(Exception): pass -def refresh_existing_templates_worktrees(by: str, job_id: int, group_settings: dict, device_primary_groups: dict): +def refresh_existing_templates_worktrees(job_id: int, group_settings: dict, device_primary_groups: dict): """Look for existing worktrees and refresh them""" logger = get_logger() updated_groups: Set[str] = set() @@ -28,6 +28,19 @@ def refresh_existing_templates_worktrees(by: str, job_id: int, group_settings: d diff = wt_repo.remotes.origin.pull() if not diff: continue + + ret: str = "" + for item in diff: + # only check for changes in our branch + if item.ref.remote_head != wt_repo.head.ref.name: + continue + + ret += "Commit {} by {} at {}\n".format( + item.commit.name_rev, item.commit.committer, item.commit.committed_datetime + ) + # don't update updated_groups if changes were only in other branches + if not ret: + continue except Exception as e: logger.exception(e) shutil.rmtree("/tmp/worktrees/" + subdir, ignore_errors=True) @@ -43,7 +56,7 @@ def refresh_existing_templates_worktrees(by: str, job_id: int, group_settings: d dev: Device = session.query(Device).filter_by(hostname=hostname).one_or_none() if dev: dev.synchronized = False - add_sync_event(hostname, "refresh_templates", by, job_id) + add_sync_event(hostname, "refresh_templates", ret, job_id) updated_hostnames.add(hostname) if updated_hostnames: logger.debug(