Skip to content

Commit

Permalink
Enable setting a Google Groups prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jfreda committed Mar 28, 2024
1 parent 96b23e0 commit 28f7cac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions configs/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion internal/api/v2/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/hashicorp-forge/hermes/internal/server"
)
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 28f7cac

Please sign in to comment.