Checkpoint GitLab integration group support

This commit is contained in:
Tuan Dang
2023-03-09 11:52:34 +07:00
parent 4edfc1e0be
commit 3f3516b7ba
21 changed files with 572 additions and 149 deletions
@@ -8,7 +8,11 @@ import {
} from '../../models';
import { INTEGRATION_SET, INTEGRATION_OPTIONS } from '../../variables';
import { IntegrationService } from '../../services';
import { getApps, revokeAccess } from '../../integrations';
import {
getApps,
getTeams,
revokeAccess
} from '../../integrations';
/***
* Return integration authorization with id [integrationAuthId]
@@ -154,25 +158,54 @@ export const saveIntegrationAccessToken = async (
* @returns
*/
export const getIntegrationAuthApps = async (req: Request, res: Response) => {
let apps;
try {
apps = await getApps({
integrationAuth: req.integrationAuth,
accessToken: req.accessToken,
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: "Failed to get integration authorization applications",
});
}
let apps;
try {
const teamId = req.query.teamId as string;
return res.status(200).send({
apps,
});
apps = await getApps({
integrationAuth: req.integrationAuth,
accessToken: req.accessToken,
...teamId && { teamId }
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: "Failed to get integration authorization applications",
});
}
return res.status(200).send({
apps
});
};
/**
* Return list of teams allowed for integration with integration authorization id [integrationAuthId]
* @param req
* @param res
* @returns
*/
export const getIntegrationAuthTeams = async (req: Request, res: Response) => {
let teams;
try {
teams = await getTeams({
integrationAuth: req.integrationAuth,
accessToken: req.accessToken
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: "Failed to get integration authorization teams"
});
}
return res.status(200).send({
teams
});
}
/**
* Delete integration authorization with id [integrationAuthId]
* @param req
+5
View File
@@ -24,22 +24,27 @@ import {
INTEGRATION_CIRCLECI_API_URL,
INTEGRATION_TRAVISCI_API_URL,
} from "../variables";
import { requireIntegrationAuthorizationAuth } from "../middleware";
/**
* Return list of names of apps for integration named [integration]
* @param {Object} obj
* @param {String} obj.integration - name of integration
* @param {String} obj.accessToken - access token for integration
* @param {String} obj.teamId - (optional) id of team for getting integration apps (used for integrations like GitLab)
* @returns {Object[]} apps - names of integration apps
* @returns {String} apps.name - name of integration app
*/
const getApps = async ({
integrationAuth,
accessToken,
teamId
}: {
integrationAuth: IIntegrationAuth;
accessToken: string;
teamId?: string;
}) => {
interface App {
name: string;
appId?: string;
+45 -3
View File
@@ -13,6 +13,7 @@ import {
INTEGRATION_NETLIFY_TOKEN_URL,
INTEGRATION_GITHUB_TOKEN_URL,
INTEGRATION_GITLAB_TOKEN_URL,
INTEGRATION_GITLAB_API_URL
} from '../variables';
import {
SITE_URL,
@@ -73,7 +74,7 @@ interface ExchangeCodeGithubResponse {
interface ExchangeCodeGitlabResponse {
access_token: string;
token_type: string;
expires_in: string;
expires_in: number;
refresh_token: string;
scope: string;
created_at: number;
@@ -370,6 +371,7 @@ const exchangeCodeGithub = async ({ code }: { code: string }) => {
*/
const exchangeCodeGitlab = async ({ code }: { code: string }) => {
let res: ExchangeCodeGitlabResponse;
const accessExpiresAt = new Date();
try {
res = (
@@ -389,6 +391,46 @@ const exchangeCodeGitlab = async ({ code }: { code: string }) => {
}
)
).data;
// 1. try getting groups
// https://gitlab.com/api/v4/groups
// const res2 = (await request.get(
// `${INTEGRATION_GITLAB_API_URL}/v4/groups`,
// {
// headers: {
// Authorization: `Bearer ${res.access_token}`,
// "Accept-Encoding": "application/json"
// }
// }
// )).data;
// 2. try getting projects of that group
// const res3 = (await request.get(
// `${INTEGRATION_GITLAB_API_URL}/v4/groups/${res2[0].id}`,
// {
// headers: {
// Authorization: `Bearer ${res.access_token}`,
// "Accept-Encoding": "application/json"
// }
// }
// )).data;
// const res = (
// await request.get(
// `${INTEGRATION_GITLAB_API_URL}/v4/users/${id}/projects`,
// {
// headers: {
// "Authorization": `Bearer ${accessToken}`,
// "Accept-Encoding": "application/json",
// },
// }
// )
// ).data;
accessExpiresAt.setSeconds(
accessExpiresAt.getSeconds() + res.expires_in
);
}catch (err) {
Sentry.setUser(null);
Sentry.captureException(err);
@@ -397,8 +439,8 @@ const exchangeCodeGitlab = async ({ code }: { code: string }) => {
return {
accessToken: res.access_token,
refreshToken: null,
accessExpiresAt: null
refreshToken: res.refresh_token,
accessExpiresAt
};
}
+2
View File
@@ -1,6 +1,7 @@
import { exchangeCode } from './exchange';
import { exchangeRefresh } from './refresh';
import { getApps } from './apps';
import { getTeams } from './teams';
import { syncSecrets } from './sync';
import { revokeAccess } from './revoke';
@@ -8,6 +9,7 @@ export {
exchangeCode,
exchangeRefresh,
getApps,
getTeams,
syncSecrets,
revokeAccess
}
+74
View File
@@ -0,0 +1,74 @@
import * as Sentry from "@sentry/node";
import {
IIntegrationAuth
} from '../models';
import {
INTEGRATION_GITLAB,
INTEGRATION_GITLAB_API_URL
} from '../variables';
import request from '../config/request';
const getTeams = async ({
integrationAuth,
accessToken
}: {
integrationAuth: IIntegrationAuth;
accessToken: string;
}) => {
interface Team {
name: string;
teamId: string;
}
let teams: Team[] = [];
try {
switch (integrationAuth.integration) {
case INTEGRATION_GITLAB:
teams = await getTeamsGitLab({
accessToken
});
break;
}
} catch (err) {
Sentry.setUser(null);
Sentry.captureException(err);
throw new Error('Failed to get integration teams');
}
return teams;
}
const getTeamsGitLab = async ({
accessToken
}: {
accessToken: string;
}) => {
let teams = [];
try {
const res = (await request.get(
`${INTEGRATION_GITLAB_API_URL}/v4/groups`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Accept-Encoding": "application/json"
}
}
)).data;
teams = res.map((t: any) => ({
name: t.name,
teamId: t.id
}));
} catch (err) {
Sentry.setUser(null);
Sentry.captureException(err);
throw new Error("Failed to get GitLab integration teams");
}
return teams;
}
export {
getTeams
}
+3 -3
View File
@@ -23,9 +23,9 @@ export interface IIntegrationAuth {
refreshCiphertext?: string;
refreshIV?: string;
refreshTag?: string;
accessIdCiphertext?: string; // new
accessIdIV?: string; // new
accessIdTag?: string; // new
accessIdCiphertext?: string;
accessIdIV?: string;
accessIdTag?: string;
accessCiphertext?: string;
accessIV?: string;
accessTag?: string;
+18 -1
View File
@@ -1,6 +1,6 @@
import express from 'express';
const router = express.Router();
import { body, param } from 'express-validator';
import { body, param, query } from 'express-validator';
import {
requireAuth,
requireWorkspaceAuth,
@@ -64,6 +64,9 @@ router.post(
integrationAuthController.saveIntegrationAccessToken
);
// this can optionally accept a teamId?
// IF teamId is passed in then it probably means that we want to get
// the apps for that team
router.get(
'/:integrationAuthId/apps',
requireAuth({
@@ -73,10 +76,24 @@ router.get(
acceptedRoles: [ADMIN, MEMBER]
}),
param('integrationAuthId'),
query('entity'),
validateRequest,
integrationAuthController.getIntegrationAuthApps
);
router.get(
'/:integrationAuthId/teams',
requireAuth({
acceptedAuthModes: ['jwt']
}),
requireIntegrationAuthorizationAuth({
acceptedRoles: [ADMIN, MEMBER]
}),
param('integrationAuthId'),
validateRequest,
integrationAuthController.getIntegrationAuthTeams
);
router.delete(
'/:integrationAuthId',
requireAuth({
+2 -1
View File
@@ -59,6 +59,7 @@ const INTEGRATION_FLYIO_API_URL = "https://api.fly.io/graphql";
const INTEGRATION_CIRCLECI_API_URL = "https://circleci.com/api";
const INTEGRATION_TRAVISCI_API_URL = "https://api.travis-ci.com";
// TODO: deprecate types?
const INTEGRATION_OPTIONS = [
{
name: 'Heroku',
@@ -156,7 +157,7 @@ const INTEGRATION_OPTIONS = [
slug: 'gitlab',
image: 'GitLab.png',
isAvailable: true,
type: 'oauth',
type: 'custom',
clientId: CLIENT_ID_GITLAB,
docsLink: ''
},