From 28f7cac4e241681cf91355cc767a8d3fc0749273 Mon Sep 17 00:00:00 2001 From: Josh Freda Date: Thu, 28 Mar 2024 17:16:49 -0500 Subject: [PATCH] Enable setting a Google Groups prefix --- configs/config.hcl | 3 +++ internal/api/v2/groups.go | 16 +++++++++++++++- internal/config/config.go | 3 +++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/configs/config.hcl b/configs/config.hcl index c889c35b9..0f9f2630d 100644 --- a/configs/config.hcl +++ b/configs/config.hcl @@ -122,6 +122,9 @@ google_workspace { // drafts_folder contains all draft documents. drafts_folder = "my-drafts-folder-id" + // groups_prefix is the prefix to use when searching for Google Groups. + // groups_prefix = "team-" + // If create_doc_shortcuts is set to true, shortcuts_folder will contain an // organized hierarchy of folders and shortcuts to published files that can be // easily browsed directly in Google Drive: diff --git a/internal/api/v2/groups.go b/internal/api/v2/groups.go index 03e0b549f..84cab6a73 100644 --- a/internal/api/v2/groups.go +++ b/internal/api/v2/groups.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "github.com/hashicorp-forge/hermes/internal/server" ) @@ -50,11 +51,24 @@ func GroupsHandler(srv server.Server) http.Handler { return } + // Sanitize query. + query := req.Query + query = strings.ReplaceAll(query, " ", "-") + + // Apply groups prefix, if applicable. + if srv.Config.GoogleWorkspace.GroupsPrefix != "" { + // Only apply prefix if query does not already have it. + if !strings.HasPrefix(query, srv.Config.GoogleWorkspace.GroupsPrefix) { + query = fmt.Sprintf( + "%s%s", srv.Config.GoogleWorkspace.GroupsPrefix, query) + } + } + // Retrieve groups. groups, err := srv.GWService.AdminDirectory.Groups.List(). Domain(srv.Config.GoogleWorkspace.Domain). MaxResults(10). - Query(fmt.Sprintf("email:%s*", req.Query)). + Query(fmt.Sprintf("email:%s*", query)). Do() if err != nil { srv.Logger.Error("error searching groups", diff --git a/internal/config/config.go b/internal/config/config.go index 199248443..38afabd5b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -219,6 +219,9 @@ type GoogleWorkspace struct { // DraftsFolder is the folder that contains all document drafts. DraftsFolder string `hcl:"drafts_folder"` + // GroupsPrefix is the prefix to use when searching for Google Groups. + GroupsPrefix string `hcl:"groups_prefix,optional"` + // OAuth2 is the configuration to use OAuth 2.0 to access Google Workspace // APIs. OAuth2 *GoogleWorkspaceOAuth2 `hcl:"oauth2,block"`