Merge pull request #18257 from boris154/token-without-sufficient-permissions

fix(plugin-catalog-backend-module-gitlab) Gitlab token without sufficient permissions
This commit is contained in:
Patrik Oldsberg
2023-06-20 15:23:30 +02:00
committed by GitHub
3 changed files with 35 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-gitlab': patch
---
Fix getGroupMembers with token that don't have sufficient permissions
@@ -429,6 +429,28 @@ describe('GitLabClient', () => {
expect(members).toEqual([1]);
});
it('gets member IDs with token without full permissions', async () => {
server.use(
graphql
.link(`${MOCK_CONFIG.baseUrl}/api/graphql`)
.operation((_, res, ctx) =>
res(
ctx.data({
group: {},
}),
),
),
);
const client = new GitLabClient({
config: MOCK_CONFIG,
logger: getVoidLogger(),
});
const members = await client.getGroupMembers('group1');
expect(members).toEqual([]);
});
it('rejects when GraphQL returns errors', async () => {
server.use(
graphql
@@ -127,6 +127,14 @@ export class GitLabClient {
if (response.errors) {
throw new Error(`GraphQL errors: ${JSON.stringify(response.errors)}`);
}
if (!response.data.group?.groupMembers?.nodes) {
this.logger.warn(
`Couldn't get members for group ${groupPath}. The provided token might not have sufficient permissions`,
);
continue;
}
memberIds.push(
...response.data.group.groupMembers.nodes
.filter(n => n.user)