From cc96285a9fdb8761fd58e8c1a502046ae96e4ba5 Mon Sep 17 00:00:00 2001 From: Fabien Boucher Date: Tue, 28 Nov 2023 19:30:42 +0000 Subject: [PATCH] Update the protob Ident and init to mempty --- codegen/Monocle/Protob/Change.hs | 83 ++++++++++++++++++++++------- schemas/monocle/protob/change.proto | 1 + src/Lentille.hs | 1 + src/Monocle/Backend/Documents.hs | 6 ++- src/Monocle/Backend/Index.hs | 2 + src/Monocle/Backend/Janitor.hs | 4 +- src/Monocle/Backend/Provisioner.hs | 2 +- src/Monocle/Backend/Test.hs | 24 ++++----- 8 files changed, 87 insertions(+), 36 deletions(-) diff --git a/codegen/Monocle/Protob/Change.hs b/codegen/Monocle/Protob/Change.hs index b704eb9b6..c0329a595 100644 --- a/codegen/Monocle/Protob/Change.hs +++ b/codegen/Monocle/Protob/Change.hs @@ -46,7 +46,11 @@ import Proto3.Wire.Decode qualified as HsProtobuf ( import Unsafe.Coerce qualified as Hs import Prelude qualified as Hs -data Ident = Ident {identUid :: Hs.Text, identMuid :: Hs.Text} +data Ident = Ident + { identUid :: Hs.Text + , identMuid :: Hs.Text + , identGroups :: Hs.Vector Hs.Text + } deriving (Hs.Show, Hs.Eq, Hs.Ord, Hs.Generic) instance Hs.NFData Ident @@ -57,18 +61,31 @@ instance HsProtobuf.Named Ident where instance HsProtobuf.HasDefault Ident instance HsProtobuf.Message Ident where - encodeMessage _ Ident {identUid = identUid, identMuid = identMuid} = - ( Hs.mconcat - [ ( HsProtobuf.encodeMessageField - (HsProtobuf.FieldNumber 1) - (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (identUid)) - ) - , ( HsProtobuf.encodeMessageField - (HsProtobuf.FieldNumber 2) - (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (identMuid)) - ) - ] - ) + encodeMessage + _ + Ident + { identUid = identUid + , identMuid = identMuid + , identGroups = identGroups + } = + ( Hs.mconcat + [ ( HsProtobuf.encodeMessageField + (HsProtobuf.FieldNumber 1) + (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (identUid)) + ) + , ( HsProtobuf.encodeMessageField + (HsProtobuf.FieldNumber 2) + (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (identMuid)) + ) + , ( HsProtobuf.encodeMessageField + (HsProtobuf.FieldNumber 3) + ( Hs.coerce @(Hs.Vector Hs.Text) + @(HsProtobuf.UnpackedVec (HsProtobuf.String Hs.Text)) + (identGroups) + ) + ) + ] + ) decodeMessage _ = (Hs.pure Ident) <*> ( HsProtobuf.coerceOver @(HsProtobuf.String Hs.Text) @(Hs.Text) @@ -83,6 +100,14 @@ instance HsProtobuf.Message Ident where (HsProtobuf.FieldNumber 2) ) ) + <*> ( HsProtobuf.coerceOver + @(HsProtobuf.UnpackedVec (HsProtobuf.String Hs.Text)) + @(Hs.Vector Hs.Text) + ( HsProtobuf.at + HsProtobuf.decodeMessageField + (HsProtobuf.FieldNumber 3) + ) + ) dotProto _ = [ ( HsProtobufAST.DotProtoField (HsProtobuf.FieldNumber 1) @@ -98,21 +123,36 @@ instance HsProtobuf.Message Ident where [] "" ) + , ( HsProtobufAST.DotProtoField + (HsProtobuf.FieldNumber 3) + (HsProtobufAST.Repeated HsProtobufAST.String) + (HsProtobufAST.Single "groups") + [] + "" + ) ] instance HsJSONPB.ToJSONPB Ident where - toJSONPB (Ident f1 f2) = + toJSONPB (Ident f1 f2 f3) = ( HsJSONPB.object [ "uid" .= (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (f1)) - , "muid" - .= (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (f2)) + , "muid" .= (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (f2)) + , "groups" + .= ( Hs.coerce @(Hs.Vector Hs.Text) + @(HsProtobuf.UnpackedVec (HsProtobuf.String Hs.Text)) + (f3) + ) ] ) - toEncodingPB (Ident f1 f2) = + toEncodingPB (Ident f1 f2 f3) = ( HsJSONPB.pairs [ "uid" .= (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (f1)) - , "muid" - .= (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (f2)) + , "muid" .= (Hs.coerce @(Hs.Text) @(HsProtobuf.String Hs.Text) (f2)) + , "groups" + .= ( Hs.coerce @(Hs.Vector Hs.Text) + @(HsProtobuf.UnpackedVec (HsProtobuf.String Hs.Text)) + (f3) + ) ] ) @@ -128,6 +168,11 @@ instance HsJSONPB.FromJSONPB Ident where <*> ( HsProtobuf.coerceOver @(HsProtobuf.String Hs.Text) @(Hs.Text) (obj .: "muid") ) + <*> ( HsProtobuf.coerceOver + @(HsProtobuf.UnpackedVec (HsProtobuf.String Hs.Text)) + @(Hs.Vector Hs.Text) + (obj .: "groups") + ) ) ) diff --git a/schemas/monocle/protob/change.proto b/schemas/monocle/protob/change.proto index c13d6f1e1..ccf9b11bc 100644 --- a/schemas/monocle/protob/change.proto +++ b/schemas/monocle/protob/change.proto @@ -10,6 +10,7 @@ import "google/protobuf/timestamp.proto"; message Ident { string uid = 1; string muid = 2; + repeated string groups = 3; } message ChangedFile { diff --git a/src/Lentille.hs b/src/Lentille.hs index 0a3abf9d1..ab429bd3f 100644 --- a/src/Lentille.hs +++ b/src/Lentille.hs @@ -126,6 +126,7 @@ toIdent host cb username = Ident {..} uid = host <> "/" <> username identUid = from uid identMuid = from $ fromMaybe username (cb uid) + identGroups = mempty ghostIdent :: Text -> Ident ghostIdent host = toIdent host (const Nothing) nobody diff --git a/src/Monocle/Backend/Documents.hs b/src/Monocle/Backend/Documents.hs index c16b441c6..b9424ba4f 100644 --- a/src/Monocle/Backend/Documents.hs +++ b/src/Monocle/Backend/Documents.hs @@ -33,6 +33,7 @@ import Monocle.Protob.Search qualified as SearchPB data Author = Author { authorMuid :: LText , authorUid :: LText + , authorGroups :: [LText] } deriving (Show, Eq, Generic) @@ -47,12 +48,13 @@ instance From ChangePB.Ident Author where Author { authorMuid = identMuid , authorUid = identUid + , authorGroups = mempty } fromMaybeIdent :: Maybe ChangePB.Ident -> Author fromMaybeIdent = maybe ghostAuthor from where - ghostAuthor = Author "backend-ghost" "backend-ghost" + ghostAuthor = Author "backend-ghost" "backend-ghost" mempty -- | CachedAuthor is used by the Author search cache data CachedAuthor = CachedAuthor @@ -116,7 +118,7 @@ instance FromJSON Commit where ensureAuthor :: Maybe ChangePB.Ident -> ChangePB.Ident ensureAuthor = \case Just i -> i - Nothing -> ChangePB.Ident "backend-ghost" "backend-host" + Nothing -> ChangePB.Ident "backend-ghost" "backend-host" mempty instance From ChangePB.Commit Commit where from ChangePB.Commit {..} = diff --git a/src/Monocle/Backend/Index.hs b/src/Monocle/Backend/Index.hs index 0fd1a37c5..947f7135c 100644 --- a/src/Monocle/Backend/Index.hs +++ b/src/Monocle/Backend/Index.hs @@ -482,11 +482,13 @@ toAuthor (Just ChangePB.Ident {..}) = Monocle.Backend.Documents.Author { authorMuid = identMuid , authorUid = identUid + , authorGroups = toList identGroups } toAuthor Nothing = Monocle.Backend.Documents.Author "backend-ghost" "backend-ghost" + mempty -- TODO: change that to a From instance toEChangeEvent :: ChangePB.ChangeEvent -> EChangeEvent diff --git a/src/Monocle/Backend/Janitor.hs b/src/Monocle/Backend/Janitor.hs index a2b5cf49b..14ad78b0b 100644 --- a/src/Monocle/Backend/Janitor.hs +++ b/src/Monocle/Backend/Janitor.hs @@ -28,9 +28,9 @@ import Streaming.Prelude qualified as Streaming updateAuthor :: Config.Index -> D.Author -> D.Author updateAuthor index author@D.Author {..} = case getIdent of - Just ident -> D.Author ident authorUid + Just ident -> D.Author ident authorUid mempty Nothing - | newMuid /= from authorMuid -> D.Author (from newMuid) authorUid + | newMuid /= from authorMuid -> D.Author (from newMuid) authorUid mempty | otherwise -> author where getIdent :: Maybe LText diff --git a/src/Monocle/Backend/Provisioner.hs b/src/Monocle/Backend/Provisioner.hs index f1561725f..bfc0c71e7 100644 --- a/src/Monocle/Backend/Provisioner.hs +++ b/src/Monocle/Backend/Provisioner.hs @@ -100,7 +100,7 @@ fakeTitle = from <$> Faker.Movie.BackToTheFuture.quotes fakeAuthor :: Faker.Fake Author fakeAuthor = do name <- from <$> Faker.TvShow.Futurama.characters - pure $ Author name name + pure $ Author name name mempty fakeText :: Faker.Fake LText fakeText = from <$> Faker.TvShow.Futurama.quotes diff --git a/src/Monocle/Backend/Test.hs b/src/Monocle/Backend/Test.hs index d047030d5..fa74da53a 100644 --- a/src/Monocle/Backend/Test.hs +++ b/src/Monocle/Backend/Test.hs @@ -41,11 +41,11 @@ fakeDate = [utctime|2021-05-31 10:00:00|] fakeDateAlt = [utctime|2021-06-01 20:00:00|] alice, bob, eve, fakeAuthor, fakeAuthorAlt :: Author -alice = Author "alice" "a" -bob = Author "bob" "b" -eve = Author "eve" "e" -fakeAuthor = Author "John" "John" -fakeAuthorAlt = Author "John Doe/12" "review.opendev.org/John Doe/12" +alice = Author "alice" "a" mempty +bob = Author "bob" "b" mempty +eve = Author "eve" "e" mempty +fakeAuthor = Author "John" "John" mempty +fakeAuthorAlt = Author "John Doe/12" "review.opendev.org/John Doe/12" mempty fakeChangePB :: ChangePB.Change fakeChangePB = @@ -603,7 +603,7 @@ testJanitorUpdateIdents = do } mkIdent :: [Text] -> Text -> Config.Ident mkIdent uid = Config.Ident uid Nothing - expectedAuthor = Author "John Doe" "github.com/john" + expectedAuthor = Author "John Doe" "github.com/john" mempty doUpdateIndentOnEventsTest :: Eff [MonoQuery, ElasticEffect, LoggerEffect, IOE] () doUpdateIndentOnEventsTest = E.runFailIO do @@ -617,8 +617,8 @@ testJanitorUpdateIdents = do evt2' <- I.getChangeEventById $ I.getEventDocId evt2 assertEqual' "Ensure event not changed" evt2' $ Just evt2 where - evt1 = mkEventWithAuthor "e1" (Author "john" "github.com/john") - evt2 = mkEventWithAuthor "e2" (Author "paul" "github.com/paul") + evt1 = mkEventWithAuthor "e1" (Author "john" "github.com/john" mempty) + evt2 = mkEventWithAuthor "e2" (Author "paul" "github.com/paul" mempty) mkEventWithAuthor :: -- eventId Text -> @@ -647,10 +647,10 @@ testJanitorUpdateIdents = do -- then default is to remove the "/" prefix checkEChangeField (I.getChangeDocId change3) echangeAuthor expectedAuthor' where - expectedAuthor' = Author "jane" "github.com/jane" - change1 = mkChangeWithAuthor "c1" (Author "john" "github.com/john") - change2 = mkChangeWithAuthor "c2" (Author "paul" "github.com/paul") - change3 = mkChangeWithAuthor "c3" (Author "Ident will revert" "github.com/jane") + expectedAuthor' = Author "jane" "github.com/jane" mempty + change1 = mkChangeWithAuthor "c1" (Author "john" "github.com/john" mempty) + change2 = mkChangeWithAuthor "c2" (Author "paul" "github.com/paul" mempty) + change3 = mkChangeWithAuthor "c3" (Author "Ident will revert" "github.com/jane" mempty) mkChangeWithAuthor :: -- changeId Text ->