Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

add search by group and project in one place

adilson2f56cd6d3727
diff --git a/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts b/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts
index 851890f294..6e4e075182 100644
--- a/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts
+++ b/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts
@@ -128,6 +128,44 @@ export const registerGitLabConnectionRouter = async (server: FastifyZodProvider)
}
});
+ server.route({
+ method: "GET",
+ url: `/:connectionId/search-groups-and-projects`,
+ config: {
+ rateLimit: readLimit
+ },
+ schema: {
+ operationId: "searchGitLabGroupsAndProjects",
+ params: z.object({
+ connectionId: z.string().uuid()
+ }),
+ querystring: z.object({
+ search: z.string().trim().min(1).max(255)
+ }),
+ response: {
+ 200: z
+ .object({
+ id: z.string(),
+ name: z.string(),
+ fullPath: z.string()
+ })
+ .array()
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async (req) => {
+ const { connectionId } = req.params;
+ const { search } = req.query;
+ const items: TGitLabGroupTreeItem[] = await server.services.appConnection.gitlab.searchGroupAndProject(
+ connectionId,
+ search,
+ req.permission
+ );
+
+ return items;
+ }
+ });
+
server.route({
method: "GET",
url: `/:connectionId/root-groups`,
diff --git a/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts b/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts
index ce0a355768..d26fc167fe 100644
--- a/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts
+++ b/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts
@@ -545,3 +545,37 @@ export const searchGitLabGroups = async (
throw new InternalServerError({ message: "Unable to search GitLab groups" });
}
};
+
+export const searchGitLabGroupAndProject = async (
+ search: string,
+ params: TNavigationParams
+): Promise<TGitLabGroupTreeItem[]> => {
+ const client = await getNavigationClient(params);
+ if (!client) return [];
+
+ try {
+ const groups = await client.Groups.all({
+ search
+ });
+ const projects = await client.Projects.all({
+ search,
+ archived: false,
+ includePendingDelete: false,
+ membership: true,
+ includeHidden: false,
+ imported: false
+ });
+ return [
+ ...groups.map((g) => ({ id: g.id.toString(), name: g.name, fullPath: g.fullPath })),
+ ...projects.map((p) => ({ id: p.id.toString(), name: p.name, fullPath: p.pathWithNamespace }))
+ ];
+ } catch (error: unknown) {
+ if (error instanceof GitbeakerRequestError) {
+ throw new BadRequestError({
+ message: `Failed to search GitLab group and project: ${error.message ?? "Unknown error"}${error.cause?.description && error.message !== "Unauthorized" ? `. Cause: ${error.cause.description}` : ""}`
+ });
+ }
+ if (error instanceof InternalServerError) throw error;
+ throw new InternalServerError({ message: "Unable to search GitLab group and project" });
+ }
+};
diff --git a/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts b/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts
index 45a96bf30b..a3f8ada76e 100644
--- a/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts
+++ b/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts
@@ -10,6 +10,7 @@ import {
listGitLabProjects,
listGitLabRootGroups,
listGitLabSubgroups,
+ searchGitLabGroupAndProject,
searchGitLabGroups
} from "./gitlab-connection-fns";
import { TGitLabConnection } from "./gitlab-connection-types";
@@ -92,12 +93,23 @@ export const gitlabConnectionService = (
}
};
+ const searchGroupAndProject = async (connectionId: string, search: string, actor: OrgServiceActor) => {
+ try {
+ const appConnection = await getAppConnection(AppConnection.GitLab, connectionId, actor);
+ return await searchGitLabGroupAndProject(search, { appConnection, appConnectionDAL, kmsService });
+ } catch (error) {
+ logger.error(error, `Failed to search group and project for GitLab connection ${connectionId}`);
+ return [];
+ }
+ };
+
return {
listProjects,
listGroups,
listRootGroups,
searchGroups,
listSubgroups,
- listGroupProjects
+ listGroupProjects,
+ searchGroupAndProject
};
};
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/GitLabProjectPicker.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/GitLabProjectPicker.tsx
index 0c5212ab01..87ea111533 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/GitLabProjectPicker.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/GitLabProjectPicker.tsx
@@ -18,6 +18,7 @@ import {
useGitLabConnectionListProjects,
useGitLabConnectionListRootGroups,
useGitLabConnectionListSubgroups,
+ useGitLabConnectionSearchGroupsAndProjects,
useGitLabConnectionSearchGroups
} from "@app/hooks/api/appConnections/gitlab";
import { useDebounce } from "@app/hooks/useDebounce";
@@ -367,9 +368,9 @@ export const GitLabProjectPicker = ({
enabled: hasConnection
});
- const personalCountQuery = useGitLabConnectionListProjects(
+ const personalProjectsQuery = useGitLabConnectionListProjects(
connectionId,
- { owned: true, page: 1, perPage: 1 },
+ { owned: true },
{ enabled: hasConnection }
);
@@ -385,9 +386,9 @@ export const GitLabProjectPicker = ({
}
);
- const groupsSearchQuery = useGitLabConnectionListProjects(
+ const groupsAndProjectsSearchQuery = useGitLabConnectionSearchGroupsAndProjects(
connectionId,
- { search: debouncedSearch, page: 1, perPage: GROUPS_SEARCH_PER_PAGE },
+ debouncedSearch,
{
enabled: hasConnection && tab === "groups" && isSearching
}
@@ -397,19 +398,6 @@ export const GitLabProjectPicker = ({
enabled: hasConnection && tab === "groups" && isSearching
});
- const personalProjectsQuery = useGitLabConnectionListProjects(
- connectionId,
- {
- owned: true,
- search: isSearching ? debouncedSearch : undefined,
- page: personalPage,
- perPage: PERSONAL_PROJECTS_PER_PAGE
- },
- {
- enabled: hasConnection && tab === "yours"
- }
- );
-
const handleTabChange = (next: TabKey) => {
if (next === tab) return;
setTab(next);
@@ -448,8 +436,34 @@ export const GitLabProjectPicker = ({
? subgroupsQuery.isLoading || groupProjectsQuery.isLoading
: rootGroupsQuery.isLoading);
- const personalTotalPages = personalProjectsQuery.data?.totalPages ?? 0;
- const personalTotalCount = personalProjectsQuery.data?.totalCount ?? 0;
+ const normalizedSearch = debouncedSearch.toLowerCase();
+ const personalProjects = personalProjectsQuery.data ?? [];
+ const filteredPersonalProjects = personalProjects.filter((project) =>
+ project.name.toLowerCase().includes(normalizedSearch)
+ );
+ const personalTotalCount = filteredPersonalProjects.length;
+ const personalTotalPages = Math.ceil(personalTotalCount / PERSONAL_PROJECTS_PER_PAGE);
+ const paginatedPersonalProjects = filteredPersonalProjects.slice(
+ (personalPage - 1) * PERSONAL_PROJECTS_PER_PAGE,
+ personalPage * PERSONAL_PROJECTS_PER_PAGE
+ );
+ const searchedGroupIds = new Set((groupNameSearchQuery.data ?? []).map((group) => group.id));
+ const searchedGroupProjects = (groupsAndProjectsSearchQuery.data ?? [])
+ .filter((item) => !searchedGroupIds.has(item.id))
+ .filter((project) => project.name.toLowerCase().includes(normalizedSearch))
+ .map((project) => ({ id: project.id, name: project.name }))
+ .slice(0, GROUPS_SEARCH_PER_PAGE);
+
+ useEffect(() => {
+ if (personalTotalPages === 0) {
+ if (personalPage !== 1) setPersonalPage(1);
+ return;
+ }
+
+ if (personalPage > personalTotalPages) {
+ setPersonalPage(personalTotalPages);
+ }
+ }, [personalPage, personalTotalPages]);
return (
<div className="flex min-w-0 flex-col gap-2">
@@ -472,7 +486,7 @@ export const GitLabProjectPicker = ({
icon={<User />}
label="Your projects"
isActive={tab === "yours"}
- count={personalCountQuery.data?.totalCount}
+ count={personalProjects.length}
onClick={() => handleTabChange("yours")}
/>
</div>
@@ -558,9 +572,9 @@ export const GitLabProjectPicker = ({
{hasConnection && tab === "groups" && isSearching && (
<SearchResultsList
query={debouncedSearch}
- isLoading={groupsSearchQuery.isLoading || groupNameSearchQuery.isLoading}
+ isLoading={groupsAndProjectsSearchQuery.isLoading || groupNameSearchQuery.isLoading}
groups={groupNameSearchQuery.data ?? []}
- projects={groupsSearchQuery.data?.items ?? []}
+ projects={searchedGroupProjects}
selectedProjectId={selectedProjectId}
onSelectGroup={handleSearchGroupSelect}
onSelectProject={handleProjectSelect}
@@ -601,7 +615,7 @@ export const GitLabProjectPicker = ({
{hasConnection && tab === "yours" && (
<PersonalProjectsList
isLoading={personalProjectsQuery.isLoading}
- projects={personalProjectsQuery.data?.items ?? []}
+ projects={paginatedPersonalProjects}
selectedProjectId={selectedProjectId}
onSelect={handleProjectSelect}
isSearching={isSearching}
@@ -613,7 +627,7 @@ export const GitLabProjectPicker = ({
{tab === "yours" && hasConnection && personalTotalCount > PERSONAL_PROJECTS_PER_PAGE && (
<div className="flex flex-wrap items-center justify-between gap-2 border-t border-border px-2 py-2 text-xs text-muted">
<span className="whitespace-nowrap">
- Page {personalProjectsQuery.data?.page ?? personalPage} of {personalTotalPages}
+ Page {personalPage} of {personalTotalPages}
</span>
<div className="flex shrink-0 items-center gap-1">
<Button
diff --git a/frontend/src/hooks/api/appConnections/gitlab/queries.tsx b/frontend/src/hooks/api/appConnections/gitlab/queries.tsx
index ace457c852..5bce53c43d 100644
--- a/frontend/src/hooks/api/appConnections/gitlab/queries.tsx
+++ b/frontend/src/hooks/api/appConnections/gitlab/queries.tsx
@@ -7,7 +7,6 @@ import {
TGitLabGroup,
TGitLabGroupTreeItem,
TGitLabListProjectsParams,
- TGitLabPaginatedProjects,
TGitLabProject
} from "./types";
@@ -21,6 +20,8 @@ const gitlabConnectionKeys = {
[...gitlabConnectionKeys.all, "root-groups", connectionId] as const,
searchGroups: (connectionId: string, search: string) =>
[...gitlabConnectionKeys.all, "search-groups", connectionId, search] as const,
+ searchGroupsAndProjects: (connectionId: string, search: string) =>
+ [...gitlabConnectionKeys.all, "search-groups-and-projects", connectionId, search] as const,
listSubgroups: (connectionId: string, groupId: string) =>
[...gitlabConnectionKeys.all, "subgroups", connectionId, groupId] as const,
listGroupProjects: (connectionId: string, groupId: string) =>
@@ -42,9 +43,9 @@ export const useGitLabConnectionListProjects = (
params?: TGitLabListProjectsParams,
options?: Omit<
UseQueryOptions<
- TGitLabPaginatedProjects,
+ TGitLabProject[],
unknown,
- TGitLabPaginatedProjects,
+ TGitLabProject[],
ReturnType<typeof gitlabConnectionKeys.listProjects>
>,
"queryKey" | "queryFn"
@@ -53,7 +54,7 @@ export const useGitLabConnectionListProjects = (
return useQuery({
queryKey: gitlabConnectionKeys.listProjects(connectionId, params),
queryFn: async () => {
- const { data } = await apiRequest.get<TGitLabPaginatedProjects>(
+ const { data } = await apiRequest.get<TGitLabProject[]>(
`/api/v1/app-connections/gitlab/${connectionId}/projects${buildProjectsQueryParams(params)}`
);
@@ -140,6 +141,32 @@ export const useGitLabConnectionSearchGroups = (
});
};
+export const useGitLabConnectionSearchGroupsAndProjects = (
+ connectionId: string,
+ search: string,
+ options?: Omit<
+ UseQueryOptions<
+ TGitLabGroupTreeItem[],
+ unknown,
+ TGitLabGroupTreeItem[],
+ ReturnType<typeof gitlabConnectionKeys.searchGroupsAndProjects>
+ >,
+ "queryKey" | "queryFn"
+ >
+) => {
+ return useQuery({
+ queryKey: gitlabConnectionKeys.searchGroupsAndProjects(connectionId, search),
+ queryFn: async () => {
+ const { data } = await apiRequest.get<TGitLabGroupTreeItem[]>(
+ `/api/v1/app-connections/gitlab/${connectionId}/search-groups-and-projects?search=${encodeURIComponent(search)}`
+ );
+
+ return data;
+ },
+ ...options
+ });
+};
+
export const useGitLabConnectionListSubgroups = (
connectionId: string,
groupId: string,
diff --git a/frontend/src/hooks/api/appConnections/gitlab/types.ts b/frontend/src/hooks/api/appConnections/gitlab/types.ts
index 19c64ca67a..111f71557c 100644
--- a/frontend/src/hooks/api/appConnections/gitlab/types.ts
+++ b/frontend/src/hooks/api/appConnections/gitlab/types.ts
@@ -14,13 +14,6 @@ export type TGitLabGroupTreeItem = {
fullPath: string;
};
-export type TGitLabPaginatedProjects = {
- items: TGitLabProject[];
- totalCount: number;
- totalPages: number;
- page: number;
-};
-
export type TGitLabListProjectsParams = {
owned?: boolean;
search?: string;