Checkly group level sync support

This commit is contained in:
Andrew Atimapre
2023-10-30 18:23:23 +01:00
parent c0592ad904
commit f3c76c79ee
9 changed files with 216 additions and 7 deletions
@@ -1,7 +1,7 @@
import { Request, Response } from "express";
import { Types } from "mongoose";
import { standardRequest } from "../../config/request";
import { getApps, getTeams, revokeAccess } from "../../integrations";
import { getApps, getTeams, getGroups, revokeAccess } from "../../integrations";
import { Bot, IntegrationAuth, Workspace } from "../../models";
import { EventType } from "../../ee/models";
import { IntegrationService } from "../../services";
@@ -208,6 +208,40 @@ export const saveIntegrationToken = async (req: Request, res: Response) => {
});
};
/**
* Return list of groups allowed for integration with integration authorization id [integrationAuthId]
* @param req
* @param res
* @returns
*/
export const getIntegrationAuthGroups = async (req: Request, res: Response) => {
const {
params: { integrationAuthId }
} = await validateRequest(reqValidator.GetIntegrationAuthGroupsV1, req);
const { integrationAuth, accessToken } = await getIntegrationAuthAccessHelper({
integrationAuthId: new ObjectId(integrationAuthId)
});
const { permission } = await getUserProjectPermissions(
req.user._id,
integrationAuth.workspace.toString()
);
ForbiddenError.from(permission).throwUnlessCan(
ProjectPermissionActions.Read,
ProjectPermissionSub.Integrations
);
const groups = await getGroups({
integrationAuth: integrationAuth,
accessToken: accessToken
});
return res.status(200).send({
groups
});
};
/**
* Return list of applications allowed for integration with integration authorization id [integrationAuthId]
* @param req
+96
View File
@@ -0,0 +1,96 @@
import {
IIntegrationAuth,
} from "../models";
import {
INTEGRATION_CHECKLY,
INTEGRATION_CHECKLY_API_URL,
} from "../variables";
import { standardRequest } from "../config/request";
interface Group {
name: string;
groupId: string;
}
/**
* Return list of groups for checkly integration authorization [integrationAuth]
* @param {Object} obj
* @param {String} obj.integrationAuth - integration authorization to get groups
* @param {String} obj.accessToken - access token for integration authorization
* @returns {Object[]} groups - groups for integration authorization
* @returns {String} groups.name - name of group
* @returns {String} groups.groupId - id of group
*/
const getGroups = async ({
integrationAuth,
accessToken,
}: {
integrationAuth: IIntegrationAuth;
accessToken: string;
}) => {
let groups: Group[] = [];
switch (integrationAuth.integration) {
case INTEGRATION_CHECKLY:
groups = await getGroupsCheckly({
accessToken,
});
break;
}
return groups;
}
/**
* Return list of groups for Checkly integration
* @param {Object} obj
* @param {String} obj.accessToken - access token for Checkly API
* @returns {Object[]} groups - list of groups in Checkly
* @returns {String} groups.name - name of group
* @returns {String} groups.groupId - id of group
*/
const getGroupsCheckly = async ({
accessToken,
}: {
accessToken: string;
}) => {
let groups: Group[] = [];
// case: fetch account id
const { data } = await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/accounts`, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json"
}
});
const accountId = data.map((a: any) => {
return {
id: a.id,
};
});
// case: fetch list of groups in Checkly
const res = accountId.length > 0 && (
await standardRequest.get(`${INTEGRATION_CHECKLY_API_URL}/v1/check-groups`, {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json",
"X-Checkly-Account": accountId[0].id,
}
})
).data;
groups = res.map((g: any) => ({
name: g.name,
groupId: g.id,
}));
return groups;
}
export {
getGroups,
}
+2
View File
@@ -2,6 +2,7 @@ import { exchangeCode } from "./exchange";
import { exchangeRefresh } from "./refresh";
import { getApps } from "./apps";
import { getTeams } from "./teams";
import { getGroups } from "./groups";
import { revokeAccess } from "./revoke";
export {
@@ -9,5 +10,6 @@ export {
exchangeRefresh,
getApps,
getTeams,
getGroups,
revokeAccess,
}
+8
View File
@@ -52,6 +52,14 @@ router.get(
integrationAuthController.getIntegrationAuthTeams
);
router.get(
"/:integrationAuthId/groups",
requireAuth({
acceptedAuthModes: [AuthMode.JWT]
}),
integrationAuthController.getIntegrationAuthGroups
);
router.get(
"/:integrationAuthId/vercel/branches",
requireAuth({
@@ -117,6 +117,12 @@ export const GetIntegrationAuthVercelBranchesV1 = z.object({
})
});
export const GetIntegrationAuthGroupsV1 = z.object({
params: z.object({
integrationAuthId: z.string().trim()
})
});
export const GetIntegrationAuthQoveryOrgsV1 = z.object({
params: z.object({
integrationAuthId: z.string().trim()