From 123f3090482794a90025b6d9289d4e0d02999b9e Mon Sep 17 00:00:00 2001 From: Cabbie Team Date: Tue, 16 Jul 2024 14:58:33 -0700 Subject: [PATCH] Add support for automatically unhiding updates when they are removed from enforcement.json PiperOrigin-RevId: 652989914 --- cabbie.go | 4 ++++ hide.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/cabbie.go b/cabbie.go index 3ed08db..2a42f5e 100644 --- a/cabbie.go +++ b/cabbie.go @@ -314,6 +314,10 @@ func enforce() error { deck.ErrorA(failures).With(eventID(cablib.EvtErrHide)).Go() } } + if err := unHideUpdates(updates); err != nil { + failures = fmt.Errorf("error unhiding updates: %v", err) + deck.ErrorA(failures).With(eventID(cablib.EvtErrUnhide)).Go() + } return failures } diff --git a/hide.go b/hide.go index b3cc76b..c684f6e 100644 --- a/hide.go +++ b/hide.go @@ -21,10 +21,12 @@ import ( "flag" "github.com/google/cabbie/cablib" + "github.com/google/cabbie/enforcement" "github.com/google/cabbie/search" "github.com/google/cabbie/session" "github.com/google/cabbie/updatecollection" "github.com/google/deck" + "golang.org/x/exp/slices" "github.com/google/subcommands" ) @@ -129,6 +131,37 @@ func hide(kbs KBSet) error { return nil } +func unHideUpdates(upd enforcement.Enforcements) error { + // Find hidden updates. + uc, err := findUpdates("IsHidden=1") + if err != nil { + return err + } + defer uc.Close() + deck.InfofA("Found %d hidden updates.", len(uc.Updates)).With(eventID(cablib.EvtUnhide)).Go() + // Check if currently hidden updates are still in the list of hidden updates. + // If they are, continue. If they are not still in the list, unhide them. + for _, u := range uc.Updates { + var skip bool + if slices.Contains(upd.HiddenUpdateID, u.Identity.UpdateID) { + continue + } + for _, kb := range u.KBArticleIDs { + if slices.Contains(upd.Hidden, kb) { + skip = true + } + } + if skip { + continue + } + deck.InfofA("Unhiding update:\n%s", u.Title).With(eventID(cablib.EvtUnhide)).Go() + if err := u.UnHide(); err != nil { + deck.ErrorfA("Failed to unhide update %s:\n %s", u.Title, err).With(eventID(cablib.EvtErrUnhide)).Go() + } + } + return nil +} + func hideByUpdateID(uuids []string) error { // Find non-hidden updates that are installed or not installed. uc, err := findUpdates("IsHidden=0 and IsInstalled=0 or IsHidden=0 and IsInstalled=1")