Skip to content

Commit

Permalink
Add pagination for group fetches and enhance logging
Browse files Browse the repository at this point in the history
  • Loading branch information
subbuvenk-atlas committed Jan 14, 2025
1 parent 0ae994a commit 95d80d4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/client/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const callGitlab = async (
body,
});

console.log(`Gitlab response status: ${resp.status}`);

if (resp.status === 204) {
// no content, we can just return here
return null;
Expand All @@ -105,20 +107,20 @@ export const getGroupsData = async (
owned?: string,
minAccessLevel?: number,
name?: string,
pageSize = 100,
): Promise<GitlabAPIGroup[]> => {
const params = {
...(owned ? { owned } : {}),
...(minAccessLevel ? { min_access_level: minAccessLevel.toString() } : {}),
...(name ? { search: name } : {}),
...(pageSize ? { per_page: pageSize.toString() } : {}),
};

const queryParams = queryParamsGenerator(params);

const { data } = await callGitlab(
`getGroupsData - Query params: ${queryParams}`,
`/api/v4/groups?${queryParams}`,
groupAccessToken,
);
const { data } = await callGitlab(`getGroupsData`, `/api/v4/groups?${queryParams}`, groupAccessToken);

console.log('Number of groups fetched:', data.length);

return data;
};
Expand Down
4 changes: 4 additions & 0 deletions src/resolvers/shared-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const groupsAllExisting = async (): Promise<ResolverResponse<GitlabAPIGro
};

export const connectedGroupsInfo = async (): Promise<ResolverResponse<GitlabAPIGroup[]>> => {
console.log('Fetching connected groups info');
try {
const connectedGroups = await getConnectedGroups();
const setupConfig = await getWebhookSetupConfig();
Expand All @@ -53,6 +54,7 @@ export const connectedGroupsInfo = async (): Promise<ResolverResponse<GitlabAPIG

return { success: true, data: connectedGroups };
} catch (e) {
console.log('Error fetching connected groups info', e);
return {
success: false,
errors: [{ message: 'Get connected groups failed.', errorType: AuthErrorTypes.UNEXPECTED_ERROR }],
Expand All @@ -76,13 +78,15 @@ export const appId = (): ResolverResponse<string> => {
};

export const webhookSetupConfig = async (): Promise<ResolverResponse<WebhookSetupConfig>> => {
console.log('Fetching webhook setup config');
try {
const config = await getWebhookSetupConfig();
return {
success: true,
data: config,
};
} catch (e) {
console.log('Error fetching webhook setup config', e);
return {
success: false,
errors: [{ message: e.message, errorType: DefaultErrorTypes.UNEXPECTED_ERROR }],
Expand Down
3 changes: 3 additions & 0 deletions src/services/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ export const connectGroupAsMaintainer = async (token: string, groupName: string)

let groups: GitlabAPIGroup[];
try {
console.log('Fetching groups data for Maintainer token role');
groups = await getGroupsData(token, null, null, groupName);
} catch (e) {
console.log('Error fetching groups data for Maintainer token role');
throw new InvalidGroupTokenError(AuthErrorTypes.INVALID_GROUP_TOKEN);
}

Expand All @@ -79,6 +81,7 @@ export const connectGroupAsOwner = async (
let groupId;
let groupName;
try {
console.log('Fetching groups data for Owner token role');
const [group] = await getGroupsData(token, 'true');
({ id: groupId, name: groupName } = group);
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion src/services/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ export const getWebhookSetupConfig = async (): Promise<WebhookSetupConfig> => {
const groupsResult = await Promise.allSettled(groups.map((group: Result) => storage.get(group.key)));

if (hasRejections(groupsResult)) {
throw new Error(`Error getting groupIds with in-progress webhooks setup: ${getFormattedErrors(groupsResult)}`);
const errorMsg = `Error getting groupIds with in-progress webhooks setup: ${getFormattedErrors(groupsResult)}`;
console.log(errorMsg);
throw new Error(errorMsg);
}

const groupIds = groupsResult.map((groupResult: PromiseFulfilledResult<number>) => groupResult.value);
Expand Down

0 comments on commit 95d80d4

Please sign in to comment.