Skip to content

Commit 5a25a60

Browse files
committed
feat(bulk-import): add function to list all repositories for authenticated user from gitlab
Signed-off-by: Dominik Augustín <daugusti@redhat.com>
1 parent 9cc50d7 commit 5a25a60

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

  • workspaces/bulk-import/plugins/bulk-import-backend/src/gitlab/utils

workspaces/bulk-import/plugins/bulk-import-backend/src/gitlab/utils/utils.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
type GitLabIntegrationConfig,
2525
} from '@backstage/integration';
2626

27-
import { Gitlab, OffsetPagination } from '@gitbeaker/rest';
27+
import { Gitlab, OffsetPagination, ProjectSchema } from '@gitbeaker/rest';
2828

2929
import { logErrorIfNeeded } from '../../helpers';
3030
import type { CustomGitlabCredentialsProvider } from '../GitlabAppManager';
@@ -235,3 +235,35 @@ export async function fetchFromAllIntegrations<T>(
235235

236236
return { data, errors: aggregatedErrors };
237237
}
238+
239+
export async function listAllRepositoriesForAuthenticatedUser(
240+
deps: {
241+
logger: LoggerService;
242+
},
243+
gitlab: InstanceType<typeof Gitlab<false>>,
244+
options?: {
245+
pageSize?: number;
246+
},
247+
): Promise<ProjectSchema[]> {
248+
try {
249+
/**
250+
* The Projects.all method with the membership: true option will grab all the repositories/projects the gitlab token has explicit access to.
251+
* These would include repositories they own, repositories where they are a collaborator,
252+
* and repositories that they can access through an organization membership.
253+
*/
254+
const allProjects = await gitlab.Projects.all<true, 'keyset'>({
255+
membership: true,
256+
perPage: options?.pageSize,
257+
showExpanded: true,
258+
orderBy: 'name',
259+
sort: 'asc',
260+
});
261+
262+
return allProjects.data;
263+
} catch (error) {
264+
deps.logger.error(
265+
`Failed to list all repositories for authenticated user: ${error}`,
266+
);
267+
return [];
268+
}
269+
}

0 commit comments

Comments
 (0)